对于MapReduce而言,在Map端经常需要知道处理文件的输入路径,以此来区分不同的处理方式。
我们知道在MapReduce框架中会将输入的文件切分成许多(InputSplit)文件块,每个文件块包含了文件路径,起止偏移量等信息,每一个文件块交给一个map任务进行处理。而文件块的生成是通过InputFormat调用getSplits方法实现的,不同的InputFormat会有不同的切分规则,生成不同类型的文件块。
因此不同的InputFormat采用不同的获取文件路径的方法:
1、一般FileInputFormat
一般的FileInputFormat是MapReduce中最常用的一种输入格式,包括TextInputFormat,ParquetInputFormat,SequenceFileInputFormat等,这些输入格式切分的文件块类型为FileSplit。
因此获取文件路径方法为:
@Override
public void map(LongWritable key, Text value, Context context)
throws IOException, InterruptedException{
InputSplit split = context.getInputSplit();
FileSplit fileSplit = (FileSplit) split;
String filePath = fileSplit.getPath().toUri().getPath();
......
}
2、CombineFileInputFormat
CombineFileInputFormat输入格式实现的是小文件合并输入&#x