gpt4 book ai didi

java - 如何在hadoopmapreduce中编写avro输出?

转载 作者:行者123 更新时间:2023-12-01 13:24:06 24 4
gpt4 key购买 nike

我编写了一个 Hadoop 字数统计程序,它接受 TextInputFormat 输入,并以 avro 格式输出字数统计。

Map-Reduce 作业运行良好,但可以使用 morevi 等 UNIX 命令读取该作业的输出。我原以为这个输出是不可读的,因为 avro 输出是二进制格式的。

我只使用了mapper,没有reducer。我只是想尝试 avro,所以我不担心内存或堆栈溢出。遵循mapper的代码

public class WordCountMapper extends Mapper<LongWritable, Text, AvroKey<String>, AvroValue<Integer>> {

private Map<String, Integer> wordCountMap = new HashMap<String, Integer>();

@Override
protected void map(LongWritable key, Text value, Context context) throws IOException, InterruptedException {
String[] keys = value.toString().split("[\\s-*,\":]");
for (String currentKey : keys) {
int currentCount = 1;
String currentToken = currentKey.trim().toLowerCase();
if(wordCountMap.containsKey(currentToken)) {
currentCount = wordCountMap.get(currentToken);
currentCount++;
}
wordCountMap.put(currentToken, currentCount);
}
System.out.println("DEBUG : total number of unique words = " + wordCountMap.size());
}

@Override
protected void cleanup(Context context) throws IOException, InterruptedException {
for (Map.Entry<String, Integer> currentKeyValue : wordCountMap.entrySet()) {
AvroKey<String> currentKey = new AvroKey<String>(currentKeyValue.getKey());
AvroValue<Integer> currentValue = new AvroValue<Integer>(currentKeyValue.getValue());
context.write(currentKey, currentValue);
}
}
}

驱动代码如下:

public int run(String[] args) throws Exception {

Job avroJob = new Job(getConf());
avroJob.setJarByClass(AvroWordCount.class);
avroJob.setJobName("Avro word count");

avroJob.setInputFormatClass(TextInputFormat.class);
avroJob.setMapperClass(WordCountMapper.class);

AvroJob.setInputKeySchema(avroJob, Schema.create(Type.INT));
AvroJob.setInputValueSchema(avroJob, Schema.create(Type.STRING));

AvroJob.setMapOutputKeySchema(avroJob, Schema.create(Type.STRING));
AvroJob.setMapOutputValueSchema(avroJob, Schema.create(Type.INT));

AvroJob.setOutputKeySchema(avroJob, Schema.create(Type.STRING));
AvroJob.setOutputValueSchema(avroJob, Schema.create(Type.INT));


FileInputFormat.addInputPath(avroJob, new Path(args[0]));
FileOutputFormat.setOutputPath(avroJob, new Path(args[1]));

return avroJob.waitForCompletion(true) ? 0 : 1;
}

我想知道 avro 输出是什么样的以及我在这个程序中做错了什么。

最佳答案

最新版本的 Avro 库包括更新的 example MRv2 采用的 ColorCount 示例。我建议你看看它,使用与Reduce类中使用的相同的模式或者只是extend AvroMapper 。请注意,使用 Pair类而不是 AvroKey+AvroValue 对于在 Hadoop 上运行 Avro 也至关重要。

关于java - 如何在hadoopmapreduce中编写avro输出?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/21876062/

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