【大数据】Hive 中的批量数据导入

Hive 中的批量数据导入

在博客【大数据】Hive 表中插入多条数据 中,我简单介绍了几种向 Hive 表中插入数据的方法。然而更多的时候,我们并不是一条数据一条数据的插入,而是以批量导入的方式。在本文中,我将较为全面地介绍几种向 Hive 中批量导入数据的方法。

1.从本地文件系统加载(load)数据

load data [local] inpath '路径' [overwrite] into table 表名 [partition (分区字段=,)];
  • overwrite:表示覆盖表中已有数据,否则表示追加。
  • 此种加载方式是数据的复制。

(1)创建一张表。

hive (default)> create table student(id string, name string) 
                row format delimited fields terminated by '\t';

(2)加载本地文件到 Hive。

hive (default)> load data local inpath '/opt/module/datas/student.txt' 
                into table default.student;

2.从 HDFS 文件系统加载(load)数据

从 HDFS 文件系统向表中加载数据,其实就是一个移动文件的操作,需要提前将数据上传到 HDFS 文件系统。

(1)上传文件到 HDFS(Linux 本地 /opt/module/datas/student.txt 文件传到 /user/victor/hive 目录)。

hive (default)> dfs -put /opt/module/datas/student.txt /user/victor/hive;

(2)从 HDFS 文件系统向表中加载数据。

hive (default)> load data inpath '/user/victor/hive/student.txt' 
                into table default.student;

3.通过 as select 向表中插入数据

hive (default)> create table if not exists student3 as select id, name from student;

4.通过 insert into 向表中插入数据

insert into table test [partition(partcol1=val1, partcol2=val2 ...)] select id,name from student;
  • insert into:以追加数据的方式插入到表或分区,原有数据不会删除。
insert overwrite table test [partition(partcol1=val1, partcol2=val2 ...)] select id,name from student;
  • insert overwrite:覆盖表中已存在的数据。

(1)创建一张分区表。

hive (default)> create table student(id string, name string) 
				partitioned by (month string) 
				row format delimited fields terminated by '\t';

(2)基本插入数据。

hive (default)> insert into table student partition(month='201801') 
				values('1004','wangwu');

(3)基本模式插入(根据单张表查询结果)。

hive (default)> insert overwrite table student partition(month='201802') 
				select id, name from student where month='201801';

(4)多插入模式(只需要扫描一遍源表就可以生成多个不相交的输出)。

hive (default)> from student
             	insert overwrite table student partition(month='201803')
             	select id, name where month='201801'
             	insert overwrite table student partition(month='201804')
             	select id, name where month='201801';

5.通过 location 的方式

直接将数据文件上传到 location 指定的 HDFS 的目录下;

(1)创建表,并指定在 HDFS 上的位置。

hive (default)> create external table student(id int, name string)
             	row format delimited fields terminated by '\t'
             	location '/user/hive/warehouse/student';

(2)上传数据到 HDFS 上。

hive (default)> dfs -mkdir -p /user/hive/warehouse/student;
hive (default)> dfs -put /opt/module/datas/student.txt /user/hive/warehouse/student;

(3)查询数据。

select * from student;
  • 6
    点赞
  • 14
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 1
    评论
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

G皮T

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值