Reputation: 2593
I am new to hadoop programming and while trying reduce side join i got error Error: java.lang.NumberFormatException: For input string: " 100". I have checked the input file are Ok .
My reducer Code is `
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));
and my two mapper are :
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) );
}
}
and
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));
}
}
and my files are
Transaction 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
and customer data is
0001,Sunil Kumar , Mumbai,India
0002,Vikas mandal, Haryana, India
0003,Karan, JFK,France
0004,manish,banglore,India
0005,devesh,meerut,India
and i got error 16/05/30 00:23:00 INFO mapreduce.Job: map 100% reduce 0% 16/05/30 00:23:04 INFO mapreduce.Job: Task Id : attempt_1464547777880_0001_r_000000_0, Status : FAILED Error: java.lang.NumberFormatException: For input string: " 100" at java.lang.NumberFormatException.forInputString(NumberFormatException.java:65)
Can someone help me to resolve this
Thanks
Upvotes: 0
Views: 6937
Reputation: 4096
The NumberFormatException is because:
There is an extra blank space in the line below (just after the last "\t"):
String data = "TRANS"+ "\t" + line[1]+"\t " + line[3];
You should replace with:
String data = "TRANS"+ "\t" + line[1]+"\t" + line[3];
After that, you will still receive a NullPointerException in the line:
context.write(new Text(name), new Text(data));
That's because you are settng the variable name
only in the "else" block.
You can solve this by setting the name
this way:
if (line[0].equals(new String("TRANS").trim())){
name = line[1]; // <==== here
s.append(line[1]+"");
amount+=Integer.parseInt(line[2]);
}
Upvotes: 2