0

我有一个 SQL 查询,我试图将其翻译成 Pig Latin(用于 Hadoop 集群)。大多数时候,我将查询移到 Pig 上没有问题,但我遇到了一些我似乎无法弄清楚的事情。

想象一下这样的 select 语句:

SELECT a.f1, b.f2, b.f3 -- just for example
FROM tableA a, tableB b
WHERE( 
(
    a.f1 = b.f2
    AND
    (
        a.f2 = b.f1
        OR
        (
            (a.f2 = 'somestring1' OR a.f2 = 'somestring2')
            AND
            (b.f1 is null OR b.f1 ='somestring3' OR b.f1 = 'somestring4')
        )
    )
OR
(
    a.f3 = b.f4
    AND
    (
        a.f4 = b.f3
        OR
        (
            (a.f4 = 'somestring5' OR a.f4 = 'somestring6')
            AND
            (b.f3 is null OR b.f3 ='somestring7' OR b.f3 = 'somestring8')
        )
    )
)
)
GROUP BY -- some other stuff

现在,我知道直接翻译为 Pig 可能是不可能的。如果是这种情况,我想知道如何将此语句分解为计算它所需的 JOIN(和过滤器)(因为可能更容易看到如何从中构造一个 Pig 查询)。

我已经访问了 Pig 邮件列表,但我还没有找到一个好的解决方案,因为 Pig 并没有真正对 JOIN 执行“或”操作。我知道这个问题有多奇怪。

(如果有人确实知道如何在 Pig 中完成此操作,我不会反对查看它......)

编辑:有谁知道这在 Hive 中是否会更容易,也许?

4

2 回答 2

1

UNION 是另一种拼写 OR ... 的方式。

也许:

SELECT a.f1, b.f2, b.f3 -- just for example
  FROM tableA a JOIN tableB b
    ON a.f1 = b.f2 AND
       (a.f2 = b.f1 OR
           ((a.f2 = 'somestring1' OR a.f2 = 'somestring2') AND
            (b.f1 is null OR b.f1 ='somestring3' OR b.f1 = 'somestring4')
           )
       )
UNION
SELECT a.f1, b.f2, b.f3 -- just for example
  FROM tableA a JOIN tableB b
    ON a.f3 = b.f4 AND
       (a.f4 = b.f3 OR
           ((a.f4 = 'somestring5' OR a.f4 = 'somestring6') AND
            (b.f3 is null OR b.f3 ='somestring7' OR b.f3 = 'somestring8')
           )
       )
GROUP BY -- some other stuff
于 2011-05-10T15:33:09.553 回答
0

我不知道 Pig,但在 SQL 中,您的语句等同于您所写的:

select ...
from a
join b on [your enormous condition]
group by ...

它也会很慢,因为缺少一个优化器(例如 Postgres)来分解 OR 子句并使用适当的索引尝试每个子句,您最终会交叉连接两个表。

select from (select ... union select ...) group by ...如果 Pig 对后者更有意义,您可以改写该语句。

于 2011-05-10T15:34:36.393 回答