我正在开发一个 hadoop map-reduce 程序,我没有设置映射器和减速器,也没有从我的程序中为作业配置设置任何其他参数。我这样做是假设作业将向输出文件发送与输入相同的输出。但是我发现它在输出文件中打印了一些虚拟整数值,每一行都由制表符分隔(我猜)。
这是我的代码:
import org.apache.hadoop.conf.Configured;
import org.apache.hadoop.fs.Path;
import org.apache.hadoop.mapreduce.Job;
import org.apache.hadoop.mapreduce.lib.input.FileInputFormat;
import org.apache.hadoop.mapreduce.lib.output.FileOutputFormat;
import org.apache.hadoop.util.Tool;
import org.apache.hadoop.util.ToolRunner;
public class MinimalMapReduce extends Configured implements Tool {
public int run(String[] args) throws Exception {
Job job = new Job(getConf());
job.setJarByClass(getClass());
FileInputFormat.addInputPath(job, new Path(args[0]));
FileOutputFormat.setOutputPath(job, new Path(args[1]));
return job.waitForCompletion(true) ? 0 : 1;
}
public static void main(String[] args) {
String argg[] = {"/Users/***/Documents/hadoop/input/input.txt",
"/Users/***/Documents/hadoop/output_MinimalMapReduce"};
try{
int exitCode = ToolRunner.run(new MinimalMapReduce(), argg);
System.exit(exitCode);
}catch(Exception e){
e.printStackTrace();
}
}
}
这是输入:
2011 22
2011 25
2012 40
2013 35
2013 38
2014 44
2015 43
这是输出:
0 2011 22
8 2011 25
16 2012 40
24 2013 35
32 2013 38
40 2014 44
48 2015 43
如何获得与输入相同的输出?
我同意@philantrovert 的回答,但这是我发现的更多细节。根据 Hadoop- The Definitive Guide,它是 TextInputFormat,它将偏移量添加到行号。这是有关 TextInputFormat 的文档:
TextInputFormat 是默认的 InputFormat。每条记录都是一行输入。LongWritable 键是文件中行首的字节偏移量。该值是该行的内容,不包括任何行终止符(例如,换行符或回车符),并被打包为一个 Text 对象。因此,包含以下文本的文件:
On the top of the Crumpetty Tree
The Quangle Wangle sat,
But his face you could not see,
On account of his Beaver Hat.
分为四个记录的一个分割。记录被解释为以下键值对:
(0, On the top of the Crumpetty Tree)
(33, The Quangle Wangle sat,)
(57, But his face you could not see,)
(89, On account of his Beaver Hat.)
显然,键不是行号。这在一般情况下是不可能实现的,因为文件在字节而不是行边界处被分成多个部分。拆分是独立处理的。行号实际上是一个连续的概念。您必须在使用它们时保留行数,因此可以知道拆分中的行号,但不能知道文件中的行号。
但是,每行文件内的偏移量由每个拆分独立于其他拆分知道,因为每个拆分都知道前面拆分的大小,并且只需将其添加到拆分内的偏移上以生成全局文件偏移。对于需要每行唯一标识符的应用程序,偏移量通常就足够了。结合文件名,它在文件系统中是唯一的。当然,如果所有的线都是固定的宽度,那么计算线数就是将偏移量除以宽度的简单问题。
本文收集自互联网,转载请注明来源。
如有侵权,请联系[email protected] 删除。
我来说两句