【问题标题】:Error: java.lang.NumberFormatException : empty string map-reduce code错误:java.lang.NumberFormatException:空字符串 map-reduce 代码
【发布时间】:2017-06-26 10:31:51
【问题描述】:

这是我的映射器类代码:

package airlinedataanalysis;

import java.io.IOException;

import org.apache.hadoop.io.*;
import org.apache.hadoop.mapreduce.Mapper;

public class AirlineMapperClass extends Mapper<LongWritable, Text, Text, FloatWritable>
{
     public void map(LongWritable key, Text PassengerDetails, Context con)throws IOException, InterruptedException
     {
              String[] detail = PassengerDetails.toString().split(",");
              Integer survived=Integer.parseInt(detail[1]);
              if(survived==1)
              {
                  String sex = detail[4];

                  if(detail[5]==null)
                  { 
                      con.write(new Text(sex), new FloatWritable((float) 0));
                  }         
                  else
                  {   
                    Float age=Float.parseFloat(detail[5]);
                    con.write(new Text(sex), new FloatWritable(age));

                  }



              }

     }

}

这是错误

当数据输入集没有空值时,我的代码可以正常工作。 但是当输入集包含空值时它不起作用。

数据输入文件如下所示:

1,0,3,"Braund Mr. Owen Harris",male,22,1,0,A/5 21171,7.25,,S,
2,1,1,"Cumings Mrs. John Bradley (Florence Briggs Thayer)",female,38,1,0,PC 17599,71.2833,C85,C,
3,1,3,"Heikkinen Miss. Laina",female,26,0,0,STON/O2. 3101282,7.925,,S,
4,1,1,"Futrelle Mrs. Jacques Heath (Lily May Peel)",female,35,1,0,113803,53.1,C123,S,
5,0,3,"Allen Mr. William Henry",male,35,0,0,373450,8.05,,S,
6,0,3,"Moran Mr. James",male,,0,0,330877,8.4583,,Q,
7,0,1,"McCarthy Mr. Timothy J",male,54,0,0,17463,51.8625,E46,S,
8,0,3,"Palsson Master. Gosta Leonard",male,2,3,1,349909,21.075,,S,
9,1,3,"Johnson Mrs. Oscar W (Elisabeth Vilhelmina Berg)",female,27,0,2,347742,11.1333,,S,
10,1,2,"Nasser Mrs. Nicholas (Adele Achem)",female,14,1,0,237736,30.0708,,C,
11,1,3,"Sandstrom Miss. Marguerite Rut",female,4,1,1,PP 9549,16.7,G6,S,

我想要的输出:

female  Total Died: 9 :: Average Age:30.222221
male    Total Died: 2 :: Average Age:31.0

我的reducer类代码:

package airlinedataanalysis;

import java.io.IOException;

import org.apache.hadoop.io.*;
import org.apache.hadoop.mapreduce.Reducer;

public class AirlineReducer extends Reducer<Text, FloatWritable, Text, Text> 
{
    public void reduce(Text key, Iterable<FloatWritable> value,Context con) throws IOException, InterruptedException
    {
               Float totalage =(float) 0;
               int count = 0;
               for (FloatWritable var : value) 
               {
                totalage += var.get();
                count++;
               }
               float avg =(totalage / count);
               String out = "Total Died: " + count + " :: " + "Average Age:" + avg;
               con.write(key, new Text(out));
    }          
}

【问题讨论】:

  • 好吧,首先,您的第二行将评估为survived,而detail[5](38) 不是Float。你会遇到很多问题。
  • 但它适用于非空值
  • 它会告诉你它在哪里,java.lang.Float.parseFloat(Float.java:452)。去看看第 452 行。
  • 如果我自己知道如何处理这个错误,那么我就不会寻求帮助了
  • 看看这个接受的答案。 stackoverflow.com/questions/31642352/…。为了能够接受浮点数或积分,您可以将它们作为双精度数拉入。

标签: java numberformatexception


【解决方案1】:

您可以在映射器类中处理空字符串/Null 条件,如下所示:

public void map(Object key, Text PassengerDetails, Context con)
            throws IOException, InterruptedException {

        String[] detail = PassengerDetails.toString().split(",");

        if (!detail[1].isEmpty() && !detail[1].equals(null)) {

            Integer survived = Integer.parseInt(detail[1]);

            if (survived != null && survived == 1) {

                String sex = "Undefined";

                if (!detail[4].equals(null)) {
                    sex = detail[4];
                }

                if (detail[5].isEmpty() || detail[5] == null) {
                    con.write(new Text(sex), new FloatWritable((int) 0));
                } else {
                    float age = Float.parseFloat(detail[5]);
                    con.write(new Text(sex), new FloatWritable(age));

                }

            }
        }
    }

它会起作用的。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2013-06-27
    • 1970-01-01
    • 2015-07-12
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多