我很难使用Hadoop map reduce来计算两个值之间的总和。
例如,我想计算[1, 15000]的总和。但据我所知,map-reduce处理的是有共同之处的数据(标签)。
我设法理解了这些数据的模式:
doctor 23
doodle 34
doctor 2
doodle 5 这些是在给定文本中出现的单词find。
使用map reduce将链接给定单词的值,如下所示:
doctor [(23 2)]
doodle [(34 5)]然后计算这些值的和。
但是关于总和,我们从来没有共同之处,比如上面例子中的一根线。给定该数据集:
DS1: 1 2 3 4 5 ..... 15000是否可以使用map reduce架构来计算列表中所有元素的总和?
发布于 2018-04-01 20:46:45
如果在文本文件中有用空格分隔的数字,则可以在映射器中拆分它们并求和,如下所示:
映射器:
public class SumMapper extends Mapper<LongWritable, Text, NullWritable, IntWritable> {
protected void map(LongWritable key, Text value, Context context) throws IOException, InterruptedException {
int sum = Arrays.stream(value.toString().split(" ")).mapToInt(Integer::valueOf).sum();
context.write(NullWritable.get(), new IntWritable(sum));
}
}作业控制:
public class LocalMapReduceRunner {
public static void main(String[] args) throws Exception {
Runtime.getRuntime().exec("rm -rf " + args[1]);
Job job = Job.getInstance(new Configuration());
job.setJobName("MR_runner");
job.setJarByClass(LocalMapReduceRunner.class);
job.setMapperClass(SumMapper.class);
job.setMapOutputKeyClass(NullWritable.class);
job.setOutputValueClass(IntWritable.class);
FileInputFormat.addInputPath(job, new Path(args[0]));
FileOutputFormat.setOutputPath(job, new Path(args[1]));
System.exit(job.waitForCompletion(true) ? 0 : 1);
}
}感谢@cricket_007的建议。
https://stackoverflow.com/questions/49592330
复制相似问题