gpt4 book ai didi

hadoop - 如何将 Hadoop MapReduce 作业的输出作为值/键而不是键/值返回?

转载 作者:可可西里 更新时间:2023-11-01 16:42:18 26 4
gpt4 key购买 nike

例如,典型的 WordCount mapreduce 可能会返回如下输出:

hello 3
world 4
again 1

我想对输出进行稍微不同的格式化,以便它显示为:

3 hello
4 world
1 again

我读过很多想要按值排序的帖子,答案建议在第一个输出上进行第二个 mapreduce 作业。但是,我不需要按值排序,并且多个键可能具有相同的值——我不希望将它们混为一谈。

有没有一种简单的方法可以简单地切换键/值的打印顺序?看起来应该很简单。

最佳答案

按难易程度顺序考虑的两个选项是:

在 Reduce 中切换 Key/Value

修改reduce的输出以切换键和值。例如 Hadoops example WordCount job 中的减少将更改为:

public static class IntSumReducer extends Reducer<Text,IntWritable,Text,IntWritable> {
private IntWritable result = new IntWritable();

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

这里的 context.write(result, key); 已经改变为切换键和值。

使用第二个仅限 map 的作业

您可以使用 Hadoop 提供的 InverseMapper ( Source ) 运行 Map only (0 reducers) 作业来切换键和值。所以你会有第二份工作,只需要编写驱动程序,它看起来像:

public static void main(String[] args) throws Exception {
Configuration conf = new Configuration();
Job job = Job.getInstance(conf, "Switch inputs");
job.setJarByClass(WordCount.class);
job.setMapperClass(InverseMapper.class);
job.setNumReduceTasks(0);
job.setOutputKeyClass(IntWritable.class);
job.setOutputValueClass(Text.class);
job.setInputFormatClass(SequenceFileInputFormat.class);
FileInputFormat.addInputPath(job, new Path(args[0]));
FileOutputFormat.setOutputPath(job, new Path(args[1]));
System.exit(job.waitForCompletion(true) ? 0 : 1);
}
}

请注意,您可能希望第一个作业使用 SequenceFileOutputFormat 写入第一个作业的输出,并使用 SequenceFileInputFormat 作为第二个作业的输入。

关于hadoop - 如何将 Hadoop MapReduce 作业的输出作为值/键而不是键/值返回?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/39736295/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com