Map type hive variable

I'm having trouble trying to determine the type of map in the hive. According to Hive Manual there is definitely a card type, unfortunately there are no examples of how to use it. :-(

Suppose I have a table (users) with the following columns:

Name Ph CategoryName 

This "CategoryName" column has a specific set of values. Now I want to create a hash table that maps CategoryName to CategoryID. I tried:

 set hivevar:nameToID=map('A',1,'B',2); 

I have 2 questions:

  • When I do set hivevar:${nameToID['A']} , I thought it would print the value as 1. But I get "$ {hivevar: nameToID ['A']} is undefined"

  • I'm not sure how I can say something like select name, ph, ${nameToID[CategoryName]} from users

Please let me know about this. Thank you

+8
hive hiveql
source share
1 answer

Suppose you have the following table:

 describe test; name string ph string category map<string,int> select * from test; name ph category Name1 ph1 {"type":1000,"color":200,"shape":610} Name2 ph2 {"type":2000,"color":200,"shape":150} Name3 ph3 {"type":3000,"color":700,"shape":167} 

Map column access:

 select ph, category["type"], category["color"] from test; ph1 1000 200 ph2 2000 200 ph3 3000 700 

Equivalent using the Hive variable:

 set hivevar:nameToID= map("t", category["type"], "c", category["color"], "s", category["shape"]); select ph, ${nameToID}["t"], ${nameToID}["c"] from test; ph1 1000 200 ph2 2000 200 ph3 3000 700 

It works on Hive 0.9.0

+18
source share

All Articles