我目前正在努力与mapreduce一点。我有以下数据集:
1,John,Computer
2,Anne,Computer
3,John,Mobile
4,Julia,Mobile
5,Jack,Mobile
6,Jack,TV
7,John,Computer
8,Jack,TV
9,Jack,TV
10,Anne,Mobile
11,Anne,Computer
12,Julia,Mobile
现在我想在这个数据集上应用mapreduce和分组和聚合,以便输出不仅显示哪个人买了多少次东西,还显示了什么产品,哪个人订购的最多。
所以输出应该如下所示:
John 3 Computer
Anne 3 Mobile
Jack 4 TV
Julia 2 Mobile
我目前的mapper和reducer的实现看起来是这样的,它完美地返回了每个人下了多少命令,然而,我真的不知道如何获得所需的输出。
static class CountMatchesMapper extends Mapper<Object,Text,Text,IntWritable> {
@Override
protected void map(Object key, Text value, Context ctx) throws IOException, InterruptedException {
String row = value.toString();
String[] row_part = row.split(",");
try{
ctx.write(new Text(row_part[1]), new IntWritable(1));
catch (IOException e) {
}
catch (InterruptedException e) {
}
}
}
}
static class CountMatchesReducer extends Reducer<Text,IntWritable,Text,IntWritable> {
@Override
protected void reduce(Text key, Iterable<IntWritable> values, Context ctx) throws IOException, InterruptedException {
int i = 0;
for (IntWritable value : values) i += value.get();
try{
ctx.write(key, new IntWritable(i));
}
catch (IOException e) {
}
catch (InterruptedException e) {
}
}
}
我非常感谢任何有效的解决方案和帮助。
提前谢谢!
1条答案
按热度按时间7uzetpgm1#
如果我正确理解您的要求,我认为第二条输出线应该是:
基于输入。安妮总共买了3种产品:2台电脑和1台手机。
我这里有一个非常基本和简单的方法,它不考虑边缘情况等,但可以给你一些方向:
以上将给出您描述的输出。
如果您想要一个适当的解决方案来扩展等,那么您可能需要一个复合键和定制的groupcomparator。这样你就可以添加合路器以及使它更有效率。然而,上述方法应该适用于一般情况。