解读:MR多路径输入

对于在一个MR-Job中使用多路径作为输入文件,一般有三种方法:

1).多次调用,加载不同路径:

import org.apache.hadoop.mapreduce.lib.input.FileInputFormat;
import org.apache.hadoop.mapreduce.lib.output.FileOutputFormat;

//输入路径in01 in02
String in01 = "hdfs://RS5-112:9000/cs01/path01";
String in02 = "hdfs://RS5-112:9000/cs02/path02";

//多次调用addInputPath()方法
FileInputFormat.addInputPath(job,new Path(in0));
FileInputFormat.addInputPath(job,new Path(in1));

2).一次调用,同时加载多路径(字符串用逗号隔开):

//这种方式的第二个参数只能是:将路径以逗号拼接的字符串
FileInputFormat.addInputPaths(job,
    "hdfs://RS5-112:9000/cs01/path1,hdfs://RS5-112:9000/cs02/path2");

3).使用MultipleInputs类的方法

addInputPath(Job job, Path path,
      Class<? extends InputFormat> inputFormatClass);
addInputPath(Job job, Path path,
  Class<? extends InputFormat> inputFormatClass,
  Class<? extends Mapper> mapperClass);

MultipleInputs类的强大之处在于不仅可以多次调用addInputPath()方法加载路径,而且可以根据路径的不同指定不同的输入格式,更有甚者还可以根据输入格式的不同指定不同的Mapper函数进行处理。详见 MR案例:倒排索引 && MultipleInputs 和 MR案例:CombineFileInputFormat

DEMO1:

MultipleInputs.addInputPath(job, 
        new Path("hdfs://RS5-112:9000/cs01/path01"), 
        TextInputFormat.class);
MultipleInputs.addInputPath(job, 
        new Path("hdfs://RS5-112:9000/cs02/path2"), 
        KeyValueInputFormat.class);

DEMO2:

MultipleInputs.addInputPath(job, 
        new Path("hdfs://RS5-112:9000/cs01/path01"), 
        TextInputFormat.class,
        Mapper01.class);
MultipleInputs.addInputPath(job, 
        new Path("hdfs://RS5-112:9000/cs02/path2"), 
        KeyValueInputFormat.class,
        Mapper02.class);

 

posted @ 2015-08-24 02:00  skyl夜  阅读(1347)  评论(0编辑  收藏  举报