0. 说明
Hive 插入数据的方法 && Hive 插入数据的顺序 && 插入复杂数据的方法 && load 命令详解
1. Hive 插入数据的方法
Hive 插入数据不是使用 insert,而是 load
2. Hive 插入数据的顺序
2.1 先定义好表结构
create table employee(name string,
work_place array<string>,
sex_age struct<sex:string, age:int>,
score map<string, int>,
depart_title map<string, string>)
row format delimited
fields terminated by '|'
collection items terminated by ','
map keys terminated by ':'
lines terminated by '\n'
stored as textfile;
2.2 准备数据。数据格式要和表结构对应 employee.txt
Michael|Montreal,Toronto|Male,30|DB:80|Product:Developer
Will|Montreal|Male,35|Perl:85|Product:Lead,Test:Lead
Shelley|New York|Female,27|Python:80|Test:Lead,COE:Architect
Lucy|Vancouver|Female,57|Sales:89,HR:94|Sales:Lead
2.3 空表中使用 load 命令加载数据
load data local inpath '/home/centos/files/employee.txt' into table employee;
2.4 取出所有的成员
# array获取
select name ,work_place[] from employee;
# 结构体获取
select name ,sex_age.sex from employee;
# map成员获取
select name, score['Python'] from employee;
3. 插入复杂类型数据 insert
3.0 设置显示表头
临时修改命令如下,永久修改需要修改配置文件 hive-site.xml
set hive.cli.print.header=true;
3.1 插入复杂类型使用转储
insert xxx select xxx
通过 select 语句构造出 array 类型
# 通过 select 语句构造出 array 类型
select array('tom','tomas','tomson') ; # 转储 array 类型数据
insert into employee(name,work_place) select 'tom',array('beijing','shanghai','guangzhou');
通过 select 语句构造出 map 类型
# 通过 select 语句构造出 map 类型
select map('bigdata',100); # 转储 map 类型数据
insert into employee(name,score) select 'tomas', map('bigdata',100);
通过 select 语句构造出 struct 类型
# 通过 select 语句构造出 struct 类型
select struct('male',10);
select named_struct('sex','male','age',10); # 转储 struct 类型数据
insert into employee(name,sex_age) select 'tomson',named_struct('sex','male','age',10);
4. load命令详解
4.0 前提:先建表
create table duowan(id int, name string, pass string, mail string, nickname string)
row format delimited
fields terminated by '\t'
lines terminated by '\n'
stored as textfile;
4.1 使用 load
# load 本地数据,相当于上传或者复制,源文件不变
load data local inpath '/home/centos/files/employee.txt' into table employee; # load hdfs 数据,相当于移动
load data inpath '/duowan_user.txt' into table duowan; # load 本地数据 + 覆盖原始数据
load data local inpath '/home/centos/files/employee.txt' overwrite into table employee; # load hdfs 数据 + 覆盖原始数据
load data inpath '/duowan_user.txt' overwrite into table duowan;