2017-02-12 1 views
1

음수가 아닌 문자를 기반으로 단어로 레코드를 분할하려고 시도 할 때 각 단어의 첫 번째 문자를 계산하고 각 단어의 첫 번째 알파벳의 전체 항목을 가져옵니다. 아래는 실행하려고했던 Mapper 클래스 로직입니다.내 MapReduce 코드의 StringIndexOutOfBoundsException

public void map(LongWritable key, Text value, Context ctx) { 
    String line = value.toString(); 
    String[] split = line.split("\\W+"); 
    String firstChar; 
    for(String words: split) { 
     firstChar = String.valueOf(words.charAt(0)); 
     try { 
      ctx.write(new Text(firstChar), new IntWritable(1)); 
     } catch (IOException | InterruptedException e) { 
      e.printStackTrace(); 
     } 
    } 
} 

예외 :

이 은 그러나 임 라인에서이 논리에 대한 StringIndexOutOfBounds 예외를 받고

Error: java.lang.StringIndexOutOfBoundsException: String index out of range: 0 
    at java.lang.String.charAt(String.java:658) 
    at com.hadoopexp.mapper.MapperClass.map(MapperClass.java:17) 
    at com.hadoopexp.mapper.MapperClass.map(MapperClass.java:1) 
: I는 입력 파일에 일부 빈 줄을 넣어 가지고

firstChar = String.valueOf(words.charAt(0)); 

단지 있는지에 공장. (아래처럼)

Liverpool 
Manchester 


London 

Toronto ? ?? !!12 32 

논리를 수정하는 방법에 대해 도움을 줄 수있는 사람이 있습니까? 어떤 도움이라도 대단히 감사합니다.

답변

0

빈 문자열을 나누는 것은 빈 문자열의 단일 요소를 포함하는 배열을 반환합니다. 그냥 명시 적으로 확인해 보겠습니다.

for(String words: split) { 
    if (!words.isEmpty()) { // Here! 
     firstChar = String.valueOf(words.charAt(0)); 
     try { 
      ctx.write(new Text(firstChar), new IntWritable(1)); 
     } catch (IOException | InterruptedException e) { 
      e.printStackTrace(); 
     } 
    } 
} 
+0

감사합니다. Mureinik. 그것은 효과가있다. – Sidhartha

관련 문제