我的代码似乎是正确的,但在cmd上它没有给出正确的输出,请任何人帮我找到代码中的问题。它运行正常,但输出错误:
package test;
import java.io.IOException;
import org.apache.hadoop.conf.Configuration;
import org.apache.hadoop.fs.Path;
import org.apache.hadoop.io.IntWritable;
import org.apache.hadoop.io.Text;
//import org.apache.hadoop.mapred.JobConf;
import org.apache.hadoop.mapreduce.Job;
import org.apache.hadoop.mapreduce.Mapper;
import org.apache.hadoop.mapreduce.Reducer;
import org.apache.hadoop.mapreduce.lib.input.FileInputFormat;
import org.apache.hadoop.mapreduce.lib.output.FileOutputFormat;
public class temp {
public static class Mymapper extends Mapper<Object, Text, IntWritable,Text> {
public void map(Object key, Text value,Context context) throws IOException, InterruptedException{
int month=Integer.parseInt(value.toString().substring(16, 18));
IntWritable mon=new IntWritable(month);
String temp=value.toString().substring(26,30);
String t=null;
for(int i=0;i<temp.length();i++){
if(temp.charAt(i)==',')
break;
else
t=t+temp.charAt(i);
}
Text data=new Text(value.toString().substring(21, 25)+t);
context.write(mon, data);
}
}
public static class Myreducer extends Reducer<IntWritable,Text,IntWritable,IntWritable> {
public void reduce(IntWritable key,Iterable<Text> values,Context context) throws IOException, InterruptedException{
String temp="";
int max=0;
for(Text t:values)
{
temp=t.toString();
if(temp.substring(0, 4)=="TMAX"){
if(Integer.parseInt(temp.substring(4,temp.length()))>max){
max=Integer.parseInt(temp.substring(4,temp.length()));
}
}
}
context.write(key,new IntWritable(max));
}
}
public static void main(String[] args) throws Exception {
Configuration conf = new Configuration();
Job job = Job.getInstance(conf, "temp");
job.setJarByClass(temp.class);
job.setMapperClass(Mymapper.class);
job.setReducerClass(Myreducer.class);
job.setMapOutputKeyClass(IntWritable.class);
job.setMapOutputValueClass(Text.class);
job.setOutputKeyClass(IntWritable.class);
job.setOutputValueClass(IntWritable.class);
FileInputFormat.addInputPath(job, new Path(args[0]));
FileOutputFormat.setOutputPath(job, new Path(args[1]));
job.waitForCompletion(true);
}
}
输入文件:
USC0037919000121,最大值,-78,,,6,
USC0037919000131,最大值,-133,,,6,
USC0037919000111,最大值,127,,,6,
此代码的输出为:
12 0
13 0
11 0
3条答案
按热度按时间bxpogfeg1#
让我们录第一张唱片:
在这个记录里
19000121
:因此,预期输出应为:
要实现这一点,您需要在代码中进行以下更改。
map()方法:
解析月份
更改:
收件人:
字符串初始化
更改:
收件人:
reduce()方法:
字符串比较
更改:
收件人:
通过这些更改,我得到了以下输出:
k3bvogb12#
下面的代码将修复该问题。用下面的代码替换mymapper和myreducer
vptzau2j3#
看起来您已经将索引移动了1(应该是子字符串(15,17)而不是子字符串(16,18)),但是我无法根据截取的输入文件找出原因(也许您在这里遗漏了什么?)
由于指数的变化,你得到的是12、13、11个月,而不是01、01、01。由于移位索引“tmax”没有显示在字符串的开头,所以接收到0作为最高温度。
建议:
您有逗号分隔的csv文件,所以不要使用子字符串。可以使用value.tostring().split(“,”)接收部分记录。
看看你需要什么零件(日期和温度)。
使用子字符串方法从日期部分获取月份。
减速机不需要tmax部件。因此,只能生成intwitable、intwritable(month、temperature)作为Map器输出。5.去除减速机中的tmax检查(仅使用温度可写值)。
建议:不要每次都在mapper和reducer中创建文本和可写示例。您可以使其成为示例字段并使用set方法(它优化内存)