如何优雅地处理pig中的空+数字加法

daolsyd0  于 2021-06-25  发布在  Pig
关注(0)|答案(1)|浏览(283)

我有一个数据模式,其中我有50+列。现在我有一个场景,需要将四个int列添加到一起。有可能四分之一的人都是空的。

if i do  null + 1 + null + 7 i get the result as null which is true as per given in the PIG 
https://pig.apache.org/docs/r0.7.0/piglatin_ref2.html#Nulls

i.e. if either sub-expression is null, the resulting expression is null.

有人能告诉我如何处理这种情况吗。我是否需要定义一个自定义项,或只是关心空,然后执行添加操作是好的。提前谢谢

bq9c1y66

bq9c1y661#

一个选项是,如果列值为null,则将列值设置为零,否则继续使用原始值。下面的示例。
输入文件

1,,3
,5,6
7,8,

Pig手稿:

A = LOAD 'input.txt' USING PigStorage(',') AS (f1:int,f2:int,f3:int);
B = FOREACH A GENERATE ((f1 is null)?0:f1) + ((f2 is null)?0:f2) +((f3 is null)?0:f3);
DUMP B;

输出:

(4)
(11)
(15)

相关问题