Hive加载数据操作

Hive加载数据操作

一、load data

语法结构:

load data [local] inpath 'filepath' [overwrite] into table table_name [partition(part1=val1,part2=val2)]
说明:

1、Load 

操作只是单纯的复制/移动操作,将数据文件移动到 Hive 表对应的位置

2、filepath:

相对路径,例如:project/data1 

绝对路径,例如:/user/hive/project/data1 

包含模式的完整 URI,如:hdfs://namenode:9000/user/hive/project/data1

3local关键字

如果指定了localload命令会去查找本地文件系统中的filepath。如果没有指定local关键字,则根据inpath中的uri查找文件

4、overwrite 关键字

如果使用了overwrite关键字,则目标表(或者分区)中的内容会被删除,然后再将filepath指向的文件/目录中的内容添加到表/分区中。 

如果目标表(分区)已经有一个文件,并且文件名和 filepath 中的文件名冲突,那么现有的文件会被新文件所替代。

1.1 加载本地数据

# 创建表
create table tb_load1(id int,name string)
row format delimited fields terminated by ',';

# 加载本地数据
load data local inpath '/home/hadoop/load1.txt' into table tb_load1;

image

1.2 加载hdfs数据

load data inpath '/hive/test/load2.txt' into table tb_load1;

image

从hdfs加载数据成功后,数据会删除。

1.3 加载数据到分区表

# 创建分区表
create table tb_load2(id int ,name string)
partitioned by (sex string)
row format delimited fields terminated by ',';

# 加载数据,数据本身要是分区的数据
load data inpath '/hive/test/load_part_male.txt' into table tb_load2 partition (sex='male');

load data inpath '/hive/test/load_part_female.txt' into table tb_load2 partition (sex='female');

image

1.4 使用overwrite关键字

load data local inpath '/home/hadoop/load3.txt' overwrite into table tb_load1;

image

overwrite会覆盖之前的数据

二、insert语句插入数据

语法结构:

# 重其他表查询结果,插入并覆盖新表
insert overwrite/into table table_name
[partition(part=val,part2=val2,...)]
select fileds,... from tb_other;

多个insert语句插入语法结构:

from table_name t
insert overwrite table tb1 [partition(col=val,...)]
    select 语句
insert overwrite table tb2 [partition(col=val,...)]
    select 语句
...;

2.1 简单使用insert语句

create table tb_select1 (id int,name string,sex string)
row format delimited fields terminated by ',';

create table tb_insert1(id int,name string);

insert overwrite table tb_insert1 select id,name from tb_select1;

image

查看下tb_insert1表数据

select * from tb_insert1;

image

使用 insert into进行插入

insert into table tb_insert1 select id,name from tb_select1 limit 2;

查询结果:

image

2.2 使用insert语句分区插入

create table tb_insert_part(id int,name string)
partitioned by(sex string);

insert overwrite table tb_insert_part partition(sex = 'male')
select id,name from tb_select1 where sex='male';

image

查询结果:

select * from tb_insert_part;

image

2.3 多个insert插入

create table tb_mutil_insert1(id int,name string)
partitioned by(sex string);

create table tb_mutil_insert2(id int,name string)
partitioned by(sex string);

from tb_select1 t
insert overwrite table tb_mutil_insert1 partition (sex='male')
select t.id,t.name where t.sex='male'
insert overwrite table tb_mutil_insert2 partition (sex='female')
select t.id,t.name where t.sex='female';

image

查询结果:

image

2.4 动态分区插入

create table tb_dy_part(id int,name string) partitioned by(sex string);

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

要使用动态分区,默认是使用严格模式,需要使用分区才行,或者将动态分区模式设置为非严格模式。

FAILED: SemanticException [Error 10096]: Dynamic partition strict mode requires at least one static partition column. To turn this off set hive.exec.dynamic.partition.mode=nonstrict

设置动态分区模式为非严格模式

set hive.exec.dynamic.partition.mode=nonstrict

image

三、使用create table的方式,将查询数据插入表中
create table tb_create_mode as 
select id,name from tb_select1;

image

结果:

image

四、导出数据

4.1 单条数据导出到本地

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

image

4.2 多条数据导出到hdfs

from tb_select1 t
insert overwrite  directory '/hive/test/male'
select t.id,t.name,t.sex where t.sex='male'
insert overwrite  directory '/hive/test/female'
select t.id,t.name,t.sex where t.sex='female';

image

  • 10
    点赞
  • 39
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
Hive是一个基于Hadoop的数据仓库工具,用于处理大规模数据集。在Hive添加分区数据是指通过创建新的分区来将数据划分成更小的部分,以提高查询性能和管理数据的灵活性。 要在Hive添加分区数据,可以按照以下步骤进行操作: 1. 创建表:首先,需要创建一个Hive表来存储数据。可以使用CREATE TABLE语句定义表结构,包括列名、数据类型和分区字段等信息。 2. 添加分区:在已创建的表上,可以使用ALTER TABLE语句添加新的分区。分区字段可以是日期、地区等信息,根据实际业务需求进行选择。 3. 加载数据:在添加分区后,可以使用LOAD DATA语句将数据加载到相应的分区。可以将数据文件或目录路径作为LOAD DATA语句的参数,Hive将自动将数据放入正确的分区。 4. 查询分区数据:完成数据加载后,可以使用SELECT语句查询特定分区的数据。可以通过指定分区字段的值来选择查询特定分区的数据,以满足具体的业务需求。 在使用Hive添加分区数据时,需要注意以下几点: 1. 分区字段应该是表的一个列,它会将表数据按照分区字段的值进行分类。可以根据业务需求选择合适的分区字段。 2. 添加分区后,可以执行MSCK REPAIR TABLE语句来更新表的分区元数据。这可以确保Hive正确识别和管理所有已添加的分区。 3. 当添加大量的分区数据时,可以考虑使用动态分区的机制。动态分区允许在将数据加载到表时自动创建新的分区,无需手动指定分区。 综上所述,通过以上步骤可以在Hive添加分区数据,并根据实际需求进行查询和分析。通过合理使用分区技术,可以提高Hive的查询性能和数据管理效果。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值