How to sort an array and return the index in hive?

Deadly 提交于 2021-02-08 05:28:51

问题


In hive, I wish to sort an array from largest to smallest, and get the index array.

For example, the table is like this:

id  |  value_array
 1  |  {30, 40, 10, 20}
 2  |  {10, 30, 40, 20}

I with to get this:

id  |  value_array
 1  |  {1, 0, 3, 2}
 2  |  {2, 1, 3, 0}

The arries in result are the index of the initial elements. How can I achieve this?


回答1:


Explode array using posexplode to get index and value, sort by value, collect array of index:

select id, collect_list(pos) as result_array
from
(
select s.id, a.pos, a.v 
  from your_table s
       lateral view posexplode(s.value_array) a as pos, v
distribute by s.id sort by a.v DESC --sort by value
)s
group by id
;

Tested, result:

id  result_array
1   [1,0,3,2]
2   [2,1,3,0]


来源:https://stackoverflow.com/questions/61935124/how-to-sort-an-array-and-return-the-index-in-hive

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!