在map函数中想要获取当前split所读取的文件名,方法如下:
1 不通用方法
1 如果读入的文件类型为 TextInputFormat,则获取文件名如下:
String filePath = context.getConfiguration().get("mapreduce.map.input.file");
Path tmpFilePath = new Path(filePath);
// 获取文件路径
filePath = tmpFilePath.getParent().toUri().getPath();
// 获取文件文件名
String fileName = tmpFilePath.getName();
2 通用方法
import org.apache.hadoop.mapreduce.InputSplit;
import org.apache.hadoop.mapreduce.Mapper;
import org.apache.hadoop.mapreduce.lib.input.FileSplit;
InputSplit inputSplit =context.getInputSplit();
// 获取文件名
String fileName = ((FileSplit)inputSplit).getPath().getName();
// 获取文件路径
String filePath = ((FileSplit)inputSplit).getPath().getParent().toUri().getPath();
该方法在使用 MultipleInputs.addInputPath 时有时候会报一个错误