老mr程序中map中conf的map.input.file参数只能获取获取CombineTextInputFormat的第一个输入文件,而新版mr程序则连第一个输入文件也无法获取,这是因为createRecordReader中的TaskAttemptContext context参数与map中的context参数不是一个对象。
解决方案:
如果需要动态获取Combine的输入文件,可以扩展CombineTextInputFormat,重写createRecordReader方法,从中获取context的Configuration对象。
示例:
public class MyCombineTextInputFormat extends CombineTextInputFormat {
private static Configuration conf; public static Configuration conf() {
return conf;
} @Override
public RecordReader<LongWritable, Text> createRecordReader(InputSplit split, TaskAttemptContext context) throws IOException,InterruptedException{
conf = context.getConfiguration();
return super.createRecordReader(split, context);
}
} class MyMapper extends Mapper<LongWritable, Text, Text, Text>{
@Override
public void map(LongWritable key, Text value, Context context) throws IOException, InterruptedException{
Configuration conf = MyCombineTextInputFormat.conf();
String path_str = conf.get("map.input.file", "");
}
}