Hive加载数据的几种方式

1、load data 加载数据方式

2、insert 方式进行加载数据

3、from table 多重插入数据方式

不多bb,主要介绍一下,多重插入数据方式

1、load data 方式

load装载数据  

LOAD DATA [LOCAL] INPATH 'filepath' [OVERWRITE] INTO TABLE tablename [PARTITION (partcol1=val1, partcol2=val2 ...)]

说明: Load 操作只是单纯的复制/移动操作,将数据文件移动到 Hive 表对应的位置。
filepath:   相对路径,例如: project/data1                
            绝对路径,例如: /user/hive/project/data1
            包含模式的完整 URI,列如:hdfs://namenode_host:9000/user/hive/project/data1
local       关键字 如果指定了 LOCAL, load 命令会去查找本地文件系统中的 filepath。如果没有指定 LOCAL 关键字,则根据 inpath 中的 uri 查找文件
    注意:uri 是指 hdfs 上的路径,分简单模式和完整模式两种,例如:
        简单模式: /user/hive/project/data1
        完整模式: hdfs://namenode_host:9000/user/hive/project/data1

overwrite 关键字 如果使用了 OVERWRITE 关键字,则目标表(或者分区)中的内容会被删除,然后再将 filepath 指向的文件/目录中的内容添加到表/分区中。
          如果目标表(分区)已经有一个文件,并且文件名和 filepath 中的文件名冲突,那么现 有的文件会被新文件所替代。

2、insert 方式

1、插入单条数据:(需要开启hive事务哦~ ) 点我了解下

insert into table test values('10','xx','beijing',28);

2、批量插入数据(利用查询语句将结果导入新表)

INSERT OVERWRITE [INTO] TABLE table_name [PARTITION (partcol1=val1, partcol2=val2 ...)] select * from test ;

3、分区插入

分区插入有两种,一种是静态分区,另一种是动态分区。如果混合使用静态分区和动态分区, 则静态分区必须出现在动态分区之前。现分别介绍这两种分区插入。
     静态分区:创建静态分区表、从查询结果中导入数据、查看插入结果
     动态分区:静态分区需要创建非常多的分区,那么用户就需要写非常多的 SQL! Hive 提供了一个动态分 区功能,其可以基于查询参数推断出需要创建的分区名称。
静态分区不做特殊讲解,因为和上面insert 方式类似。
动态分区表数据插入:
    3-1、创建分区表,和创建静态分区表是一样的
    3-2、参数设置
        hive> set hive.exec.dynamic.partition=true;
        hive> set hive.exec.dynamic.partition.mode=nonstrict;
    注意: 动态分区默认情况下是没有开启的。开启后,默认是以”严格“模式执行的,在这种模式下要求至少有一列分区字段是静态的。
          这有助于阻止因设计错误导致查询产生大量的分区。但是此处我们不需要静态分区字段,估将其设为 nonstrict。
    3-3、动态数据插入示例 (partition字段必须出现在select字段的最后)
        insert into table test2 partition (age) select name,address,school,age from students;
    注意:查询语句 select 查询出来的 age 字段必须放在最后,和分区字段对应,不然结果会出错

    3-4、完整示例

-- Mock数据
create table t_test_p_source (
    id string,
    name string,
    birthday string
)
row format delimited fields terminated by '\t'
stored as textfile;
 
insert into t_test_p_source values ('a1', 'zhangsan', '2018-01-01');
insert into t_test_p_source values ('a2', 'lisi', '2018-01-02');
insert into t_test_p_source values ('a3', 'zhangsan', '2018-01-03');
insert into t_test_p_source values ('a4', 'wangwu', '2018-01-04');
insert into t_test_p_source values ('a5', 'sanzang', '2018-01-05');
insert into t_test_p_source values ('a6', 'zhangsan2', '2018-01-01');


-- 建立一张分区表 (按ds字段分区)
create table t_test_p_target (
    id string,
    name string
)
partitioned by (ds string)
row format delimited fields terminated by '\t'
stored as textfile;

-- 动态分区插入数据
-- 是否开启动态分区,默认是false,所以必须要设置成true
SET hive.exec.dynamic.partition=true;

-- 动态分区模式,默认为strict, 表示表中必须一个分区为静态分区,nostrict表示允许所有字段都可以作为动态分区   
SET hive.exec.dynamic.partition.mode=nonstrict;    

-- 插入数据语法
insert into table t_test_p_target partition (ds) select id, name, birthday as ds from t_test_p_source;

    3-5、case(create table ... as  select..)

在实际情况中,表的输出结果可能太多,不适于显示在控制台上,这时候,将 Hive 的查 询输出结果直接存在一个新的表中是非常方便的,我们称这种情况为 CTAS
       示例:

CREATE TABLE mytest AS SELECT name, age FROM test;

       注意:CTAS 操作是原子的,因此如果 select 查询由于某种原因而失败,新表是不会创建 的!

3、from table 多重插入数据方式

为什么需要多重插入?我们可以先看看单个插入的效果
假如有一个需求,从t_day01中筛选出不同的数据插入到另外两张表中。

insert overwrite table t_day01_lt_400 partition(day='1')
select ip,url,staylong from t_day01 where staylong<400;

insert overwrite table t_day01_gt_400 partition(day='1')
select ip,url,staylong from t_day01 where staylong>400;

但是以上实现方式有一个弊端,两次筛选job,要分别启动两次mr过程,要对同一份源表数据进行两次读取
如果使用多重插入语法,则可以避免上述弊端,提高效率,源表只要读取一次即可。

from t_day01
insert overwrite  table t_day01_lt_400 partition(day='2')
select ip,url,staylong where staylong < 400
insert overwrite table t_day01_gt_400 partition(day='2')
select ip,url,staylong where staylong > 400;

 

今天新学的,查了一下资料分享一下~  

我遇到的场景是什么呢?就是针对uuid做了主键的表,怎么把id进行均匀的划分到不同分区里面~。 

from xxx_table_name PARTITION (ds = '20190827')
insert overwrite table table1 PARTITION (ds = '0') select name where crc32(cust_id)%4 == 0
insert overwrite table table1 PARTITION (ds = '1') select name where crc32(cust_id)%4 == 1
insert overwrite table table1 PARTITION (ds = '2') select name where crc32(cust_id)%4 == 2
insert overwrite table table1 PARTITION (ds = '3') select name where crc32(cust_id)%4 == 3

详细的加载数据方式和测试用例,可以参考如下链接哦~ (虽然排版不咋滴,内容还可以)