gpt4 book ai didi

Java Hadoop : How can I create mappers that take as input files and give an output which is the number of lines in each file?

转载 作者:可可西里 更新时间:2023-11-01 14:17:43 25 4
gpt4 key购买 nike

我是 Hadoop 的新手,我已经设法运行了 wordCount 示例:http://hadoop.apache.org/common/docs/r0.18.2/mapred_tutorial.html

假设我们有一个包含 3 个文件的文件夹。我希望每个文件都有一个映射器,这个映射器将只计算行数并将其返回给缩减器。

然后,reducer 会将每个映射器的行数作为输入,并将所有 3 个文件中存在的总行数作为输出。

所以如果我们有以下3个文件

input1.txt
input2.txt
input3.txt

映射器返回:

mapper1 -> [input1.txt, 3]
mapper2 -> [input2.txt, 4]
mapper3 -> [input3.txt, 9]

reducer 会输出

3+4+9 = 16 

我已经在一个简单的 Java 应用程序中完成了这项工作,所以我想在 Hadoop 中完成。我只有一台电脑,想尝试在伪分布式环境中运行。

我怎样才能实现这个目标?我应该采取哪些正确的步骤?

我的代码应该像 apache 示例中的那样吗?我将有两个静态类,一个用于 mapper,一个用于 reducer?或者我应该有 3 个类,每个映射器一个?

如果你能指导我完成这个,我不知道该怎么做,我相信如果我设法编写一些代码来做这些事情,那么我将来能够编写更复杂的应用程序。

谢谢!

最佳答案

除了 sa125 的答案之外,您可以通过不为每个输入记录发出记录来极大地提高性能,而只是在映射器中累积一个计数器,然后在映射器清理方法中发出文件名和计数值:

public class LineMapper extends Mapper<LongWritable, Text, Text, LongWritable> {
protected long lines = 0;

@Override
protected void cleanup(Context context) throws IOException,
InterruptedException {
FileSplit split = (FileSplit) context.getInputSplit();
String filename = split.getPath().toString();

context.write(new Text(filename), new LongWritable(lines));
}

@Override
protected void map(LongWritable key, Text value, Context context)
throws IOException, InterruptedException {
lines++;
}
}

关于Java Hadoop : How can I create mappers that take as input files and give an output which is the number of lines in each file?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/10367389/

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