hadoop中输出的格式和可读性

jutyujz0  于 2021-05-29  发布在  Hadoop
关注(0)|答案(1)|浏览(383)

所以我只是在学习hadoop,并尝试wordcount.java教程,它很好用。我没有任何问题。我唯一的问题是,当我在输出文件中得到结果时,我想自己添加字符串。假设我读了另一个文件,我希望它是可读的。

  1. ========= Output 1 =========
  2. // Results here
  3. ============================
  4. ========= Output 2 =========
  5. // 2nd Results here
  6. ============================

而不是

  1. // Results here
  2. // More results

我基本上只希望能够向输出文件发送一次输出。做这件事最好的地方/方法是什么?我猜主要是这样,但我不确定。hadoop是为此而设计的(使用的?),还是我应该使用某种bash脚本来使输出文件变得漂亮?
代码如下,

  1. import java.io.IOException;
  2. import java.util.*;
  3. import org.apache.hadoop.fs.Path;
  4. import org.apache.hadoop.conf.*;
  5. import org.apache.hadoop.io.*;
  6. import org.apache.hadoop.mapred.*;
  7. import org.apache.hadoop.util.*;
  8. public class WordCount {
  9. public static class Map extends MapReduceBase implements Mapper<LongWritable, Text, Text, IntWritable> {
  10. private final static IntWritable one = new IntWritable(1);
  11. private Text word = new Text();
  12. public void map(LongWritable key, Text value, OutputCollector<Text, IntWritable> output, Reporter reporter) throws IOException {
  13. String line = value.toString();
  14. StringTokenizer tokenizer = new StringTokenizer(line);
  15. while (tokenizer.hasMoreTokens()) {
  16. word.set(tokenizer.nextToken());
  17. output.collect(word, one);
  18. }
  19. }
  20. }
  21. public static class Reduce extends MapReduceBase implements Reducer<Text, IntWritable, Text, IntWritable> {
  22. public void reduce(Text key, Iterator<IntWritable> values, OutputCollector<Text, IntWritable> output, Reporter reporter) throws IOException {
  23. int sum = 0;
  24. while (values.hasNext()) {
  25. sum += values.next().get();
  26. }
  27. output.collect(key, new IntWritable(sum));
  28. }
  29. }
  30. public static void main(String[] args) throws Exception {
  31. JobConf conf = new JobConf(WordCount.class);
  32. conf.setJobName("wordcount");
  33. conf.setOutputKeyClass(Text.class);
  34. conf.setOutputValueClass(IntWritable.class);
  35. conf.setMapperClass(Map.class);
  36. conf.setCombinerClass(Reduce.class);
  37. conf.setReducerClass(Reduce.class);
  38. conf.setInputFormat(TextInputFormat.class);
  39. conf.setOutputFormat(TextOutputFormat.class);
  40. FileInputFormat.setInputPaths(conf, new Path(args[0]));
  41. FileOutputFormat.setOutputPath(conf, new Path(args[1]));
  42. JobClient.runJob(conf);
  43. }
  44. }
e0bqpujr

e0bqpujr1#

我相信我可以用nullwriteable来做这个。如果有更好的方法,尽管请张贴一个答案。

相关问题