Map type variable in hive

后端 未结 1 578
旧巷少年郎
旧巷少年郎 2020-12-29 12:21

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. :-

相关标签:
1条回答
  • 2020-12-29 13:05

    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

    0 讨论(0)
提交回复
热议问题