hadoop - Hadoop中输出的格式和可读性

标签 hadoop output readability

因此,我只是在学习Hadoop并尝试正常运行的WordCount.java教程。我有零问题。我唯一的问题是,当我在输出文件中获取结果时,我想自己添加Strings。因此,可以说我有另一个读取的文件,例如,我希望它可读。

========= Output 1 =========
// Results here
============================

========= Output 2 =========
// 2nd Results here
============================

而不是
// Results here
// More results

我实质上只是希望能够一次将输出发送到输出文件。最佳的位置/方式是什么?我猜这将是主要问题,但我不确定。 hadoop是为此设计的(使用吗?)还是应该使用某种bash脚本来使输出文件漂亮?

下面的代码
import java.io.IOException;
import java.util.*;

import org.apache.hadoop.fs.Path;
import org.apache.hadoop.conf.*;
import org.apache.hadoop.io.*;
import org.apache.hadoop.mapred.*;
import org.apache.hadoop.util.*;

public class WordCount {

public static class Map extends MapReduceBase implements Mapper<LongWritable, Text, Text, IntWritable> {
  private final static IntWritable one = new IntWritable(1);
  private Text word = new Text();

  public void map(LongWritable key, Text value, OutputCollector<Text, IntWritable> output, Reporter reporter) throws IOException {
    String line = value.toString();
    StringTokenizer tokenizer = new StringTokenizer(line);
    while (tokenizer.hasMoreTokens()) {
      word.set(tokenizer.nextToken());
      output.collect(word, one);
    }
  }
}

public static class Reduce extends MapReduceBase implements Reducer<Text, IntWritable, Text, IntWritable> {
  public void reduce(Text key, Iterator<IntWritable> values, OutputCollector<Text, IntWritable> output, Reporter reporter) throws IOException {
    int sum = 0;
    while (values.hasNext()) {
      sum += values.next().get();
    }
    output.collect(key, new IntWritable(sum));
  }
}

public static void main(String[] args) throws Exception {
  JobConf conf = new JobConf(WordCount.class);
  conf.setJobName("wordcount");

  conf.setOutputKeyClass(Text.class);
  conf.setOutputValueClass(IntWritable.class);

  conf.setMapperClass(Map.class);
  conf.setCombinerClass(Reduce.class);
  conf.setReducerClass(Reduce.class);

  conf.setInputFormat(TextInputFormat.class);
  conf.setOutputFormat(TextOutputFormat.class);

  FileInputFormat.setInputPaths(conf, new Path(args[0]));
  FileOutputFormat.setOutputPath(conf, new Path(args[1]));

  JobClient.runJob(conf);
}
}

最佳答案

我相信我可以为此使用NullWritable。如果还有更好的方法,请发布答案。

关于hadoop - Hadoop中输出的格式和可读性,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/40942494/

相关文章:

hadoop - 将 Hive Map 数据对象分解为长格式

plsql - 如何在oracle sql developer中运行pl/sql程序

python - 处理大型 if/else 的最佳方法

html - .Net中推荐的HTML可读性转码库

java - Hadoop MapReduce NoSuchElementException

java - 为 hadoop MapReduce Cleanup 添加进度跟踪机制

hadoop - 云应用程序是什么样的?

c - 为什么我不能通过这段代码得到正确的输出?

java - 用 java 降序输出 Map.Entry<Sentence, Integer>

javascript - 如何在 JavaScript 中将很长的正则表达式拆分为多行?