【发布时间】:2016-05-07 20:05:30
【问题描述】:
这是Extracting rows containing specific value using mapReduce and hadoop的后续问题
Mapper函数
public static class MapForWordCount extends Mapper<Object, Text, Text, IntWritable>{
private IntWritable saleValue = new IntWritable();
private Text rangeValue = new Text();
public void map(Object key, Text value, Context con) throws IOException, InterruptedException
{
String line = value.toString();
String[] words = line.split(",");
for(String word: words )
{
if(words[3].equals("40")){
saleValue.set(Integer.parseInt(words[0]));
rangeValue.set(words[3]);
con.write( rangeValue , saleValue );
}
}
}
}
减速器功能
public static class ReduceForWordCount extends Reducer<Text, IntWritable, Text, IntWritable>
{
private IntWritable result = new IntWritable();
public void reduce(Text word, Iterable<IntWritable> values, Context con) throws IOException, InterruptedException
{
for(IntWritable value : values)
{
result.set(value.get());
con.write(word, result);
}
}
}
得到的输出是
40 105
40 105
40 105
40 105
编辑 1: 但预期的输出是
40 102
40 104
40 105
我做错了什么?
mapper 和 reducer 函数到底发生了什么?
【问题讨论】:
-
您正在写出键值对...您还想知道什么?
-
感谢@cricket_007 的建议,我一定会尝试...我实际上想知道mapper 返回和reducer 到底做了什么- 接受和打印。
-
当您
extends他们时,两个类的顺序都是<KeyIn, ValueIn, KeyOut, ValueOut>。而且mapper的输出key-value必须和reducer的输入key-value匹配 -
提供更多信息 - 映射器正在使用上下文对象将值写入减速器(而不是“返回”),并且减速器将值发送到输出(再次使用上下文 - 而不是通过“返回”)。映射器将具有相同“键”的所有值“发送”到同一个减速器(这实际上发生在 shuffle 阶段),因此每个减速器将在一组具有相同键的值上“运行”。
-
感谢@It-Z,这正是我想要的。
标签: hadoop mapreduce hadoop2 feature-extraction mapper