要在使用Hadoop 2.6 ..时扩展我的 header ,并且需要将整个文件发送到我的映射器,而不是一次发送一行。我已经按照《权威指南》中的Tom Whites代码创建了WholeFileInputFormat和WholeFileRecordReader,但是我的Mapper仍在一次处理1行文件。谁能看到我的代码中缺少的内容?我所看到的完全是本书的例子。任何指导将不胜感激。
WholeFileInputFormat.java
public class WholeFileInputFormat extends FileInputFormat <NullWritable, BytesWritable>{
@Override
protected boolean isSplitable(JobContext context, Path file){
return false;
}
@Override
public RecordReader<NullWritable, BytesWritable> createRecordReader(
InputSplit split, TaskAttemptContext context) throws IOException, InterruptedException {
WholeFileRecordReader reader = new WholeFileRecordReader();
reader.initialize(split, context);
return reader;
}
}
WholeFileRecordReader.java
public class WholeFileRecordReader extends RecordReader<NullWritable, BytesWritable> {
private FileSplit fileSplit;
private Configuration conf;
private BytesWritable value = new BytesWritable();
private boolean processed = false;
@Override
public void initialize(InputSplit split, TaskAttemptContext context) throws IOException, InterruptedException{
this.fileSplit = (FileSplit) split;
this.conf = context.getConfiguration();
}
@Override
public boolean nextKeyValue() throws IOException, InterruptedException{
if (!processed){
byte[] contents = new byte[(int) fileSplit.getLength()];
Path file = fileSplit.getPath();
FileSystem fs = file.getFileSystem(conf);
FSDataInputStream in = null;
try{
in = fs.open(file);
IOUtils.readFully(in, contents, 0, contents.length);
value.set(contents, 0, contents.length);
}finally{
IOUtils.closeStream(in);
}
processed = true;
return true;
}
return false;
}
@Override
public NullWritable getCurrentKey() throws IOException, InterruptedException{
return NullWritable.get();
}
@Override
public BytesWritable getCurrentValue() throws IOException, InterruptedException{
return value;
}
@Override
public float getProgress() throws IOException {
return processed ? 1.0f : 0.0f;
}
@Override
public void close() throws IOException{
//do nothing :)
}
}
而我的Mapreduce的主要方法
public class ECCCount {
public static void main(String[] args) throws Exception {
if (args.length != 2) {
System.out.printf("Usage: ProcessLogs <input dir> <output dir>\n");
System.exit(-1);
}
//@SuppressWarnings("deprecation")
Job job = new Job();
job.setJarByClass(ECCCount.class);
job.setJobName("ECCCount");
//FileInputFormat.setInputPaths(job, new Path(args[0]));
WholeFileInputFormat.setInputPaths(job, new Path(args[0]));
FileOutputFormat.setOutputPath(job, new Path(args[1]));
job.setMapperClass(ECCCountMapper.class);
job.setReducerClass(SumReducer.class);
job.setOutputKeyClass(Text.class);
job.setOutputValueClass(IntWritable.class);
boolean success = job.waitForCompletion(true);
System.exit(success ? 0 : 1);
}
}
和我的Mapper很好。现在,它只是简单地返回作为测试用例给出的值,以查看其返回的是一行还是整个文件
public class ECCCountMapper extends Mapper<LongWritable, Text, Text, IntWritable>{
@Override
public void map(LongWritable key, Text value, Context context)
throws IOException, InterruptedException {
context.write(new Text(value), new IntWritable(1));
}
}
最佳答案
问题可以是映射器的输入格式。您有LongWritable和文本。但是,在上述示例中,他们使用了NullWritable,BytesWritable,因为这就是WholeFileInputFormat所具有的。另外,您需要给 job.setInputFormatClass(WholeFileInputFormat.class); Job类中的(主要方法)。希望对您有所帮助,祝您编码愉快