Hive数据加载方式(load、insert;普通表、分区表)

前言

介绍Hive数据加载方式(insert、load)


方式一:load data

基础语法:
load data [local] inpath '/opt/module/datas/student.txt' [overwrite] into table student[partition ]

参数说明:
1 load data: 表示加载数据
2 local: 表示从本地加载数据到 hive 表;否则从 HDFS 加载数据到 hive 表
3 inpath: 表示加载数据的路径
相对路径,例如:project/data1
绝对路径,例如:/user/hive/project/data1
包含模式的完整 URI,如:hdfs://namenode:9000/user/hive/project/data1
4 overwrite: 表示覆盖表中已有数据,否则表示追加。目标表(或者分区)中的内容会被删除,然后再将filepath指向的文件/目录中的内容添加到表/分区中
5 into table: 表示加载到哪张表
6 student: 表示具体的表
7 partition: 表示上传到指定分区

-- 加载本地文件
load data local inpath '/home/hadoop/load1.txt' into table tb_load1;

-- 加载HDFS文件
load data inpath '/hive/test/load2.txt' into table tb_load1;

-- 加载分区数据
load data inpath '/hive/test/load_part_male.txt' into table tb_load2 
partition (sex='male');

--使用overwrite:会覆盖之前的数据
load data local inpath '/home/hadoop/load3.txt' overwrite into table tb_load1;

方式二: insert 插入

1.普通表

-- 覆盖 
insert overwrite table tb_insert1 select id,name from tb_select1;
-- 追加
insert into table tb_insert1 select id,name from tb_select1;

2.分区表

-- 分区插入
insert overwrite table tb_insert_part partition(sex = 'male')
select id,name from tb_select1 where sex='male';

-- 动态分区插入(需先设置非严格模式)
set hive.exec.dynamic.partition.mode=nonstrict;

insert overwrite table tb_dy_part partition(sex) 
select id,name,sex from tb_select1;

方式三:as select

注意: 只能以as方式加载数据, 如其他有分区字段, 分区字段只以字段形式保留

create table tb_create_mode as 
select id,name from tb_select1;

数据导出

(1)导出到本地

insert overwrite local directory '/home/hadoop/'
select id,name from tb_select1;

例子 :

INSERT overwrite directory "/user/yuanpengfei/ypf/lifeng/vehPOI" ROW format delimited fields terminated BY "," 
select substr( md5(concat('mb',field_2,'xx')),9,6), field_3, field_4, field_5, field_6, field_7
from default.longchuan_od_temp

总结

如果此篇文章有帮助到您, 希望打大佬们能关注点赞收藏评论支持一波,非常感谢大家!
如果有不对的地方请指正!!!

参考1
参考2

猜你喜欢

转载自blog.csdn.net/weixin_42326851/article/details/132214145