我的mapreducer无法工作,无法获取输出

5vf7fwbs  于 2021-05-29  发布在  Hadoop
关注(0)|答案(1)|浏览(395)
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。。我的代码有什么问题。

pvcm50d1

pvcm50d11#

我假设你的“类”是逗号分隔数据中的最后一个数字。如果是这样的话,您就犯了一个错误,将错误的值指定为键。

String Line=value.toString();
    String[] elements=Line.split(",");
    int surv=Integer.parseInt(elements[1]);
    Text clas=new Text(elements[5]); //instead of elements[4]

当发出Map器输出时,您不必初始化新的文本对象,因为您之前已经这样做了。

context.write(clas, new IntWritable(surv));

这应该能解决你的问题。干杯。

相关问题