hive hive窗口打印库和表头、复杂类型ARRAY、MAP 和 STRUCT的定义和使用


1. hive窗口打印库和表头

  在hive-site.xml中加入如下两个配置:

<property>
    <name>hive.cli.print.header</name>
    <value>true</value>
</property>
<property>
    <name>hive.cli.print.current.db</name>
    <value>true</value>
</property>

2. hive支持的类型

data type文档

Complex Types
arrays: ARRAY<data_type> (Note: negative values and non-constant expressions are allowed as of Hive 0.14.)
maps: MAP<primitive_type, data_type> (Note: negative values and non-constant expressions are allowed as of Hive 0.14.)
structs: STRUCT<col_name : data_type [COMMENT col_comment], …>
union: UNIONTYPE<data_type, data_type, …> (Note: Only available starting with Hive 0.7.0.)


3. 复杂类型

3.1 ARRAY、MAP 和 STRUCT

数据类型描述语法
STRUCT通过点符号访问元素内容。如列的数据类型是struct<street:string,city:string>,那么第1个元素可以通过字段名.street来引用STRUCT<col_name : data_type [COMMENT col_comment], ...>
MAPMAP是一组键值对元组集合,使用数组表示法可以访问数据。如列的数据类型是map<string,int>,列某一行的值为{"xiao song":18,"xiaoxiao song":19},那么可以通过 字段名['xiao song']获取第一个元素对应的值 MAP<primitive_type, data_type>
arrays数组是一组具有相同类型和名称的变量的集合。这些变量称为数组的元素,每个数组元素都有一个编号,编号从零开始。如数组值为["bingbing","lili"],那么第2个元素可以通过字段名[1]进行引用。ARRAY<data_type>

3.2 复杂类型使用范例

  (1)创建本地测试文件test.txt

songsong,bingbing_lili,xiao song:18_xiaoxiao song:19,hui long guan_beijing
yangyang,caicai_susu,xiao yang:18_xiaoxiao yang:19,chao yang_beijing
cz,bingbing_lili,xiao song:18_xiaoxiao song:19,hui long guan_beijing
zx,caicai_susu,xiao yang:18_xiaoxiao yang:19,chao yang_beijing
zmy,bingbing_lili,xiao song:18_xiaoxiao song:19,hui long guan_beijing
tom,caicai_susu,xiao yang:18_xiaoxiao yang:19,chao yang_beijing
mike,bingbing_lili,xiao song:18_xiaoxiao song:19,hui long guan_beijing
jack,caicai_susu,xiao yang:18_xiaoxiao yang:19,chao yang_beijing

  (2)创建测试表test

create table test(
name string,
friends array<string>,
children map<string, int>,
address struct<street:string, city:string>
)
row format delimited fields terminated by ','
collection items terminated by '_'
map keys terminated by ':'
lines terminated by '\n';

字段解释:

row format delimited fields terminated by ','  -- 列分隔符
collection items terminated by '_'  	--MAP STRUCT 和 ARRAY 的分隔符(数据分割符号)
map keys terminated by ':'			-- MAP中的key与value的分隔符
lines terminated by '\n';				-- 行分隔符

在这里插入图片描述

  (3)导入文本数据到测试表

load data local inpath '/home/hdfs/test.txt' into table test;

在这里插入图片描述
  (4)访问三种集合列里的数据,(ARRAY,MAP,STRUCT的访问方式)

select friends[1],children['xiao song'],address.city from test;

在这里插入图片描述


  • 2
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

但行益事莫问前程

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值