从Hive中的group by中选择单个随机样本

问题描述 投票:0回答:1

我有一个看起来像这样的表:

Name      Age       Num_Hobbies     Num Shoes
Jane      31        10              2
Bob       23        3               4
Jane      60        2               200
Jane      31        100             6
Bob       10        8               7
etc etc

我想按名称和年龄对此表进行分组,并随机从其余列中选择一行。

在熊猫中,我会做以下事情:

df.groupby(['Name', 'Age']).apply(lambda x: x.sample(n=1))

在hive中,我知道如何创建组,但不知道如何从组中选择单个随机样本。

我在堆栈溢出时看到了这个问题:How to sample for each group in hive?

但是,我不明白如何应用动态分区或Hive bucketing从组中选择单个样本。

random hive group-by
1个回答
0
投票

你可以使用rank()row_number()rand()

select * from 
(
       select name,age,rank() (partition by name,age order by rand()) as rank
       from table         
) t 
where rank = 1
© www.soinside.com 2019 - 2024. All rights reserved.