HIVE : array,map,struct 文本批量导入和str_to_map,named_struct多条插入使用

转载:https://blog.csdn.net/qq_27082249/article/details/78912727

1.array文本批量导入和数据插入

1.1数据准备
   1,zhangsan,唱歌-跳舞-游泳
   2,lishi,打游戏-唱歌-篮球

1.2表的创建

  create table t_arr(id int,name string,hobby array<string>)
  row format delimited
  fields terminated by ','  --字段以逗号终止
  collection items terminated by '-';  --字符串符号分割

1.3数据导入

  load data local inpath '/root/txt/array.txt' into table t_arr;

1.4数据插入

insert into table t_arr select 3,'xm',array('A','B','C') from t_b limit 1;

 
2.map文本批量导入和str_to_map 数据插入

2.1数据准备

  1,zhangsan,唱歌:非常喜欢-跳舞:喜欢-游泳:一般般
  2,lishi,打游戏:非常喜欢-篮球:不喜欢

2.2表的创建

create table if not exists t_map(id int,name string,hobby map<string,string>)
row format delimited
fields terminated by ','   --字段以逗号终止
collection items terminated by '-' --字符串符号分割
map keys terminated by ':';   --map的KV分割符号

2.3数据导入

load data local inpath '/root/txt/map.txt' into table t_map;

2.4 str_to_map 数据插入
  hive 函数地址
    https://cwiki.apache.org/confluence/display/Hive/LanguageManual+UDF

 
insert into table t_map select 4,'zl',`str_to_map`('唱歌:喜欢,跳舞:喜欢') from t_b limit 1;

insert into table t_map select 5,'qb',str_to_map('唱歌=喜欢&跳舞=喜欢','&','=') from t_b limit 1;

 
3.struct文本批量导入和named_struct 数据插入

3.1数据准备
  1,zhangsan,china-beijing
  2,lisi,USA-newyork

3.2表的创建

create external table t_struct(id int,name string,address struct<country:string,city:string>)
row format delimited
fields terminated by ','
collection items terminated by '-';

3.3数据导入

  load data local inpath '/root/txt/struct.txt' into table t_struct;

3.4 named_struct 数据插入

insert into table t_struct select 3,'Bala',named_struct('country','Tampa','city','FL')  from t_b limit 1;

原文地址:https://www.cnblogs.com/pengpenghuhu/p/14714560.html