MapReduce获取输入文件路径(全)

对于MapReduce而言,在Map端经常需要知道处理文件的输入路径,以此来区分不同的处理方式。

我们知道在MapReduce框架中会将输入的文件切分成许多(InputSplit)文件块,每个文件块包含了文件路径,起止偏移量等信息,每一个文件块交给一个map任务进行处理。而文件块的生成是通过InputFormat调用getSplits方法实现的,不同的InputFormat会有不同的切分规则,生成不同类型的文件块。

因此不同的InputFormat采用不同的获取文件路径的方法:

1、一般FileInputFormat

一般的FileInputFormat是MapReduce中最常用的一种输入格式,包括TextInputFormat,ParquetInputFormat,SequenceFileInputFormat等,这些输入格式切分的文件块类型为FileSplit。

因此获取文件路径方法为:

@Override
public void map(LongWritable key, Text value, Context context)
				         throws IOException, InterruptedException{

    InputSplit split = context.getInputSplit();
    FileSplit fileSplit = (FileSplit) split;
    String filePath = fileSplit.getPath().toUri().getPath();
    ......
			 
}

2、CombineFileInputFormat

CombineFileInputFormat输入格式实现的是小文件合并输入,使用场景是输入文件中有很多小文件,导致会产生很多的map任务增大了系统的调度、通信开销。需要将许多小文件合并成一个map任务进行处理,CombineFileInputFormat详细使用情况会专门介绍,这里就不展开了。此输入格式切分的文件块类型为CombineFileSplit。由于CombineFileSplit包含多个要处理文件的信息,所以通过CombineFileSplit并不知道当前处理的数据来自哪份文件。不用担心,在map任务读取文件块的时候,将数据所在的文件信息在上下文context中进行了保存,获取方法如下:

@Override
public void map(LongWritable key, Text value, Context context)
				         throws IOException, InterruptedException{

    String filePath = context.getConfiguration().get("mapreduce.map.input.file");
    
    ......
			 
}

3、MultipleInputs(DelegatingInputFormat)

MultipleInputs多文件输入:对多种输入类型的需要编写不同的map函数进行处理,比如同时处理hdfs上的一般文本数据,Hbase数据,mysql数据等,此时就需要用到这种输入方式。

//普通文件
MultipleInputs.addInputPath(job, TxtInputPath, TextInputFormat.class,TextMapper.class);
//Hbase,hbaseInputPath无意义且没有用到,但是是必传的,可以任意指定
MultipleInputs.addInputPath(job, hbaseInputPath, TableInputFormat.class, TableMap.class);

其指定的输入格式为DelegatingInputFormat,但是每份数处理用的还是上面指定的输入格式, 切分出的文件块类型也是根据上面指定的输入格式确定,被封装在TaggedInputSplit类中,这个类访问修饰符是default的,定义如下:

class TaggedInputSplit extends InputSplit implements Configurable, Writable

这样我们就不能直接使用这个类,此时需要用反射的机制来获取输入文件路径:

对于这种格式输入获取的是一般文本数据路径(Hbase,mysql没有文件路径的概念)。

@Override
public void map(LongWritable key, Text value, Context context)
				         throws IOException, InterruptedException{
    String filePath = null;
    InputSplit split = context.getInputSplit();
    if (split.getClass().getName().equals("org.apache.hadoop.mapreduce.lib.input.TaggedInputSplit")) {
        Method getInputSplitMethod = split.getClass().getDeclaredMethod("getInputSplit");
		// 设置访问权限 true:不需要访问权限检测直接使用 false:需要访问权限检测
		getInputSplitMethod.setAccessible(true);
		InputSplit realSplit = (InputSplit) getInputSplitMethod.invoke(split);
        if (realSplit instanceof FileSplit) {
            FileSplit fileSplit = (FileSplit) realSplit;
            filePath = fileSplit.getPath().toUri().getPath();
        } else if (realSplit instanceof CombineFileSplit) {
            filePath = context.getConfiguration().get("mapreduce.map.input.file");
        }

  }
    
    ......
			 
}

spark获取文件路径我的另一篇博文https://blog.csdn.net/zwlll19900607/article/details/103482312

  • 2
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值