【发布时间】:2015-06-04 16:43:29
【问题描述】:
在使用 Hadoop 2.6 时扩展我的标题.. 并且需要一次将整个文件发送到我的映射器而不是单行。我已按照权威指南中的 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));
}
}
【问题讨论】: