5.Hive系列之DML数据操作(一)

我们本章来了解下数据导入导出的一些操作

# 加载本地文件到hive
load data local inpath '/opt/hive/datas/student.txt' into table default.student;
# 加载HDFS文件到hive
load data inpath '/user/hive/student.txt' into table default.student;
# 加载数据覆盖表中已有的数据
load data inpath '/user/hive/student.txt' overwrite into table default.student;
# 基本模式插入(根据单张表查询结果) insert 不支持插入部分字段
insert overwrite table student_bak select id, name from student;
# 多表(多分区)插入模式(根据多张表查询结果)
from student insert overwrite table student partition(month='202307') select id, name where month='202307' insert overwrite table student partition(month='202308') select id, name where month='202308';
# 根据查询结果创建表
create table if not exists student2 as select id, name from student;
# 通过 Location 指定加载数据路径
create external table if not exists student3(id int, name string) row format delimited fields terminated by '\t' location '/student';
# Import 数据到指定 Hive 表中, 注意:先用 export 导出后,再将数据导入
import table student4 from '/user/hive/warehouse/export/student';
# 将查询的结果导出到本地
insert overwrite local directory '/opt/module/hive/data/export/student' select * from student;
# 将查询的结果格式化导出到本地
insert overwrite local directory '/opt/module/hive/data/export/student1' ROW FORMAT DELIMITED FIELDS TERMINATED BY '\t' select * from student;
# 将查询的结果导出到 HDFS 上(没有 local)
insert overwrite directory '/user/student2' ROW FORMAT DELIMITED FIELDS TERMINATED BY '\t' select * from student;
# Hadoop 命令导出到本地
dfs -get /user/hive/warehouse/student/student.txt /opt/module/data/export/student3.txt;
# Hive Shell 命令导出
hive -e 'select * from default.student;' /opt/module/hive/data/export/student4.txt;
# Export 导出到 HDFS 上
export table default.student to '/user/hive/warehouse/export/student';
# Truncate 只能删除管理表,不能删除外部表中数据
truncate table student;

猜你喜欢

转载自blog.csdn.net/SJshenjian/article/details/131544660