1、简单描述

  • arrays: ARRAY<data_type>

  • maps: MAP<primitive_type, data_type>

  • structs: STRUCT<col_name : data_type [COMMENT col_comment], ...>

  • union: UNIONTYPE<data_type, data_type, ...>

Hive 中对该类型的完全支持仍然不完整。如果 JOIN、WHERE 和 GROUP BY 子句中引用的 UNIONTYPE 字段的查询将会失败,Hive 没有定义语法来提取 UNIONTYPE 的 tag 或 value 字段。

复杂数据类型的构造函数:

构造函数 操作数 描述
map (key1, value1, key2, value2, ...) Creates a map with the given key/value pairs.
struct (val1, val2, val3, ...) Creates a struct with the given field values. Struct field names will be col1, col2, ....
named_struct (name1, val1, name2, val2, ...) Creates a struct with the given field names and values. (As of Hive 0.8.0.)
array (val1, val2, ...) Creates an array with the given elements.
create_union (tag, val1, val2, ...) Creates a union type with the value that is being pointed to by the tag parameter.

注:create_union 中的 tag 让我们知道 union 的哪一部分正在被使用。

复杂数据类型访问元素:

构造函数 操作数 描述
A[n] A is an Array and n is an int Returns the nth element in the array A. The first element has index 0. For example, if A is an array comprising of ['foo', 'bar'] then A[0] returns 'foo' and A[1] returns 'bar'.
M[key] M is a Map<K, V> and key has type K Returns the value corresponding to the key in the map. For example, if M is a map comprising of {'f' -> 'foo', 'b' -> 'bar', 'all' -> 'foobar'} then M['all'] returns 'foobar'.
S.x S is a struct Returns the x field of S. For example for the struct foobar {int foo, int bar}, foobar.foo returns the integer stored in the foo field of the struct.

2、测试

-- ------------------------------ ARRAY ------------------------------

-- ARRAY<data_type>
create table arraytest (id int,info array<string>)
row format delimited
fields terminated by '\t'
collection items terminated by ','
stored as textfile; -- 不要忽略`collection items terminated by ','
-- 它表示数组元素间的分隔符
-- 如果忽略了输出是这样的:
hive> select * from arraytest;
OK
1 ["zhangsan,male"]
2 ["lisi,male"] -- 数据
1 zhangsan,male
2 lisi,male -- 导入
load data local inpath '/root/data/arraytest.txt' into table arraytest; -- 查看
hive> select * from arraytest;
OK
1 ["zhangsan","male"]
2 ["lisi","male"] -- 索引查看数组元素
hive> select id,info[0] from arraytest;
OK
1 zhangsan
2 lisi -- 将数组的所有元素展开输出
hive> select explode(info) from arraytest;
OK
zhangsan
male
lisi
male -- ------------------------------ MAP ------------------------------ -- MAP<primitive_type, data_type>
create table maptest (id int,info map<string,string>)
row format delimited
fields terminated by '\t'
collection items terminated by ','
map keys terminated by ':'
stored as textfile; -- 不要忽略`map keys terminated by ':'
-- 它表示键值间的分隔符 -- 数据
1 name:zhangsan,sex:male
2 name:lisi,sex:male -- 导入
load data local inpath '/root/data/maptest.txt' into table maptest; -- 查看
hive> select * from maptest;
OK
1 {"name":"zhangsan","sex":"male"}
2 {"name":"lisi","sex":"male"} -- 查看map元素
hive> select id,info["name"] from maptest;
OK
1 zhangsan
2 lisi -- ------------------------------ STRUCT ------------------------------ -- STRUCT<col_name : data_type [COMMENT col_comment], ...>
create table structtest (id int,info struct<name:string,sex:string>)
row format delimited
fields terminated by '\t'
collection items terminated by ','
stored as textfile; -- 数据
1 zhangsan,male
2 lisi,male -- 导入
load data local inpath '/root/data/structtest.txt' into table structtest; -- 查看
hive> select * from structtest;
OK
1 {"name":"zhangsan","sex":"male"}
2 {"name":"lisi","sex":"male"} hive> select id,info.name from structtest;
OK
1 zhangsan
2 lisi -- ------------------------------ 综合array\map\struct ------------------------------ create table alltest(
id int,
name string,
salary bigint,
sub array<string>,
details map<string, int>,
address struct<city:string, state:string, pin:int>
)
row format delimited
fields terminated by ','
collection items terminated by '$'
map keys terminated by '#'
stored as textfile; -- 数据
1,abc,40000,a$b$c,pf#500$epf#200,hyd$ap$500001
2,def,3000,d$f,pf#500,bang$kar$600038
4,abc,40000,a$b$c,pf#500$epf#200,bhopal$MP$452013
5,def,3000,d$f,pf#500,Indore$MP$452014 -- 导入数据
load data local inpath '/root/data/alltest.txt' into table alltest; -- 查看
hive> select * from alltest;
OK
1 abc 40000 ["a","b","c"] {"pf":500,"epf":200} {"city":"hyd","state":"ap","pin":500001}
2 def 3000 ["d","f"] {"pf":500} {"city":"bang","state":"kar","pin":600038}
4 abc 40000 ["a","b","c"] {"pf":500,"epf":200} {"city":"bhopal","state":"MP","pin":452013}
5 def 3000 ["d","f"] {"pf":500} {"city":"Indore","state":"MP","pin":452014} -- ------------------------------ UNIONTYPE ------------------------------ -- create_union(tag, val1, val2, ...)
-- Creates a union type with the value that is being pointed to by the tag parameter. -- ---- 简单示例:里面都是基本类型 ------ create table uniontest(
id int,
info uniontype<string,string>
)
row format delimited
fields terminated by '\t'
collection items terminated by ','
stored as textfile; -- 插入数据:insert into
-- tag 索引后面的值是从 0 开始的
insert into table uniontest
values
(1,create_union(0,"zhangsan","male")), -- 使用 "zhangsan"
(1,create_union(1,"zhangsan","male")), -- 使用 "male"
(2,create_union(0,"lisi","female")),
(2,create_union(1,"lisi","female")); -- 查看
hive> select * from uniontest;
OK
1 {0:"zhangsan"}
1 {1:"male"}
2 {0:"lisi"}
2 {1:"female"} -- 数据
1 0,zhangsan
1 1,male
2 0,lisi
2 1,female -- 插入数据:load data
load data local inpath '/root/data/uniontest.txt' into table uniontest; -- 查看
hive> select * from uniontest;
OK
1 {0:"zhangsan"}
1 {1:"male"}
2 {0:"lisi"}
2 {1:"female"} -- 如果数据格式是这样的:
-- 1 0,zhangsan,male
-- 1 1,zhangsan,male
-- 2 0,lisi,female
-- 2 1,lisi,female
-- 会把后面的字符串当作一个整体,输出:
-- 1 {0:"zhangsan,male"}
-- 1 {1:"zhangsan,male"}
-- 2 {0:"lisi,female"}
-- 2 {1:"lisi,female"} -- ---- 复杂示例:里面包含复杂类型 ------ create table uniontest_comp(
id int,
info uniontype<int,
string,
array<string>,
map<string,string>,
struct<sex:string,age:string>>
)
row format delimited
fields terminated by '\t'
collection items terminated by ','
stored as textfile; -- 插入数据
-- 也可以使用 `insert into table ....select ....`
insert into table uniontest_comp
values
(1,create_union(0,1,"zhangsan",array("male","33"),map("sex","male","age","33"),named_struct("sex","male","age","33"))),
(1,create_union(1,1,"zhangsan",array("male","33"),map("sex","male","age","33"),named_struct("sex","male","age","33"))),
(1,create_union(2,1,"zhangsan",array("male","33"),map("sex","male","age","33"),named_struct("sex","male","age","33"))),
(1,create_union(3,1,"zhangsan",array("male","33"),map("sex","male","age","33"),named_struct("sex","male","age","33"))),
(1,create_union(4,1,"zhangsan",array("male","33"),map("sex","male","age","33"),named_struct("sex","male","age","33"))); -- 查看
hive> select * from uniontest_comp;
OK
1 {0:1}
1 {1:"zhangsan"}
1 {2:["male","33"]}
1 {3:{"sex":"male","age":"33"}}
1 {4:{"sex":"male","age":"33"}}

参考:http://querydb.blogspot.com/2015/11/hive-complex-data-types.html

hive复杂数据类型的用法的更多相关文章

  1. 大数据时代的技术hive:hive的数据类型和数据模型

    在上篇文章里,我列举了一个简单的hive操作实例,创建了一张表test,并且向这张表加载了数据,这些操作和关系数据库操作类似,我们常把hive和关系数据库进行比较,也正是因为hive很多知识点和关系数 ...

  2. day01-day04总结- Python 数据类型及其用法

    Python 数据类型及其用法: 本文总结一下Python中用到的各种数据类型,以及如何使用可以使得我们的代码变得简洁. 基本结构 我们首先要看的是几乎任何语言都具有的数据类型,包括字符串.整型.浮点 ...

  3. Hive 5、Hive 的数据类型 和 DDL Data Definition Language)

    官方帮助文档:https://cwiki.apache.org/confluence/display/Hive/LanguageManual+DDL Hive的数据类型 -- 扩展数据类型data_t ...

  4. hadoop笔记之Hive的数据类型

    Hive的数据类型 Hive的数据类型 前面说过,Hive是一个数据仓库,相当于一个数据库.既然是数据库,那么就必须能创建表,既然有表,那么当中就有列,列中就有对应的类型 总的来讲,hive的数据类型 ...

  5. Hive之数据类型

    Hive之数据类型   (本文是基于多篇文章根据个人理解进行的整合,参考的文章见末尾的整理) 数据类型 Hive支持两种数据类型,一类叫原子数据类型,一类叫复杂数据类型.原子数据类型包括数值型.布尔型 ...

  6. Hive 复杂数据类型的使用

    Hive复杂数据类型 1.Array数据类型的使用 1.1.创建数据库表,以array作为数据类型 hive (hive_demo1)> create table stu_test(name a ...

  7. 《Hive编程指南》读书笔记 | 一文看懂Hive的数据类型和文件格式

    Hive支持关系型数据库中的大多数基本数据类型,同时也支持关系型数据库中很少出现的3种集合数据类型. 和大多数数据库相比,Hive具有一个独特的功能,那就是其对于数据在文件中的编码方式具有非常大的灵活 ...

  8. Mybatis中动态SQL语句中的parameterType不同数据类型的用法

    Mybatis中动态SQL语句中的parameterType不同数据类型的用法1. 简单数据类型,    此时#{id,jdbcType=INTEGER}中id可以取任意名字如#{a,jdbcType ...

  9. 【Kylin实战】Hive复杂数据类型与视图

    1. 引言 在分析广告日志时,会有这样的多维分析需求: 曝光.点击用户分别有多少? 标签能覆盖多少广告用户? 各个标签(标注)类别能覆盖的曝光.点击在各个DSP上所覆盖的用户数 -- 广告数据与标签数 ...

随机推荐

  1. 【bzoj 2339】[HNOI2011]卡农(数论--排列组合+逆元+递推)

    题意:从编号为 1~N 的音阶中可选任意个数组成一个音乐片段,再集合组成音乐篇章.要求一个音乐篇章中的片段不可重复,都不为空,且出现的音符的次数都是偶数个.问组成 M 个片段的音乐篇章有多少种.答案取 ...

  2. c语言实现n!算法

    最近一面学习数据结构,一面在做些c语言的题目.这个题目前些天碰到,和同学讨论了下.于是就用c语言实现n!(n=10000) 1 #include<stdio.h> 2 #define MA ...

  3. Codeforces Round #Pi (Div. 2) D. One-Dimensional Battle Ships

    Alice and Bob love playing one-dimensional battle ships. They play on the field in the form of a lin ...

  4. 记一次基于springboot+aop实现日志记录实战

    1. 为什么要记录日志 好处: a. 可以对一些重要功能进行记录,方便以后跟踪是谁操作此功能的. b. 在操作某些功能时可能会发生异常,但每次出现异常我们想定位日志都要去服务器查看我们的日志.有了日志 ...

  5. 国产网络测试仪MiniSMB - 如何3秒内创建出16,000条源/目标MAC地址号递增流

    国产网络测试仪MiniSMB(www.minismb.com)是复刻smartbits的IP网络性能测试工具,是一款专门用于测试智能路由器,网络交换机的性能和稳定性的软硬件相结合的工具.可以通过此以太 ...

  6. OpenStack Train版-6.安装nova计算服务(计算节点)

    安装nova计算服务(computel01计算节点 192.168.0.20)安装软件包 yum install centos-release-openstack-train -y yum insta ...

  7. .NET中使用DebuggerDisplay轻松定制调试

    前言 对于调试的方式有多种,不过在今天我们将看到的监视窗口对变量的监视,当然在这里我们是定制内部的变量值,或者说变量的显示与计算的内容. 注:监视窗口在调试时可以一次显示多个变量."快速监视 ...

  8. oslab oranges 一个操作系统的实现 实验一

    实验目的: 搭建基本实验环境,熟悉基本开发与调试工具 对应章节:第一.二章 实验内容: 1.认真阅读章节资料 2.在实验机上安装virtualbox,并安装ubuntu 3.安装ubuntu开发环境, ...

  9. Leetcode(102)-二叉树的层次遍历

    给定一个二叉树,返回其按层次遍历的节点值. (即逐层地,从左到右访问所有节点). 例如:给定二叉树: [3,9,20,null,null,15,7], 3 / \ 9 20 / \ 15 7 返回其层 ...

  10. Linux 驱动框架---驱动中的并发

    并发指多个执行单元被同时.并行的执行,而并发执行的单元对共享资源的访问就容易导致竟态.并发产生的情况分为抢占和并行(多核)和硬抢占(中断).Linux为解决这一问题增加了一系列的接口来解决并发导致的竟 ...