问题
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