11

我在尝试在 hive 中定义地图类型时遇到了麻烦。根据Hive Manual肯定有一个地图类型,不幸的是没有任何关于如何使用它的例子。:-(

假设,我有一个包含以下列的表(用户):

Name     Ph    CategoryName

此“CategoryName”列具有特定的值集。现在我想创建一个将 CategoryName 映射到 CategoryID 的哈希表。我试着做:

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

我有两个问题:

  1. 当我这样做时,set hivevar:${nameToID['A']}我认为它会将值打印为 1。但我得到“${hivevar:nameToID['A']} is undefined”

  2. 我不确定我该怎么说,select name, ph, ${nameToID[CategoryName]} from users

4

1 回答 1

28

假设您有下表:

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}

访问地图列:

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

使用 Hive 变量的等效项:

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

这适用于 Hive 0.9.0

于 2013-01-29T12:20:09.683 回答