Hadoop多输入文件错误

Hadoop多输入文件错误,hadoop,mapreduce,Hadoop,Mapreduce,我试图用下面的代码从hdfs输入中读取2个文件,但我遇到如下错误 我是mapreduce编程的初学者,在这个问题上坚持了几天,任何帮助都将不胜感激。 我的代码: import java.io.IOException; 导入org.apache.hadoop.conf.Configuration; 导入org.apache.hadoop.fs.Path; 导入org.apache.hadoop.io.IntWritable; 导入org.apache.hadoop.io.Text; 导入org.


我试图用下面的代码从hdfs输入中读取2个文件,但我遇到如下错误 我是mapreduce编程的初学者,在这个问题上坚持了几天,任何帮助都将不胜感激。
我的代码:

import java.io.IOException;
导入org.apache.hadoop.conf.Configuration;
导入org.apache.hadoop.fs.Path;
导入org.apache.hadoop.io.IntWritable;
导入org.apache.hadoop.io.Text;
导入org.apache.hadoop.mapred.FileSplit;
导入org.apache.hadoop.mapreduce.Job;
导入org.apache.hadoop.mapreduce.Mapper;
导入org.apache.hadoop.mapreduce.Reducer;
导入org.apache.hadoop.mapreduce.lib.input.FileInputFormat;
导入org.apache.hadoop.mapreduce.lib.output.FileOutputFormat;
导入org.apache.hadoop.util.GenericOptionsParser;
导入org.apache.hadoop.mapreduce.lib.input.MultipleInputs;
导入org.apache.hadoop.mapreduce.lib.input.TextInputFormat;
公共课食谱{
公共静态类TokenizerMapper1
扩展映射器{
私有最终静态IntWritable one=新的IntWritable(1);
私有文本字=新文本();
公共无效映射(对象键、文本值、上下文
)抛出IOException、InterruptedException{
字符串行=value.toString();
字集(行子串(2,8));
上下文。写(单词,一);
}
}
公共静态类TokenizerMapper2
扩展映射器{
私有最终静态IntWritable one=新的IntWritable(1);
私有文本字=新文本();
公共无效映射(对象键、文本值、上下文
)抛出IOException、InterruptedException{
字符串行=value.toString();
字集(行子串(2,8));
上下文。写(单词,一);
}
}
公共静态类IntSumReducer
伸缩减速机{
私有IntWritable结果=新的IntWritable();
public void reduce(文本键、Iterable值、,
语境
)抛出IOException、InterruptedException{
整数和=0;
for(可写入值:值){
sum+=val.get();
}
结果集(总和);
编写(键、结果);
}
}
公共静态void main(字符串[]args)引发异常{
Configuration conf=新配置();
String[]otherArgs=新的GenericOptionsParser(conf,args);
if(otherArgs.length!=2){
System.err.println(“用法:配方”);
系统出口(2);
}
@抑制警告(“弃用”)
作业作业=新作业(配置,“配方”);
job.setJarByClass(Recipe.class);
setMapperClass(TokenizerMapper1.class);
setMapperClass(TokenizerMapper2.class);
job.setCombinerClass(IntSumReducer.class);
job.setReducerClass(IntSumReducer.class);
job.setOutputKeyClass(Text.class);
job.setOutputValueClass(IntWritable.class);
MultipleInputs.addInputPath(作业,新路径(args[0]),TextInputFormat.class,TokenizerMapper1.class);
MultipleInputs.addInputPath(作业,新路径(args[1]),TextInputFormat.class,TokenizerMapper2.class);
setOutputPath(作业,新路径(args[2]);
//FileInputFormat.addInputPath(作业,新路径(“hdfs://localhost:9000/in"));
//FileOutputFormat.setOutputPath(作业,新路径(“hdfs://127.0.0.1:9000/out"));
系统退出(作业等待完成(真)?0:1;
//job.submit();
}
我设置了如下程序运行配置参数:
/输入/输入




错误:

线程“main”java.lang.ArrayIndexOutOfBoundsException中出现异常异常:2 位于Recipe.main(Recipe.java:121)


有几个问题。程序需要3个参数,而您只传递2个。此外,如果您必须处理多个输入格式,则需要使用多个输入

假设您调用程序/in1/in2/out

    MultipleInputs.addInputPath(job, args[0], TokenizerMapper1.class, FirstMapper.class);
    MultipleInputs.addInputPath(job, args[1], TokenizerMapper2.class, SecondMapper.class);
您可以从代码中删除以下行:

job.setMapperClass(TokenizerMapper1.class);
job.setMapperClass(TokenizerMapper2.class);   

现在,它可以进行以下修改:

  • 将每个文件放在单独的目录中
  • 使用实地址而不是arg[],如下所示:

    MultipleInputs.addInputPath(job,new Path("hdfs://localhost:9000/in1"),TextInputFormat.class,TokenizerMapper1.class);
    MultipleInputs.addInputPath(job,new Path("hdfs://localhost:9000/in2"),TextInputFormat.class,TokenizerMapper1.class);
    FileOutputFormat.setOutputPath(job, new Path("hdfs://127.0.0.1:9000/out"));
    
  • 在运行配置\参数中指定所有输入和输出路径,如下所示:

    127.0.0.1:9000/in1/DNAIn.txt 127.0.0.1:9000/in2/DNAIn2.txt 127.0.0.1:9000/out
    

  • 确保此处的参数长度为2:
    如果(otherArgs.length!=2)
    ,请尝试在此处检索args数组中的第三个索引:
    FileOutputFormat.setOutputPath(作业,新路径(args[2]))
    。因此,例外情况是您需要输入2个文件。您可以找到一个例子,otherArgs.length等于2,然后我将此行更改为:FileOutputFormat.setOutputPath(作业,新路径(args[2]);改为:FileOutputFormat.setOutputPath(作业,新路径()hdfs://127.0.0.1:9000/out"));但我还有一个错误:1-我无法理解为什么必须在输入文件所在的目录中有2个:in1,in2-在这种情况下,两个文件都有文本格式,我通过以下行指定:MultipleInputs.addInputPath(作业,新路径(arg[0]),TextInputFormat.class,TokenizerMapper2.class);3-关于3个参数,你能解释一下吗?我只是复制了这些代码,我不知道到底发生了什么
    127.0.0.1:9000/in1/DNAIn.txt 127.0.0.1:9000/in2/DNAIn2.txt 127.0.0.1:9000/out