Pig:在 GROUP 之后将各个字段拉出

发布于 2024-12-09 20:54:10 字数 434 浏览 0 评论 0原文

在 PigLatin 中,我想从由于聚合(例如 MAX)而选择的记录中提取其他字段。

我无法解释这个问题,所以这是一个例子。假设我想获取一个家庭中最年长的人的名字:

关系A有四列,(姓名,地址,邮政编码,年龄)

B = GROUP A BY (address, zipcode); # group by the address

# generate the address, the person's age, but how do I grab that person's name?
C = FOREACH B GENERATE FLATTEN(group), MAX(age), ??? Name ???;

我如何生成MAX 年龄的人的姓名?

In PigLatin, I want to pull the other fields out of a record I want to select because of an aggregate, such as MAX.

I'm having trouble explaining the problem, so here is an example. Let's say I want to grab the name of the oldest person at a household:

Relation A is four columns, (name, address, zipcode, age)

B = GROUP A BY (address, zipcode); # group by the address

# generate the address, the person's age, but how do I grab that person's name?
C = FOREACH B GENERATE FLATTEN(group), MAX(age), ??? Name ???;

How do I generate the name of the person with the MAX age?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

新人笑 2024-12-16 20:54:10

你的逻辑问题是,可能有超过 1 个人的 MAX(年龄)。然后你必须按(姓名、地址、年龄)进行分组。但为了给你一个快速的答案,我会写下只有最大年龄之一。 (虽然我不确定这是最佳方式)

C = FOREACH B {                          
   DA = ORDER A BY age DESC;                
   DB = LIMIT DA 1;                         
   GENERATE FLATTEN(group), FLATTEN(DB.age), FLATTEN(DB.name);
}

The problem with your logic is there can be more then 1 people with the MAX(age). Then you have to GROUP BY (name, address, age). But to give you a quick answer I will write that gets only one of the max ages. (I am not sure its the optimum way though)

C = FOREACH B {                          
   DA = ORDER A BY age DESC;                
   DB = LIMIT DA 1;                         
   GENERATE FLATTEN(group), FLATTEN(DB.age), FLATTEN(DB.name);
}
素手挽清风 2024-12-16 20:54:10

请小心 frail 接受的答案,因为如果 LIMIT 命令中的数字高于 1,它会产生不良行为。特别是,在这种情况下,输出将是所有年龄和名称之间的叉积,因为最后一个两次 FLATTEN 调用。然后,如果 LIMIT 中的值为 N,则将有 N^2 输出行,而不是预期的 N。

更安全的方法是在 GENERATE 行中执行以下操作,这将给出与 'LIMIT 时接受的答案完全相同的结果使用 1':

GENERATE FLATTEN(group) AS (address, zipcode), FLATTEN(DB.(age, name)) AS (age, name);

Be careful with frail's answer which is accepted, as it would have undesirable behavior if the number in the LIMIT command is higher than 1. In particular, in that case the output would be a cross-product between all ages and names due to the last two FLATTEN calls. Then, if the value in the LIMIT is N, there would be N^2 output rows instead of intended N.

Much safer is to do the following in the GENERATE line, which would give exactly the same result as the accepted answer when 'LIMIT 1' is used:

GENERATE FLATTEN(group) AS (address, zipcode), FLATTEN(DB.(age, name)) AS (age, name);
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文