pig sum失败,值为+ve和-ve

yptwkmov  于 2021-05-31  发布在  Hadoop
关注(0)|答案(1)|浏览(377)

我有以下数据

primary,first,second
1,393440.09,354096.08
1,4410533.33,3969479.99
1,-4803973.41,-4323576.07

我得把第一栏和第二栏加起来。下面是我正在执行的脚本

data_load= load <filelocation> using org.apache.pig.piggybank.storage.CSVExcelStorage(',', 'NO_MULTILINE', 'NOCHANGE', 'SKIP_INPUT_HEADER') As  (primary:double, first:double,second:double)

   dataAgrr = group data_load by primary;

   sumData = FOREACH dataAgrr GENERATE 
   group as data,
   SUM(data_load.first) as first,
   SUM(data_load.second) as second,
   SUM(data_load.primary) as primary;

执行后,生成以下输出:

(1.0,0.009999999951105565,-5.820766091346741E-11,3.0)

但是当我们手动添加第二列(354096.083969479.99,-4323576.07)时,得到0。

kmbjn2e3

kmbjn2e31#

pig在内部使用java“double”。使用下面的示例代码进行测试

import java.math.BigDecimal;
public class TestSum  {
  public static void main(String[] args) {
    double d1 = 354096.08;
    double d2 = 3969479.99;
    double d3 = -4323576.07;
    System.err.println("Total in double is " + ((d3 + d2 ) + d1));

    BigDecimal bd1 = new BigDecimal("354096.08");
    BigDecimal bd2 = new BigDecimal("3969479.99");
    BigDecimal bd3 = new BigDecimal("-4323576.07");
    System.err.println("Total in BigDecimal is " + bd3.add(bd2).add(bd1));

  }
}

这就产生了

Total in double is -5.820766091346741E-11
Total in BigDecimal is 0.00

如果需要更高的精度,可以尝试在脚本中使用“bigdecimal”而不是“double”。

相关问题