Hive中,同时存在map、array、struct,建表语句应该怎么指定分隔符?

7 篇文章 0 订阅

      Hive中存在map、array、和struct格式,那如果同时存在这三种格式时,建表语句的分隔符应该怎么指定呢?
      

一、 先说答案

      先说答案:

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';       /* 行分隔符  */

      其中需要解释的地方其实只有两个:
      ①. collection items terminated by ‘_’ ,在hive中,map、array、struct都使用collection items terminated by来指定,所以只能共用一个分隔符。
      ②. lines terminated by ‘\n’, 不写也行,行分隔符默认就是 \n

二、 举个例子

      假设有如下数据,需要插入到hive相关表中

{
	"name": "张三",
	"friends": ["李四" , "王五"] , //列表 Array,
	"children": { //键值 Map,
		"小李四": 18 ,
		"小王五": 19
	}
	"address": { //结构 Struct,
		"street": "大兴" ,
		"city": "北京"
	}
}
  1. 首先,将其整理成一条数据:
张三,李四_王五,小李四:18_小王五:19,大兴_北京

      注意分隔符

  1. 建表
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';
  1. 将数据先vim到文档中,再读取到hive中
load data local inpath
"/home/software/data/test.txt" into table test;
  1. 访问方式
    访问map:
select 	friends[1], /* 这是访问array */
	children['xiaosong'], /* 这是访问map */
	address.city
	from test;

      
      

  • 0
    点赞
  • 7
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值