public class SelfPatient {
public static class selfMapper1 extends Mapper<LongWritable,Text,Text,IntWritable>
{
public void map(LongWritable key,Text value,Context context) throws IOException,InterruptedException
{
//IntWritable clas =new IntWritable(Integer.parseInt(str.nextToken(",")));
String Line=value.toString();
String[] elements=Line.split(",");
int surv=Integer.parseInt(elements[1]);
Text clas=new Text(elements[4]);
//int i=Integer.parseInt(elements[0]);
//IntWritable number=new IntWritable(i);
context.write(new Text(clas),new IntWritable(surv));
//context.write(clas,number);
}
}
public static class selfReducer1 extends Reducer<Text,IntWritable,Text,IntWritable>
{
public void reduce(Text key,Iterable<IntWritable> values,Context context) throws IOException,InterruptedException
{
int sum=0;
for (IntWritable val :values)
{
sum += val.get();
}
context.write(key, new IntWritable(sum));
}
}
数据
"","survive","cases","age","sex","class"
"1",1,1,0,0,1
"2",13,13,0,0,2
"3",14,31,0,0,3
"4",5,5,0,1,1
"5",11,11,0,1,2
"6",13,48,0,1,3
"7",140,144,1,0,1
"8",80,93,1,0,2
"9",76,165,1,0,3
"10",57,175,1,1,1
"11",14,168,1,1,2
"12",75,462,1,1,3
我想得到的输出是:每个班级的幸存者总数。示例类 1-45、类 2-34。 我的代码有什么问题。
我假设您的"类"是逗号分隔数据中的最后一个数字。如果是这种情况,则您错误地将错误的值分配为键。
String Line=value.toString();
String[] elements=Line.split(",");
int surv=Integer.parseInt(elements[1]);
Text clas=new Text(elements[5]); //instead of elements[4]
发出映射器输出时,不必初始化新的 Text 对象,因为您之前已经这样做了。
context.write(clas, new IntWritable(surv));
这应该可以解决您的问题。干杯。