Pig:在 GROUP 之后将各个字段拉出
在 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
你的逻辑问题是,可能有超过 1 个人的 MAX(年龄)。然后你必须按(姓名、地址、年龄)进行分组。但为了给你一个快速的答案,我会写下只有最大年龄之一。 (虽然我不确定这是最佳方式)
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)
请小心 frail 接受的答案,因为如果 LIMIT 命令中的数字高于 1,它会产生不良行为。特别是,在这种情况下,输出将是所有年龄和名称之间的叉积,因为最后一个两次 FLATTEN 调用。然后,如果 LIMIT 中的值为 N,则将有 N^2 输出行,而不是预期的 N。
更安全的方法是在 GENERATE 行中执行以下操作,这将给出与 'LIMIT 时接受的答案完全相同的结果使用 1':
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: