Hadoop:Mapper没有';无法从多个输入路径读取文件
映射程序无法从多个目录中读取文件。有人能帮忙吗? 我需要在每个映射器中读取一个文件。我添加了多个输入路径,并实现了自定义的WholeFileInputFormat、WholeFileRecordReader。在map方法中,我不需要输入键。我确保每个地图都可以读取整个文件 命令行:hadoop jar AutoProduce.jar AutoProduce/input\u a/input\u b/output 我指定了两个输入路径--1.input_a;2.输入 运行方法片段:Hadoop:Mapper没有';无法从多个输入路径读取文件,map,hadoop,path,file-read,Map,Hadoop,Path,File Read,映射程序无法从多个目录中读取文件。有人能帮忙吗? 我需要在每个映射器中读取一个文件。我添加了多个输入路径,并实现了自定义的WholeFileInputFormat、WholeFileRecordReader。在map方法中,我不需要输入键。我确保每个地图都可以读取整个文件 命令行:hadoop jar AutoProduce.jar AutoProduce/input\u a/input\u b/output 我指定了两个输入路径--1.input_a;2.输入 运行方法片段: Job job
Job job = new Job(getConf());
job.setInputFormatClass(WholeFileInputFormat.class);
FileInputFormat.setInputPaths(job, new Path(args[0]), new Path(args[1]));
FileOutputFormat.setOutputPath(job, new Path(args[2]));
public void map(NullWritable key, BytesWritable value, Context context){
FileSplit fileSplit = (FileSplit) context.getInputSplit();
System.out.println("Directory :" + fileSplit.getPath().toString());
......
}
映射方法片段:
Job job = new Job(getConf());
job.setInputFormatClass(WholeFileInputFormat.class);
FileInputFormat.setInputPaths(job, new Path(args[0]), new Path(args[1]));
FileOutputFormat.setOutputPath(job, new Path(args[2]));
public void map(NullWritable key, BytesWritable value, Context context){
FileSplit fileSplit = (FileSplit) context.getInputSplit();
System.out.println("Directory :" + fileSplit.getPath().toString());
......
}
自定义全局文件输入格式:
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;
}
}
类WholeFileInputFormat扩展FileInputFormat{
@凌驾
受保护的布尔isSplitable(JobContext上下文,路径文件){
返回false;
}
@凌驾
公共记录阅读器createRecordReader(
InputSplit拆分,TaskAttemptContext上下文)引发IOException,
中断异常{
WholeFileRecordReader=新的WholeFileRecordReader();
初始化(拆分,上下文);
返回读取器;
}
}
自定义WholeFileRecordReader:
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
}
}
类WholeFileRecordReader扩展了RecordReader{
私有文件分割文件分割;
私有配置配置;
私有BytesWritable值=新的BytesWritable();
私有布尔值=false;
@凌驾
公共void初始化(InputSplit拆分,TaskAttemptContext上下文)
抛出IOException、InterruptedException{
this.fileSplit=(fileSplit)split;
this.conf=context.getConfiguration();
}
@凌驾
公共布尔值nextKeyValue()引发IOException、InterruptedException{
如果(!已处理){
byte[]contents=新字节[(int)fileSplit.getLength()];
Path file=fileSplit.getPath();
FileSystem fs=file.getFileSystem(conf);
FSDataInputStream in=null;
试一试{
in=fs.open(文件);
IOUtils.readFully(in,contents,0,contents.length);
value.set(contents,0,contents.length);
}最后{
IOUtils.closeStream(in);
}
已处理=真;
返回true;
}
返回false;
}
@凌驾
public NullWritable getCurrentKey()引发IOException、InterruptedException{
返回NullWritable.get();
}
@凌驾
public BytesWritable getCurrentValue()引发IOException、InterruptedException{
返回值;
}
@凌驾
公共浮点getProgress()引发IOException{
已处理退货?1.0f:0.0f;
}
@凌驾
public void close()引发IOException{
//无所事事
}
}
问题:
设置两个输入路径后,所有映射任务仅从一个目录读取文件
提前感谢。您必须在驱动程序中使用
多个输入
,而不是文件输入格式
。因此,您的代码应该是:
MultipleInputs.addInputPath(job, new Path(args[0]), <Input_Format_Class_1>);
MultipleInputs.addInputPath(job, new Path(args[1]), <Input_Format_Class_2>);
.
.
.
MultipleInputs.addInputPath(job, new Path(args[N-1]), <Input_Format_Class_N>);
希望这对你有用 您必须在驱动程序中使用
MultipleInputs
而不是FileInputFormat
。因此,您的代码应该是:
MultipleInputs.addInputPath(job, new Path(args[0]), <Input_Format_Class_1>);
MultipleInputs.addInputPath(job, new Path(args[1]), <Input_Format_Class_2>);
.
.
.
MultipleInputs.addInputPath(job, new Path(args[N-1]), <Input_Format_Class_N>);
希望这对你有用 我想知道job是否仍应设置InputFormatClass,因为在各个文件旁边有InputFormatClass。我想知道job是否仍应设置InputFormatClass,因为在各个文件旁边有InputFormatClass。