如何在Hadoop程序中的映射器中获取输入文件名? [英] How to get the input file name in the mapper in a Hadoop program?

查看:159
本文介绍了如何在Hadoop程序中的映射器中获取输入文件名?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我如何获得映射器中输入文件的名称?我有多个输入文件存储在输入目录中,每个映射器可能会读取一个不同的文件,我需要知道映射器读取了哪个文件。 解决方案首先,您需要获得输入拆分,使用更新的mapreduce API,它将按如下方式完成:

  context.getInputSplit(); 

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



因此,为了获得输入文件路径,您可以执行以下操作:

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

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

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


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.

解决方案

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

context.getInputSplit();

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();

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

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

这篇关于如何在Hadoop程序中的映射器中获取输入文件名?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆