【发布时间】:2014-08-12 21:03:03
【问题描述】:
我是 Pig-Latin 的初学者,我发现关于 FILTER 语句的问题。看例子:
假设我们有一个数据文件(test.txt),其内容是:
1,2,3
2,3,4
3,4,5
4,5,6
我想选择第一个字段为“3”的记录。 Pig 脚本是:
t = LOAD 'test.txt' USING PigStorage(',');
t1 = FOREACH t GENERATE $0 AS i0:chararray, $1 AS i1:chararray, $2 AS i2:chararray;
f1 = FILTER t1 BY i0 == '3';
DUMP f1
任务运行良好,但输出结果什么都没有。解释 f1 显示:
#--------------------------------------------------
# Map Reduce Plan
#--------------------------------------------------
MapReduce node scope-27
Map Plan
f1: Store(fakefile:org.apache.pig.builtin.PigStorage) - scope-26
|
|---f1: Filter[bag] - scope-22
| |
| Equal To[boolean] - scope-25
| |
| |---Project[chararray][0] - scope-23
| |
| |---Constant(3) - scope-24
|
|---t1: New For Each(false,false,false)[bag] - scope-21
| |
| Project[bytearray][0] - scope-15
| |
| Project[bytearray][1] - scope-17
| |
| Project[bytearray][2] - scope-19
|
|---t: Load(file:///Users/woody/test.txt:PigStorage(',')) - scope-14--------
Global sort: false
----------------
但是,如果我将头 2 行更改为:
t1 = LOAD 'test.txt' USING PigStorage(',') AS (i0:chararray, i1:chararray, i2:chararray)
(即在 LOAD 语句中分配模式)
任务运行良好,结果也正确。在这种情况下,EXPLAIN f1 显示:
#--------------------------------------------------
# Map Reduce Plan
#--------------------------------------------------
MapReduce node scope-33
Map Plan
f1: Store(fakefile:org.apache.pig.builtin.PigStorage) - scope-32
|
|---f1: Filter[bag] - scope-28
| |
| Equal To[boolean] - scope-31
| |
| |---Project[chararray][0] - scope-29
| |
| |---Constant(3) - scope-30
|
|---t1: New For Each(false,false,false)[bag] - scope-27
| |
| Cast[chararray] - scope-19
| |
| |---Project[bytearray][0] - scope-18
| |
| Cast[chararray] - scope-22
| |
| |---Project[bytearray][1] - scope-21
| |
| Cast[chararray] - scope-25
| |
| |---Project[bytearray][2] - scope-24
|
|---t1: Load(file:///Users/woody/test.txt:PigStorage(',')) - scope-17--------
Global sort: false
----------------
这是猪的虫子吗?或者有什么好的方法可以避免?
pig --version 在我的电脑上是:
Apache Pig version 0.9.2 (r1232772)
compiled Jan 18 2012, 07:57:19
【问题讨论】:
-
它可能不成熟,但我笑了。
-
是否有什么阻止您使用第二条路由并在 LOAD 语句中使用 AS 分配架构?这似乎不仅是一个可行的解决方案,而且是更高效的编码。
标签: hadoop apache-pig