有时任务的多个输入源有着相同的格式,没有其他方式可以做区分,比较恼火。查询文档,发现下面这种方法。
Mapper.Context提供了获取当前Mapper的input split信息的接口,返回的类型是InputSplit,而实际类型一般情况下默认是FileSplit。FileSplit提供了获取当前split的输入文件的路径,可以用此路径区分各个数据源。
我有些数据是按照月日时的方式组织的,目录结构类似于 201104/16/02/part-r-00001 ,数据里没有日期字段。现在有个需求,要求程序能够区分数据的日期,那么可以在对应的mapper类的setup方法中嵌入以下代码:
protected void setup(Context context) throws IOException, InterruptedException {
super.setup(context);
FileSplit fs = (FileSplit) context.getInputSplit();
Path grandpa = fs.getPath().getParent().getParent();
this.date = grandpa.getName();
if(this.date == null)
throw new RuntimeException("The input path is not right. No date field can be extracted!");
}