Hadoop 在Pig拉丁语中使用FOREACH后的筛选器失败

Hadoop 在Pig拉丁语中使用FOREACH后的筛选器失败,hadoop,apache-pig,Hadoop,Apache Pig,我是一个猪拉丁语的初学者,我发现了一个关于过滤语句的问题。看看这个例子: 假设我们有一个数据文件(test.txt),其内容是: 1,2,3 2,3,4 3,4,5 4,5,6 我想选择第一个字段为“3”的记录。猪的脚本是: t = LOAD 'test.txt' USING PigStorage(','); t1 = FOREACH t GENERATE $0 AS i0:chararray, $1 AS i1:chararray, $2 AS i2:chararray; f1 = FILT

我是一个猪拉丁语的初学者,我发现了一个关于过滤语句的问题。看看这个例子:

假设我们有一个数据文件(test.txt),其内容是:

1,2,3
2,3,4
3,4,5
4,5,6
我想选择第一个字段为“3”的记录。猪的脚本是:

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
----------------
#--------------------------------------------------
# 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
----------------
但是,如果我将头2行更改为:

t1 = LOAD 'test.txt' USING PigStorage(',') AS (i0:chararray, i1:chararray, i2:chararray)
(即在LOAD语句中分配模式)

该任务运行良好,结果也正确。在这种情况下,EXPLAIN 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
----------------
#--------------------------------------------------
# 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--我的计算机上的版本是:

Apache Pig version 0.9.2 (r1232772)
compiled Jan 18 2012, 07:57:19

我知道,在GENERATE中,这会为数据提供一种类型,但不会执行真正的转换:

GENERATE $0 AS i0:chararray
您需要手动强制转换它:

t1 = FOREACH t GENERATE (chararray) $0 AS i0, (chararray) $1 AS i1, (chararray) $2 AS i2;

这是违反直觉的,可能是一个bug。

有趣的是,这似乎是一个已知的问题,被视为“无法修复”,因此它不是真正的bug。这是一种奇怪的行为,似乎可以解释我过去在使用过滤函数时遇到的一些奇怪现象

以下内容类似,并在注释流中以“不会修复”结束:

加载期间的强制转换似乎有一些微妙之处,这可能对其他人有所帮助:

前面的答案非常准确——我已经确认它确实返回了预期的结果


这可能促使我在未来明确地投下更多——伟大的问题和答案。很抱歉没有添加任何内容……但我认为该主题的内容已经足够发布了。

这应该有用:

t=使用PigStorage(',')作为(i0:int,i1:int,i2:int)加载“test.txt”

t=滤波器t乘以i0==3


倾销税

这可能是不成熟的,但我很高兴。是否有任何东西阻止您使用第二个路由并在LOAD语句中使用AS分配模式?这似乎不仅是一个可行的解决方案,而且是一个更高效的编码。只是为了其他用户的启发:这不是一个“把戏”。这是一个标准的Java风格的强制转换操作符。