Hive之DML数据操作

1. 加载数据

1.Load方式加载数据

load  data  [local]  inpath  '/opt/module/datas/student.txt'  [overwrite]  into  table  student [partition (partcol1=val1,...)];
(1)load data:表示加载数据
(2)local:表示从本地加载数据到hive表;否则从HDFS加载数据到hive表
(3)inpath:表示加载数据的路径
(4)overwrite:表示覆盖表中已有数据,否则表示追加
(5)into table:表示加载到哪张表
(6)student:表示具体的表
(7)partition:表示上传到指定分区
  • 注意:从本地加载数据后本地数据还在,从HDFS加载数据后HDFS上的数据就不在了。

2. insert方式

insert overwrite table student partition(month='201708')
select id, name from student where month='201709';

(根据多张表查询结果)
from student
insert into table student partition(month='201707')
select id, name where month='201709'
insert into table student partition(month='201706')
select id, name where month='201709';

3.查询语句中创建表并加载数据

create table if not exists student3
as selectid, name from student;

4.创建表时通过Location指定加载数据路径

# 上传数据到HDFS
dfs -put /usr/ywq/datas/student.txt /user/ywq;
# 创建表
create table student7 like student0 location '/user/ywq';

5.Import数据到指定Hive表中

  • 注意:先用export导出后,再将数据导入。
import table student2 partition(month='201709') from '/user/hive/warehouse/export/student';

2. 数据导出

1. insert导出

  • 将查询的结果导出到本地
insert overwrite local directory '/opt/module/datas/export/student' select * from student;

  • 将查询的结果格式化导出到本地
insert overwrite local directory '/opt/module/datas/export/student1' ROW FORMAT DELIMITED FIELDS TERMINATED BY '\t'
select * from student;
  • 将查询的结果导出到HDFS上(没有local)
 insert overwrite directory '/user/atguigu/student2' ROW FORMAT DELIMITED FIELDS TERMINATED BY '\t' 
 select * from student;

2. Hadoop命令导出到本地

dfs -get/user/hive/warehouse/student/month=201709/000000_0   /opt/module/datas/export/student3.txt

3. Hive Shell 命令导出

hive -e "select * from test" >> res.csv
 
 
或者:
hive -f sql.hql >> res.csv
 
其中文件sql.hql内容为查询语句

4. Export导出到HDFS上

export table default.student to '/user/hive/warehouse/export/student';

3.清空表数据(Truncate)

注意:Truncate只能删除管理表,不能删除外部表数据

truncate table 表名;

猜你喜欢

转载自blog.csdn.net/qq_41725214/article/details/86569180