配置单元中的映射类型变量 [英] Map type variable in hive

查看:111
本文介绍了配置单元中的映射类型变量的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我试图在配置单元中定义地图类型时遇到问题。根据 Hive手册,肯定有地图类型,不幸的是没有任何关于如何使用它的例子。 :-(

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

这个CategoryName列有一组特定的值。希望创建一个将CategoryName映射到CategoryID的散列表,我尝试过:

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. 当我做设置hivevar:$ {nameToID ['A']} 我认为它将打印出价值为1.但我得到
    $ {hivevar:nameToID ['A']}未定义

  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"

我不是确定我怎么能说,像 select name,ph,$ {nameToID [CategoryName]}这样的用户

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

请让我知道这些
Thanks!

Please let me know about these. Thanks!

推荐答案

假设您有以下表格:

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

使用Hive变量的等价物:

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

这适用于Hive 0.9.0

This works on Hive 0.9.0

这篇关于配置单元中的映射类型变量的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆