因此,我只是在学习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/