In Hive, there are map, array, and struct at the same time. How should the separator be specified in the table-building statement?

      There are map, array, and struct formats in Hive. If these three formats exist at the same time, how should the separator for the table-building statement be specified?
      

1. Say the answer first

      Say the answer first:

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';

      Field explanation:

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

      There are actually only two places that need explanation:
      ①. Collection items terminated by '_', in hive, map, array, and struct are all specified using collection items terminated by, so only one separator can be shared.
      ②. Lines terminated by '\ n', no need to write, the line separator is \ n by default

2. Give an example

      Assuming the following data, you need to insert into the hive related table

{
	"name": "张三",
	"friends": ["李四" , "王五"] , //列表 Array,
	"children": { //键值 Map,
		"小李四": 18 ,
		"小王五": 19
	}
	"address": { //结构 Struct,
		"street": "大兴" ,
		"city": "北京"
	}
}
  1. First, organize it into a piece of data:
张三,李四_王五,小李四:18_小王五:19,大兴_北京

      Note the separator

  1. Build table
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 the data into the document first, and then read it into hive
load data local inpath
"/home/software/data/test.txt" into table test;
  1. Access method
    Access map:
select 	friends[1], /* 这是访问array */
	children['xiaosong'], /* 这是访问map */
	address.city
	from test;

      
      

Published 48 original articles · Like 36 · Visits 130,000+

Guess you like

Origin blog.csdn.net/weixin_42845682/article/details/104919328