Hive: 创建分区表(partition表)及分区表导入csv文本文件数据

版权声明:本文为博主原创文章,未经博主允许不得转载。 https://blog.csdn.net/m0_37793798/article/details/84330644

2018.11.21

文章目录

前言

某项目生产环境中的Hive是按月份分区,而测试环境的没有分区,导致部分功能无法验证。

方法

基本思路:分别创建两个表,一张是分区表,另一张是非分区表,表结构相同;再通过insert语句将非分区表的数据插入到分区表1

要注意是,分区表的插入分两种:静态插入和动态插入。在一般情况下,Hive不建议直接使用动态插入2,所以有个默认情况下是不允许使用动态分区插入:hive.exec.dynamic.partition=false;但在Hive 0.9.0及之后的版本,上述参数默认为true,虽然如此却有另一个参数约束着动态分区插入:hive.exec.dynamic.partition.mode=strict,动态分区插入的模式默认为严格模式,在严格模式下,插入操作需要指定一个特定的静态分区。所以如果在从非分区表select出来并insert到分区表中,就需要设置这两个参数。

# 创建分区表
create table table_partitioned (col1 string, col2 int) 
  partitioned by (month int) 
  row format delimited fields terminated by ',' lines terminated by '\n' 
  stored as textfile;
# 创建非分区表
create table table_unpartitioned (col1 string, col2 int, month int) 
  row format delimited fields terminated by ',' lines terminated by '\n' 
  stored as textfile;
# 导入csv文本文件
load data local inpath '/path/to/file' into table table_unpartitioned;
# 设置允许动态分区插入,并动态分区插入模式为非严格模式
SET hive.exec.dynamic.partition = true;
SET hive.exec.dynamic.partition.mode = nonstrict;
# 插入到分区表
insert into table table_partitioned partition(month) select * from table_unpartitioned;

  1. Cloudera: create a partitioned table ↩︎

  2. Wiki Hive tutorial ↩︎

猜你喜欢

转载自blog.csdn.net/m0_37793798/article/details/84330644