Skip to content
Advertisement

How to transform data into a map using group by in Hive SQL?

I have data like below

…and I want to create a map with lecture as the key and count as a value. How can I get an output like below?

Advertisement

Answer

If you can live with count being a string, you probably be able to use Hive str_to_map() function to get a desired map. That will require a couple of preliminary steps, to reformat column values in a way accepted by it. Something like this:

Output:

Otherwise, you’re looking at writing your own UDAF or using one of the existing ones built by third-parties, at least until JIRA-4966 is resolved (although those chances are quite low after 7 years).

User contributions licensed under: CC BY-SA
5 People found this is helpful
Advertisement