我试图将输入值分成3部分,并将每个部分分配给一个单独的字符串,并对其执行一些操作。但我正在获取arrayindexoutofbound异常,我无法找出原因。
Map器:
public void map (Object Key, Text value,Context context )throws IOException,InterruptedException{
String text=value.toString();
String date =null;
String parts[]=tweet.split("\\t");
String sentence= parts[0].toString();
for(int i=0;i<parts.length;i++) {
System.out.println("part "+i+parts[i]);
}
if(parts.length>0){
date=parts[1];
}
word.set(date);
context.write(word, one);
}
堆栈跟踪:
2015-07-31 16:50:50,288 INFO [Thread-11] mapred.LocalJobRunner (LocalJobRunner.java:run(397)) - Map task executor complete.
2015-07-31 16:50:50,295 WARN [Thread-11] mapred.LocalJobRunner (LocalJobRunner.java:run(482)) - job_local467783972_0001
java.lang.Exception: java.lang.ArrayIndexOutOfBoundsException: 1
at org.apache.hadoop.mapred.LocalJobRunner$Job.run(LocalJobRunner.java:403)
Caused by: java.lang.ArrayIndexOutOfBoundsException: 1
*at sw$TweetMapper.map(sw.java:103)* --> points to date=parts[1]
at sw$TweetMapper.map(sw.java:1)
at org.apache.hadoop.mapreduce.Mapper.run(Mapper.java:145)
at org.apache.hadoop.mapred.MapTask.runNewMapper(MapTask.java:763)
at org.apache.hadoop.mapred.MapTask.run(MapTask.java:339)
at org.apache.hadoop.mapred.LocalJobRunner$Job$MapTaskRunnable.run(LocalJobRunner.java:235)
at java.util.concurrent.Executors$RunnableAdapter.call(Executors.java:511)
at java.util.concurrent.FutureTask.run(FutureTask.java:266)
at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1142)
at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:617)
at java.lang.Thread.run(Thread.java:745)
零件[]长度大于0,但即使如此,赋值也会导致错误。如有任何帮助,将不胜感激。
1条答案
按热度按时间i86rm4rw1#
我认为你应该用
\t
对于split中的tab文本,而不是\\t
,因为这将尝试在实际字符串上拆分"\t"
. 因此:if(parts.length > 1) {
date = parts[1];
}