Source From Here
Question
How I can get the name of the input file within a mapper? I have multiple input files stored in the input directory, each mapper may read a different file, and I need to know which file the mapper has read.
How-To
First you need to get the InputSplit object, using the MapReduce v2 API it would be done as follows:
But in order to get the file path and the file name you will need to first typecast the result into
FileSplit. So, in order to get the input file path you may do the following:
Similarly, to get the file name, you may just call upon
getName(), like this:
Question
How I can get the name of the input file within a mapper? I have multiple input files stored in the input directory, each mapper may read a different file, and I need to know which file the mapper has read.
How-To
First you need to get the InputSplit object, using the MapReduce v2 API it would be done as follows:
- ...
- @Override
- public void map(LongWritable key, Text value, Context context)
- throws IOException, InterruptedException {
- InputSplit inputSplit = context.getInputSplit();
- }
- ...
- Path filePath = ((FileSplit) context.getInputSplit()).getPath();
- String filePathString = filePath.toString();
- String fileName = ((FileSplit) context.getInputSplit()).getPath().getName();
沒有留言:
張貼留言