MapReduce如何减少reducer生成的输出?

时间:2016-12-15 14:30:43

标签: java csv hadoop mapreduce

我正面临一个问题,我想进一步减少输出生成我的减速器。我使用CSV作为输入。 CSV显示每个记录都缺少一名学生。

对于每个ClassRoom,计算输入数据中记录至少五名学生缺席的天数。在输出中,输出中的每个记录应该具有代表ClassRoom作为关键字的数字,以及至少有五个学生缺席的天数作为值。

注意:显示的以下输入仅适用于需要处理数据的列。原始CSV具有更多的thaqn 18列,其中包含不同的数据。所以Date列是第10列,Class列是第13列。

INPUT screenshot

Date    Class  
01/01/2011  12  
03/01/2011  12  
01/01/2011  12  
01/01/2011  12  
01/01/2011  12  
01/01/2011  12  
11/01/2011  12  
11/01/2011  13  
11/01/2011  13  
11/01/2011  13  
11/01/2011  13  
11/01/2011  13  
21/03/2011  14  
21/03/2011  14  
26/04/2011  12  
26/04/2011  12  
26/04/2011  12  
26/04/2011  12  
26/04/2011  12  

目前我正在尝试使用以下Map和Reduce

MAP:

public static class TestMapper extends Mapper<Object, Text, Text, IntWritable> {

    private final static IntWritable one = new IntWritable(1);
    private Text word = new Text();

    public void map(Object key, Text value, Context context) 
                   throws IOException, InterruptedException {

        String[] tokens = value.toString().split(",");
        String district = tokens[12];
        String dateColumn = tokens[9];           

        if (districtColumn == "Class") {
            return;
        }
        else 
        {
            word.set(district+"|"+dateColumn);
            context.write(word, one);
        }       
    }
}

减少:

public static class TestReducer extends Reducer<Text,IntWritable,Text,Text> {
    private Text word = new Text();
    private Text word2 = new Text();

    public void reduce(Text key, Iterable<IntWritable> values, Context context) 
                         throws IOException, InterruptedException {
        int no = 0;
        int no2 = 0;
        for (IntWritable val : values) {
            no += val.get();
        }

        if (no>=5) {
            no2 = 1;
            String S = key.toString();
            String[] A = S.split("\\|");
            word.set(A[0]);
            word2.set(no2);

            context.write(word, word2);
            no2++;
        }    
    }
}

实际输出:

12 1  
11 1

预期输出:

12 2  
11 1

Expected OUTPUT screenshot

0 个答案:

没有答案
相关问题