gpt4 book ai didi

hadoop - 为什么 mapper 会抛出 ArrayIndexoutofboundexception?

转载 作者:可可西里 更新时间:2023-11-01 15:01:14 26 4
gpt4 key购买 nike

我正在尝试逐行读取 CSV 文件,并发送包含“India”作为子字符串的那些行的键值对。为此,我开发了以下代码。

映射器代码

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)

请帮帮我。提前致谢!

最佳答案

问题就出在这里。

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

您正在尝试使用“空格”进行拆分。在这种特殊情况下,您可以使用“,”

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

这应该可以正常工作。

关于hadoop - 为什么 mapper 会抛出 ArrayIndexoutofboundexception?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/42870541/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com