【问题标题】:Remove Empty bags in Pig移除 Pig 中的空袋
【发布时间】:2018-01-26 04:46:01
【问题描述】:

我有这样的数据。

(a,b,c,d)
(g,b,v,n)
(n,h,l,o)
(,,,)
(,,,)
(,,,)
(,,,)

我想取出空袋子。 想要的输出

(a,b,c,d)
(g,b,v,n)
(n,h,l,o)

【问题讨论】:

  • 得到了答案。 Not Null 在这里不起作用。检查每列的大小,如果大小不为零,则只考虑那些记录。
  • 根据问题描述,我假设按 null 过滤会起作用。无论如何,我更新了答案以包括按列大小过滤,以便遇到相同问题的任何人都可以从中受益。干杯!

标签: hadoop apache-pig hadoop2


【解决方案1】:

如果你能给出你目前拥有的代码,那就更好了。

方法一:按空值过滤

-- load comma deliited values into columns
A = load './input.txt' using PigStorage(',') as (one:chararray, two:chararray, three:chararray, four:chararray);
dump A;

-- remove records where columns are null
B = FILTER A BY (one is not null) OR (two is not null) OR (three is not null) OR (four is not null);
dump B;

这里假设 input.txt 如下。

a,b,c,d
g,b,v,n
n,h,l,o
,,,
,,,
,,,
,,,

运行命令:

pig -x local clean.pig

输出第一个转储:

(a,b,c,d)
(g,b,v,n)
(n,h,l,o)
(,,,)
(,,,)
(,,,)
(,,,)

输出第二次转储:

(a,b,c,d)
(g,b,v,n)
(n,h,l,o)

方法二:按列大小过滤

-- load comma deliited values into columns
A = load './input.txt' using PigStorage(',') as (one:chararray, two:chararray, three:chararray, four:chararray);
dump A;

-- generate column count
B = FOREACH A GENERATE COUNT(TOBAG(*)),$0..;
dump B;

-- filter by column count
C = FILTER B BY $0 > 0;
dump C;

-- remove column count
D = FOREACH C GENERATE $1..;
dump D;

转储 A 的输出:

(a,b,c,d)
(g,b,v,n)
(n,h,l,o)
(,,,)
(,,,)
(,,,)
(,,,)

转储 B 的输出:

(4,a,b,c,d)
(4,g,b,v,n)
(4,n,h,l,o)
(0,,,,)
(0,,,,)
(0,,,,)
(0,,,,)

转储 C 的输出:

(4,a,b,c,d)
(4,g,b,v,n)
(4,n,h,l,o)

转储 D 的输出:

(a,b,c,d)
(g,b,v,n)
(n,h,l,o)

附注:

如果您的输入文件最初有括号,您可能需要单独处理。

【讨论】:

    猜你喜欢
    • 2014-06-18
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2016-09-08
    • 1970-01-01
    相关资源
    最近更新 更多