11

I am having trouble trying to define map type in hive. According to Hive Manual there definitely is a map type, unfortunately there aren't any examples on how to use it. :-(

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

Name     Ph    CategoryName

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

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

I have 2 questions:

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

  2. I am not sure how can I say something like, select name, ph, ${nameToID[CategoryName]} from users

BSMP
  • 4,596
  • 8
  • 33
  • 44
test123
  • 13,865
  • 9
  • 28
  • 33

1 Answers1

28

Let's assume 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}

Accessing the map column :

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

An equivalent using a 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

This works on Hive 0.9.0

Lorand Bendig
  • 10,630
  • 1
  • 38
  • 45
  • Thanks for the reply Lorand. I just checked the hive version on our node and unfortunately it is 0.8.1.6. So I think I am out of luck here. :-( Thanks anyways for your help! – test123 Jan 30 '13 at 20:15
  • Hi Lorand, here is what I am trying: `hive> set hivevar:nameToID=map("t","2","c","1");` `hive> set ${nameToID}["t"];` `${nameToID}["t"] is undefined` – test123 Jan 31 '13 at 21:07
  • What do you want to achieve? Hive variables/properties are **[key=value]** pairs. `set ${nameToID}["t"]` seems to be incomplete. If you want to access to a map value by using hivevar then see my answer above. Also note when accessing to a collection's value you can't evaluate any function at the same time that returns the index you need (in this case you'll get `Non-constant expression for map indexes not supported` error). That's why I defined the complete field name in the map values. – Lorand Bendig Feb 01 '13 at 16:57