且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

如何在Hadoop程序的映射器中获取输入文件名?

更新时间:2022-05-25 20:15:11

首先,您需要将输入拆分,使用较新的 mapreduce API 将按如下方式完成:

First you need to get the input split, using the newer mapreduce API it would be done as follows:

context.getInputSplit();

但是为了获得文件路径和文件名,您需要首先将结果类型转换为 FileSplit.

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:

Path filePath = ((FileSplit) context.getInputSplit()).getPath();
String filePathString = ((FileSplit) context.getInputSplit()).getPath().toString();

同样,要获取文件名,您可以调用 getName(),如下所示:

Similarly, to get the file name, you may just call upon getName(), like this:

String fileName = ((FileSplit) context.getInputSplit()).getPath().getName();