目录
数据导入
装载数据(Load)
语法:
load data [local] inpath 'file_path' [overwrite] into table tb_name [partition(partcol1=val1,...)]
-
load data:表示加载数据
-
local:表示从本地加载数据到 hive 表;否则从 HDFS 加载数据到 hive 表
-
inpath:表示加载数据的路径
-
overwrite:表示覆盖表中已有数据,否则表示追加
-
into table:表示加载到哪张表
-
tb_name:表示具体的表
-
partition:表示上传到指定分区
案例,加载本地数据到student表中:
hive> load data local inpath '/opt/module/datas/student.txt' into table student_part partition(dt='201912');
Loading data to table iceink.student_part partition (dt=201912)
Partition iceink.student_part{dt=201912} stats: [numFiles=2, numRows=0, totalSize=96, rawDataSize=0]
OK
通过查询语句向表中插入数据(Insert)
-- 基本插入数据
insert into table student_part partition(dt='201909') values(1,'posion',20);
-- 根据单张表查询结果
insert overwrite table student_part partition(dt='201909') select id,name,age from student_part where dt = '201911';
查询语句中创建表并加载数据(As Select)
create table if not exists student_copy2 as select id,name,age from student;
数据导出
数据导出有很多种方式,如:Insert导出、Hadoop命令导出、Hive Shell导出、Export导出、Sqoop导出
下面主要介绍Insert导出。
-- 将查询结果导出到本地
insert overwrite local directory '/opt/module/datas/student' select * from student;
结果数据:
1iceink18
2icydate18
-- 将查询结果格式化导出到本地
insert overwrite local directory '/opt/module/datas/student' row format delimited fields terminated by '\t' select * from student;
结果数据:
1 iceink 18
2 icydate 18
-- 将查询结果导出到HDFS上,没有local
insert overwrite directory '/opt/module/datas/student' select * from student;
清除表中数据
语法: truncate table tb_name;
注意:
Truncate 只能删除管理表,不能删除外部表中数据
hive> truncate table student_external; FAILED: SemanticException [Error 10146]: Cannot truncate non-managed table student_external.