2017-03-18 67 views
1

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

請幫助我。提前致謝!

回答

2

只是因爲你試圖訪問一個不是這個大小的數組中的索引。讓我跟蹤一下。

1- Clarissa Chun,30,United States,2012,08-12-2012,Wrestling,0,0,1,1 
    2- splitter = ["Clarissa", "Chun,30,United", "States,2012,08-12-2012,Wrestling,0,0,1,1] 
    3- keyvalpair = ["Clarissa"] 
    4. keyvalpair[2] ==> ???? 

你明白了嗎?希望這可以幫助。

針對特定的目標,嘗試一些更簡單,你只需要做到:

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(); 
     if(line.contains("india")) { 
      String keyvalpair[] = line.split(","); 
      context.write(new Text(keyvalpair[2].trim()), new IntWritable(Integer.parseInt(line))); 
     } 
    } 
} 
+0

是的,我同意你的意見。當我用「,」而不是「」(用於分割線)嘗試時。這又是一個例外。 @dbustosp – user3928562

+0

@ user3928562檢查更新。 – dbustosp

2

問題就出在這裏。

String splitter[]=Line.split(" "); 

您正在嘗試使用 'space'.In你coulde使用本perticular案' 分裂」

String splitter[]=Line.split(","); 

這應該很好地工作。

+0

是的,我嘗試過這樣也沒有運氣。 – user3928562

相關問題