2

在 map-reduce 中,我将提取输入文件名,如下所示

public void map(WritableComparable<Text> key, Text value, OutputCollector<Text,Text> output, Reporter reporter)
        throws IOException {

      FileSplit fileSplit = (FileSplit)reporter.getInputSplit();
      String filename = fileSplit.getPath().getName();
      System.out.println("File name "+filename);
      System.out.println("Directory and File name"+fileSplit.getPath().toString());

    process(key,value);

}

我怎样才能用级联做类似的事情

Pipe assembly = new Pipe(SomeFlowFactory.class.getSimpleName());
Function<Object> parseFunc = new SomeParseFunction();
assembly = new Each(assembly, new Fields(LINE), parseFunc);
...

public class SomeParseFunction extends BaseOperation<Object> implements Function<Object> {
...

 @Override
    public void operate(FlowProcess flowProcess, FunctionCall<Object> functionCall) {

how can I get the input file name here ???    
}

谢谢,

4

3 回答 3

1

我不使用级联,但我认为使用 functionCall.getContext() 访问上下文实例就足够了,以获得您可以使用的文件名:

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

但是,似乎级联使用旧 API,如果上述方法不起作用,您必须尝试:

Object name = flowProcess.getProperty( "map.input.file" );
于 2012-12-11T05:07:45.563 回答
1

感谢Engineiro分享答案。但是,当调用 hfp.getReporter().getInputSplit() 方法时,我得到了 MultiInputSplit 类型,它不能在级联 2.5.3 中直接转换为 FileSplit 类型。在深入研究了相关的级联 API 之后,我找到了一种方法并成功检索了输入文件名。因此,我想分享这个来补充Engineiro的答案。请看下面的代码。

HadoopFlowProcess hfp = (HadoopFlowProcess) flowProcess;
MultiInputSplit mis = (MultiInputSplit) hfp.getReporter().getInputSplit();
FileSplit fs = (FileSplit) mis.getWrappedInputSplit();
String fileName = fs.getPath().getName();
于 2014-06-02T12:58:56.713 回答
0

You would do this by getting the reporter within the buffer class, from the provided flowprocess argument in the buffer operate call.

HadoopFlowProcess hfp = (HadoopFlowProcess) flowprocess; 

FileSplit fileSplit = (FileSplit)hfp.getReporter().getInputSplit();
.
.//the rest of your code
.
于 2013-08-20T19:38:00.350 回答