如何在Map-Reduce中从多个目录中读取多个文件
问题描述:
我想从Map-Reduce程序中的多个目录中读取多个文件。 我曾试图给出的文件名中的主要方法:如何在Map-Reduce中从多个目录中读取多个文件
FileInputFormat.setInputPaths(conf,new Path("hdfs://localhost:54310/user/test/"));
FileInputFormat.setInputPaths(conf,new Path("hdfs://localhost:54310/Test/test1/"));
但仅从一个文件中读取。
我该怎么做才能读取多个文件?
请提出解决方案。
谢谢。
答
FileInputFormat#setInputPaths
将在覆盖之前设置的输入路径之后设置输入路径。使用FileInputFormat#addInputPath
或FileInputFormat#addInputPaths
添加到现有路径。
答
Follow the below steps for passsing multiple input files from different direcories.Just driver code changes.Follow the below driver code.
CODE:
public int run(String[] args) throws Exception {
Configuration conf=new Configuration();
Job job=Job.getInstance(conf, "MultipleDirectoryAsInput");
job.setMapperClass(Map1Class.class);
job.setMapperClass(Map2Class.class);
job.setReducerClass(ReducerClass.class);
job.setJarByClass(DriverClass.class);
job.setMapOutputKeyClass(Text.class);
job.setMapOutputValueClass(IntWritable.class);
job.setOutputKeyClass(Text.class);
job.setOutputValueClass(NullWritable.class);
//FileInputFormat.setInputPaths(job, new Path(args[0]));
MultipleInputs.addInputPath(job, new Path(args[0]),TextInputFormat.class,Map1Class.class);
MultipleInputs.addInputPath(job, new Path(args[1]), TextInputFormat.class, Map2Class.class);
FileOutputFormat.setOutputPath(job, new Path(args[2]));
return job.waitForCompletion(true)?0:1;
}