我試圖逐行讀取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