【发布时间】:2016-12-07 05:47:06
【问题描述】:
我正在尝试使用 MapReduce 来查找由标签分隔的制表符分隔输入的总和。数据是这样的
1 5.0 4.0 6.0
2 2.0 1.0 3.0
1 3.0 4.0 8.0
第一列是类标签,所以我期待一个按类标签分类的输出。对于这种情况,输出将是
label 1: 30.0
label 2: 6.0
这是我尝试过的代码,但我得到了错误的输出和
显示意外的类标签。
public class Total {
public static class Map extends Mapper<LongWritable, Text, Text, DoubleWritable> {
private final static DoubleWritable one = new DoubleWritable();
private Text word = new Text();
public void map(LongWritable key, Text value, Context context) throws IOException, InterruptedException {
String line = value.toString();
StringTokenizer tokenizer = new StringTokenizer(line);
word.set(tokenizer.nextToken());
while (tokenizer.hasMoreTokens()) {
one.set(Double.valueOf(tokenizer.nextToken()));
context.write(word, one);
}
}
}
public static class Reduce extends Reducer<Text, DoubleWritable, Text, DoubleWritable> {
private Text Msg = new Text();
public void reduce(Text key, Iterable<DoubleWritable> values, Context context)
throws IOException, InterruptedException {
firstMsg.set("label " + key+": Total");
Double sum = 0.0;
for (DoubleWritable val : values) {
sum += val.get();
}
context.write(Msg, new DoubleWritable(sum));
}
}
//void method implementation also exists
}
【问题讨论】:
标签: java hadoop mapreduce hdfs hadoop2