Get invited to our slack community and get access to opportunities and data science insights


to_ordered_map(key, value [, const int k|const boolean reverseOrder=false]) – Convert two aggregated columns into an ordered key-value map

with t as (
select 10 as key, ‘apple’ as value
union all
select 3 as key, ‘banana’ as value
union all
select 4 as key, ‘candy’ as value
)
select
to_ordered_map(key, value, true), — {10:”apple”,4:”candy”,3:”banana”} (reverse)
to_ordered_map(key, value, 1), — {10:”apple”} (top-1)
to_ordered_map(key, value, 2), — {10:”apple”,4:”candy”} (top-2)
to_ordered_map(key, value, 3), — {10:”apple”,4:”candy”,3:”banana”} (top-3)
to_ordered_map(key, value, 100), — {10:”apple”,4:”candy”,3:”banana”} (top-100)
to_ordered_map(key, value), — {3:”banana”,4:”candy”,10:”apple”} (natural)
to_ordered_map(key, value, -1), — {3:”banana”} (tail-1)
to_ordered_map(key, value, -2), — {3:”banana”,4:”candy”} (tail-2)
to_ordered_map(key, value, -3), — {3:”banana”,4:”candy”,10:”apple”} (tail-3)
to_ordered_map(key, value, -100) — {3:”banana”,4:”candy”,10:”apple”} (tail-100)
from t

Platforms: WhereOS, Spark, Hive
Class: hivemall.tools.map.UDAFToOrderedMap

More functions can be added to WhereOS via Python or R bindings or as Java & Scala UDF (user-defined function), UDAF (user-defined aggregation function) and UDTF (user-defined table generating function) extensions. Custom libraries can be added on via Settings-page or installed from WhereOS Store.

Related Post

Leave a Comment