我在Map程序中获得异常的Array索引绑定。下面是数据和mapreduce程序。
数据:
1,拉加,10,10000
2,jyo,10,10000
3,TEJ,11,20000
4,tej1,11,20000
MapReduce计划:
public static class EmployMap extends Mapper<LongWritable, Text, Text, IntWritable>
{
String dNname;
public void map(LongWritable k,Text v,Context con) throws IOException, InterruptedException{
String text=v.toString();
String[] textArry=text.split(",");
System.out.println(textArry.length);
int dNo=Integer.parseInt(textArry[2]);
int sal=Integer.parseInt(textArry[3]);
if(dNo==10){
dNname="Automation";
}else{
dNname="Manual";
}
con.write(new Text(dNname), new IntWritable(sal));
}
}
public static class EmployReduce extends Reducer<Text, IntWritable, Text, IntWritable>{
int totalSal;
public void reduce(Text k, Iterable<IntWritable> v,Context con) throws IOException, InterruptedException{
for(IntWritable val:v){
totalSal+=val.get();
}
con.write(k, new IntWritable(totalSal));
}
}
public static void main(String[] args) throws IOException, ClassNotFoundException, InterruptedException {
Configuration conf=new Configuration();
Path input=new Path(args[0]);
Path output=new Path(args[1]);
Job job=Job.getInstance(conf);
job.setJarByClass(Employ.class);
job.setMapperClass(EmployMap.class);
job.setReducerClass(EmployReduce.class);
job.setOutputKeyClass(Text.class);
job.setOutputValueClass(IntWritable.class);
FileInputFormat.addInputPath(job, input);
FileOutputFormat.setOutputPath(job, output);
System.exit(job.waitForCompletion(true) ? 0:1);
}
}
错误记录
Error: java.lang.ArrayIndexOutOfBoundsException: 2
at Employ$EmployMap.map(Employ.java:21)
at Employ$EmployMap.map(Employ.java:1)
at org.apache.hadoop.mapreduce.Mapper.run(Mapper.java:146)
at org.apache.hadoop.mapred.MapTask.runNewMapper(MapTask.java:787)
at org.apache.hadoop.mapred.MapTask.run(MapTask.java:341)
at org.apache.hadoop.mapred.YarnChild$2.run(YarnChild.java:163)
at java.security.AccessController.doPrivileged(Native Method)
at javax.security.auth.Subject.doAs(Subject.java:422)
at org.apache.hadoop.security.UserGroupInformation.doAs(UserGroupInformation.java:1657)
at org.apache.hadoop.mapred.YarnChild.main(YarnChild.java:158)
错误在第21行,即&#34; int dNo = Integer.parseInt(textArry [2]);&#34;有人可以帮我理解代码中的错误吗?
答案 0 :(得分:0)
检查您的数据集。您正在拆分数据,并且逗号之后在列之间有额外的空格删除数据之间的空格并运行它。我希望这有效。