gpt4 book ai didi

hadoop - MapReduce 程序在读取文本文件时保持计数器

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

我正在尝试实现一个 map reduce 程序,以便输出是 .txt 文件的对角线。 例如,读取文件

a*****
*b****
**c***
***d**
****e*
*****f

我希望输出为 abcdef

我写的映射器类是这个:

public class MapperClass extends MapReduceBase implements Mapper<LongWritable, Text, Text, Text>
{
//hadoop supported data types
private static final Text t = new Text("");
private Text word = new Text();
//private static int linenumber = 0;

public void map(LongWritable key, Text value, OutputCollector<Text, Text> output, Reporter reporter) throws IOException
{
//taking one line at a time from input file
String line = value.toString();
int linenumber = 0;
word.set(Character.toString(line.charAt(linenumber++)));
output.collect(word, t);
}
}

但是我得到的输出是

a
*
*
*
*
*

我试图将行号放在 map 方法之外,但仍然得到相同的结果。有人可以帮忙吗?我只需要找到一种方法来保留一个计数器,当我从文件中读取下一行时该计数器会递增。P.S. 我认为这里不需要 reducer,因为我不想对任何中间结果进行排序。如果我错了,请纠正我。谢谢!

最佳答案

使用已提供给您的map 方法的LongWritable 键 参数,并指向已处理文件中的行号。

通常,您无法跟踪映射器中的 linenumber,因为一个文件可能会被多个映射器处理(尤其是当您使用 TextInputFormat 时,它假定常规文本文件是 splittable )。这种全局状态通常只在计数器中才有意义。

关于hadoop - MapReduce 程序在读取文本文件时保持计数器,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/30241003/

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