Hive分区表实战

来源:www.ithao123.cn 作者:网友

1. Hive分区表

  • Partition和Bucket,为了提升查询效率,前者是粗粒度的划分,后者是细粒度的划分。

  • 建表语句中使用partitioned by指定分区字段

  • 分区表有静态分区和动态分区两种。若分区的值是确定的,那么称为静态分区字段,反之,若分区的值是非确定的,那么称之为动态分区字段。默认是采用静态分区。

2. 静态分区

应用场景1

每天有很多不同的商店各自会产生成百上千的销售记录,当天的数据当天加载。那么这是一个最简单的示例,每天产生的这些销售记录中,某些客户的销售额如何插入到已有的数仓的表中去。日期是确定的。

createexternaltableifnotexists sp_trans(
cust_no stringcomment'客户号',
shop_no stringcomment'商铺号',
trans_amt doublecomment'交易额',
trans_date stringcomment'交易日期',
etl_ts timestampcomment'etl时间戳')
partitioned by (dt string)
rowformatdelimitedfieldsterminatedby',';
/* 将数据插入到某一个确定的日起分区中去 */
insertinto sp_trans partition (dt='2016-01-13')
select * fromtransactionwhere trans_date='2016-01-13';

应用场景2

加入一个分区字段,商铺号。分区字段之间具有层级关系。

createexternaltableifnotexists sp_trans2(
cust_no stringcomment'客户号',
shop_no stringcomment'商铺号',
trans_amt doublecomment'交易额',
trans_date stringcomment'交易日期',
etl_ts timestampcomment'etl时间戳')
partitioned by (dt string,shop string)
rowformatdelimitedfieldsterminatedby',';
insertinto sp_trans2 partition (dt='2016-01-13',shop='9502')
select * fromtransactionwhere trans_date='2016-01-13'and shop_no='9502';
insertinto sp_trans2 partition (dt='2016-01-13',shop='9507')
select * fromtransactionwhere trans_date='2016-01-13'and shop_no='9507';
insertinto sp_trans2 partition (dt='2016-01-14',shop='9502')
select * fromtransactionwhere trans_date='2016-01-14'and shop_no='9502';

应用场景3

每天按照不同的商铺对交易额进行统计,这里统计的实体是商铺,同样的,日期是固定的。

createexternaltableifnotexists sp_shop_daily(
shop_no string,
trans_sum doublecomment'交易额统计',
etl_ts timestamp)
partitioned by (shop string,dt string)
rowformatdelimitedfieldsterminatedby',';
/*插入汇总后的数据到最终的统计结果所指定的日期分区中去*/
insertinto sp_shop_daily partition (shop='9502',dt='2016-01-13')
select shop_no,sum(trans_amt),current_timestamp() fromtransactionwhere shop_no='9502'and trans_date='2016-01-13'groupby shop_no;

2. 动态分区

应用场景1

每天有很多不同的商店各自会产生成百上千的销售记录,当天的数据当天加载。那么这是一个最简单的示例,每天产生的这些销售记录中,某些客户的销售额如何插入到已有的数仓的表中去。日期是非确定的。 
动态分区表的插入原则,分区字段的取值字段放在select子句的最后面

createexternaltableifnotexists dp_trans(
cust_no stringcomment'客户号',
shop_no stringcomment'商铺号',
trans_amt doublecomment'交易额',
trans_date stringcomment'交易日期',
etl_ts timestampcomment'etl时间戳')
partitioned by (dt string)
rowformatdelimitedfieldsterminatedby',';
insertinto dp_trans partition (dt)
select *,trans_date fromtransaction;

  • Error 1 
    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;

  • Error2 
    Caused by: org.apache.hadoop.hive.ql.metadata.HiveFatalException: [Error 20004]: Fatal error occurred when node tried to create too many dynamic partitions. The maximum number of dynamic partitions is controlled by hive.exec.max.dynamic.partitions and hive.exec.max.dynamic.partitions.pernode. Maximum was set to 100 partitions per node, number of dynamic partitions on this node: 101 
    set hive.exec.max.dynamic.partitions.pernode=2000;

应用场景2

加入一个分区字段,商铺号。分区字段之间具有层级关系。

createexternaltableifnotexists dp_trans2(
cust_no stringcomment'客户号',
shop_no stringcomment'商铺号',
trans_amt doublecomment'交易额',
trans_date stringcomment'交易日期',
etl_ts timestampcomment


相关文档推荐

腾讯大数据基于StarRocks的向量检索探索.PDF

1737425434 赵裕隆 3.48MB 34页 积分6

B站一站式大数据集群管理平台.PDF

1737421412 刘明刚 1.37MB 30页 积分6

StarRocks在爱奇艺大数据场景的实践.PDF

1737365327 林豪 3.57MB 27页 积分5

农业农村大数据平台互联互通规范.PDF

1736163860  0.49MB 11页 积分5

工业大数据管理与治理智能制造的基座.PDF

1733702095 王宏志 3.83MB 54页 积分6

中国出海互联网公司数据保护合规对策.PDF

1732603379  2.22MB 14页 积分4

数据跨境现状调查与分析报告.PDF

1732603016  1.98MB 53页 积分5

企业数据合规指引个人信息保护指引.PDF

1732591271  15.22MB 23页 积分5

相关文章推荐

大数据开发流程及规范

网络收集 CIO之家的朋友 

大数据技术名词解释

51CTO CIO之家的朋友 

大数据常见问题之数据倾斜

CSDN CIO之家的朋友 

详解大数据批流处理中的两大架构

数仓宝贝库 韩锐、刘驰 

什么是大数据战略以及如何构建

51CTO CIO之家的朋友 

大数据的数据治理与应用场景

51CTO CIO之家的朋友