【问题标题】:Is it possible to do a 'normalized' dense_rank() in hive?是否可以在 hive 中进行“标准化”dense_rank()?
【发布时间】:2017-03-30 22:50:07
【问题描述】:

我有一个这样的消费者表。

consumer | product | quantity
-------- | ------- | --------
a        | x       | 3
a        | y       | 4
a        | z       | 1
b        | x       | 3
b        | y       | 5
c        | x       | 4

我想要的是分配给每个消费者的“标准化”排名,以便我可以轻松拆分表格以进行测试和培训。我在 hive 中使用了 dense_rank(),所以得到了下表。

rank | consumer | product | quantity
---- | -------- | ------- | --------
1    | a        | x       | 3
1    | a        | y       | 4
1    | a        | z       | 1
2    | b        | x       | 3
2    | b        | y       | 5
3    | c        | x       | 4

这很好,但我想扩展它以与任意数量的消费者一起使用,所以理想情况下我希望排名范围在 0 和 1 之间,就像这样。

rank | consumer | product | quantity
---- | -------- | ------- | --------
0.33 | a        | x       | 3
0.33 | a        | y       | 4
0.33 | a        | z       | 1
0.67 | b        | x       | 3
0.67 | b        | y       | 5
1    | c        | x       | 4

这样,我总是知道排名的范围是多少,并且可以以标准方式拆分数据(排名 0.7 测试)

有没有办法在 hive 中实现这一点?

或者,对于我原来拆分数据的问题,是否有不同的更好的方法?

我尝试执行select * where rank < 0.7*max(rank),但 hive 说 MAX UDAF 在 where 子句中尚不可用。

【问题讨论】:

  • UDAF 在where 子句中永远不可用。它与 SQL 语言定义不匹配。

标签: hadoop machine-learning hive training-data


【解决方案1】:

percent_rank

select  percent_rank() over (order by consumer) as pr
       ,* 

from    mytable
;

+-----+----------+---------+----------+
| pr  | consumer | product | quantity |
+-----+----------+---------+----------+
| 0.0 | a        | z       |        1 |
| 0.0 | a        | y       |        4 |
| 0.0 | a        | x       |        3 |
| 0.6 | b        | y       |        5 |
| 0.6 | b        | x       |        3 |
| 1.0 | c        | x       |        4 |
+-----+----------+---------+----------+

为了过滤,您需要一个子查询/CTE

select  *

from   (select  percent_rank() over (order by consumer) as pr
               ,* 

        from    mytable
        ) t

where   pr <= ...
;

【讨论】:

  • 太完美了!谢谢!
猜你喜欢
  • 1970-01-01
  • 2012-02-14
  • 1970-01-01
  • 1970-01-01
  • 2015-05-01
  • 1970-01-01
  • 2018-06-16
  • 2013-09-30
  • 1970-01-01
相关资源
最近更新 更多