我试图逐行读取CSV文件,并将包含“India”的行的键值对作为子字符串发送。为此,我开发了下面的代码。为什么映射器会抛出ArrayIndexoutofboundexception?
映射代码
import java.io.IOException;
import org.apache.hadoop.io.IntWritable;
import org.apache.hadoop.io.LongWritable;
import org.apache.hadoop.io.Text;
import org.apache.hadoop.mapreduce.Mapper;
public class MapperCode extends Mapper<LongWritable,Text,Text,IntWritable> {
public void map(LongWritable key,Text value,Context context) throws IOException,InterruptedException
{
String Line=value.toString();
String out="";
String search_line=Line;
String splitter[]=Line.split(" ");
String searchStr="india";
for(String words:splitter)
{
if(searchStr.equals(words))
{
out=out+"\n"+search_line;
System.out.println(out);
}
}
String keyvalpair[]=out.split(",");
context.write(new Text(keyvalpair[2].trim()), new IntWritable(Integer.parseInt(keyvalpair[9].trim())));
}
}
数据集
Clarissa Chun,30,United States,2012,08-12-2012,Wrestling,0,0,1,1
Yogeshwar Dutt,29,India,2012,08-12-2012,Wrestling,0,0,1,1
Jaime Espinal,27,Puerto Rico,2012,08-12-2012,Wrestling,0,1,0,1
Johan Eurén,27,Sweden,2012,08-12-2012,Wrestling,0,0,1,1
Karam Gaber,32,Egypt,2012,08-12-2012,Wrestling,0,1,0,1
异常
17/03/17 21:11:08 INFO mapred.JobClient: Task Id : attempt_201703140915_0030_m_000000_1, Status : FAILED
java.lang.ArrayIndexOutOfBoundsException: 2
at MapperCode.map(MapperCode.java:26)
at MapperCode.map(MapperCode.java:1)
at org.apache.hadoop.mapreduce.Mapper.run(Mapper.java:144)
at org.apache.hadoop.mapred.MapTask.runNewMapper(MapTask.java:647)
at org.apache.hadoop.mapred.MapTask.run(MapTask.java:323)
at org.apache.hadoop.mapred.Child$4.run(Child.java:270)
at java.security.AccessController.doPrivileged(Native Method)
at javax.security.auth.Subject.doAs(Subject.java:396)
at org.apache.hadoop.security.UserGroupInformation.doAs(UserGroupInformation.java:1127)
at org.apache.hadoop.mapred.Child.main(Child.java:264)
请帮助我。提前致谢!
是的,我同意你的意见。当我用“,”而不是“”(用于分割线)尝试时。这又是一个例外。 @dbustosp – user3928562
@ user3928562检查更新。 – dbustosp