java - 错误 : java. lang.NumberFormatException:对于输入字符串: "100"

标签 java hadoop mapreduce

我是 hadoop 编程的新手,在尝试 reduce side join 时出现错误错误:java.lang.NumberFormatException:对于输入字符串:“100”。 我已经检查了输入文件是否正常。

我的reducer代码是`

public void reduce (IntWritable key , Iterable<Text> value , Context context) throws IOException , InterruptedException{

    // value : TRAN\t product name \t Amt and CUST \t custometr name
    String data = new String();
    int amount=0;
    String name =null;
    StringBuilder s = new StringBuilder();
    for (Text val : value){

        String[] line = val.toString().split("\t");
        if (line[0].equals(new String("TRANS").trim())){
            //data += line[1]+"\t";
            s.append(line[1]+"");
            amount+=Integer.parseInt(line[2]);
        }
        else if(line[0].equals(new String("CUST").trim())){

            name = line[1];


        }

        data= s.toString()+ Integer.toString(amount);
context.write(new Text(name), new Text(data));

我的两个映射器是:

package reduceSideJoin.reducejoin;

public class transMapper extends Mapper<LongWritable ,Text , IntWritable , Text > {

public void map(LongWritable key , Text value , Context context) throws IOException , InterruptedException{

    String[] line = value.toString().split(",");
    String data = "TRANS"+ "\t" + line[1]+"\t " + line[3];
    context.write(new IntWritable (Integer.parseInt(line[0])), new Text(data) );

    }

}

package reduceSideJoin.reducejoin;

public class userMapper extends Mapper<LongWritable, Text, IntWritable, Text> {
public void map (LongWritable key, Text value , Context context) throws IOException , InterruptedException{

    String [] line = value.toString().split(",");
    String data = "CUST"+"\t"+line[1];
    context.write(new IntWritable (Integer.parseInt(line[0])), new Text(data));

    }

} 

我的文件是

交易数据

0001,crax,2,300
0002,munch,1,10
0003,lays,1,20
0004,ship,1,200
0005,barOne,3,400
0002,crax,2,300
0001,kurkure,3,100
0003,milk,1,20
0004,butter,2,300
0005,meat,1,1220
0002,color,1,230
0003,dailrymilk,1,20

客户数据是

0001,Sunil Kumar , Mumbai,India
0002,Vikas mandal, Haryana, India
0003,Karan, JFK,France
0004,manish,banglore,India
0005,devesh,meerut,India

我在 16/05/30 00:23:00 收到错误信息 mapreduce.Job: map 100% reduce 0% 16/05/30 00:23:04 INFO mapreduce.Job:任务 ID:attempt_1464547777880_0001_r_000000_0,状态:失败 错误:java.lang.NumberFormatException:对于输入字符串:“100” 在 java.lang.NumberFormatException.forInputString(NumberFormatException.java:65)

谁能帮我解决这个问题

谢谢

最佳答案

NumberFormatException 是因为:

下面一行中有一个额外的空格(就在最后一个“\t”之后):

String data = "TRANS"+ "\t" + line[1]+"\t " + line[3];

您应该替换为:

String data = "TRANS"+ "\t" + line[1]+"\t" + line[3];

在那之后,您仍然会在行中收到 NullPointerException:

context.write(new Text(name), new Text(data));

那是因为您只在“else” block 中设置变量 name

你可以通过这样设置 name 来解决这个问题:

if (line[0].equals(new String("TRANS").trim())){
    name = line[1]; // <==== here
    s.append(line[1]+"");
    amount+=Integer.parseInt(line[2]);
}

关于java - 错误 : java. lang.NumberFormatException:对于输入字符串: "100",我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/37513769/

相关文章:

hadoop - Hadoop-需要帮助以了解处理步骤

scala - 使用两个条件过滤列表并创建 map scala

java - ActionListener 与 AbstractAction

java - 在按键时显示图像并将其保留在屏幕上直到线程停止

hadoop - 什么是无状态节点? Hadoop 节点如何是无状态的?

java - 与 tez0.5.2 集成时在配置单元 1.0.0 中执行插入操作时出错

hadoop - Spark : How to start remotely Jupyter in 'yarn_client' mode from a different user

java - Eclipse 的 Jess 插件无法工作?并且未安装

java - 将JButton添加到JPanel中按JButton问题

java - Mongo hadoop mapreduce 显示错误