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

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

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

2. 静态分区

应用场景1

每天有很多不同的商店各自会产生成百上千的销售记录,当天的数据当天加载。那么这是一个最简单的示例,每天产生的这些销售记录中,某些客户的销售额如何插入到已有的数仓的表中去。日期是确定的。create external table if not exists sp_trans(cust_no string comment '客户号',shop_no string comment '商铺号',trans_amt double comment '交易额',trans_date string comment '交易日期',etl_ts timestamp comment 'etl时间戳')partitioned by (dt string)row format delimited fields terminated by ',';/* 将数据插入到某一个确定的日起分区中去 */insert into sp_trans partition (dt='2016-01-13')select * from transaction where trans_date='2016-01-13';

应用场景2

加入一个分区字段,商铺号。分区字段之间具有层级关系。create external table if not exists sp_trans2(cust_no string comment '客户号',shop_no string comment '商铺号',trans_amt double comment '交易额',trans_date string comment '交易日期',etl_ts timestamp comment 'etl时间戳')partitioned by (dt string,shop string)row format delimited fields terminated by ',';insert into sp_trans2 partition (dt='2016-01-13',shop='9502')select * from transaction where trans_date='2016-01-13' and shop_no='9502';insert into sp_trans2 partition (dt='2016-01-13',shop='9507')select * from transaction where trans_date='2016-01-13' and shop_no='9507';insert into sp_trans2 partition (dt='2016-01-14',shop='9502')select * from transaction where trans_date='2016-01-14' and shop_no='9502';

应用场景3

每天按照不同的商铺对交易额进行统计,这里统计的实体是商铺,同样的,日期是固定的。create external table if not exists sp_shop_daily(shop_no string,trans_sum double comment '交易额统计',etl_ts timestamp)partitioned by (shop string,dt string)row format delimited fields terminated by ',';/*插入汇总后的数据到最终的统计结果所指定的日期分区中去*/insert into sp_shop_daily partition (shop='9502',dt='2016-01-13')select shop_no,sum(trans_amt),current_timestamp() from transaction where shop_no='9502' and trans_date='2016-01-13' group by shop_no;

2. 动态分区

应用场景1

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

动态分区表的插入原则,分区字段的取值字段放在select子句的最后面create external table if not exists dp_trans(cust_no string comment '客户号',shop_no string comment '商铺号',trans_amt double comment '交易额',trans_date string comment '交易日期',etl_ts timestamp comment 'etl时间戳')partitioned by (dt string)row format delimited fields terminated by ',';insert into dp_trans partition (dt)select *,trans_date from transaction;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

加入一个分区字段,商铺号。分区字段之间具有层级关系。create external table if not exists dp_trans2(cust_no string comment '客户号',shop_no string comment '商铺号',trans_amt double comment '交易额',trans_date string comment '交易日期',etl_ts timestamp comment 'etl时间戳')partitioned by (dt string,shop string)row format delimited fields terminated by ',';insert into dp_trans2 partition (shop,dt)select *,shop_no,trans_date from transaction;

应用场景3

每天按照不同的商铺对交易额进行统计,这里统计的实体是商铺,同样的,日期是非固定的。create external table if not exists dp_shop_daily(shop_no string,trans_sum double comment '交易额统计',etl_ts timestamp)partitioned by (shop string,dt string)row format delimited fields terminated by ',';insert into dp_shop_daily partition(shop,dt)select shop_no,sum(trans_amt),current_timestamp(),shop_no,trans_date from transaction group by shop_no,trans_date;

注意,当分区字段包含动态和静态分区字段两种时,动态分区字段不能作为静态分区字段的父级

3. 修改分区

1. 添加分区alter table dp_trans2 add partition (dt='2016-01-13',shop='001');

2. 重命名alter table sp_trans2 partition (dt='2016-01-13',shop='001') rename to partition (dt='2016-01-13',shop='000');

3. 交换分区alter table sp_trans2 exchange partition (dt='2016-01-13',shop='9510') with table dp_trans2;

4. 恢复分区hive> msck repair table sp_trans2;FAILED: Execution Error, return code 1 from org.apache.hadoop.hive.ql.exec.DDLTask在Hive v2.1.1修复了此问题,可下载最新版本使用

5. 删除分区alter table sp_trans2 drop partition(dt='2016-01-13',shop='9510');

hive 分区表select全部数据_Hive分区表实战相关推荐

  1. hive 分区表select全部数据_hive分区表

    内部表和外部表 内部表:create table,copy数据到warehouse,删除表时数据也会删除 外部表:create external table,不copy数据到warehouse,删除表 ...

  2. hive 分区表select全部数据_Hive分区表的分区操作

    为了对表进行合理的管理以及提高查询效率,Hive可以将表组织成"分区".一个分区实际上就是表下的一个目录,一个表可以在多个维度上进行分区,分区之间的关系就是目录树的关系. 1.创建 ...

  3. hive 分区表select全部数据_hive 查询表数据量大小

    为什么要查询表数据量 在做数据仓库管理时,数据导入hive或向表生成数据后形成的数据资产,表里的数据量和占用存储空间是总要的元数据属性.为方便数据使用时对数据有基本的了解,需要对表的数据量做统计. 使 ...

  4. hive 分区表select全部数据_Hive中如何快速的复制一张分区表(包括数据)

    关键字:Hive 复制表 Hive中有时候会遇到复制表的需求,复制表指的是复制表结构和数据. 如果是针对非分区表,那很简单,可以使用CREATE TABLE new_table AS SELECT * ...

  5. hive 指定字段插入数据_Hive 表之间数据处理,Int 类型字段部分字段出现 NULL情况...

    背景 hive 中有一张待处理的分区表,存储的方式是parquet,处理之后的目标表是一张非分区的外部表,并且分隔方式为 ",". 问题 部分记录的 int 类型字段 出现 nul ...

  6. hive sql插入一行数据_Hive查询某一重复字段记录第一条数据

    场景:数据库中id.toapp.topin.toclienttype几个字段都相同,receivetime字段不一样,现需要将receive最小的一行查出,其他行舍去. select * from ( ...

  7. oracle数据泵导入分区表,Oracle 10g 数据泵分区表的导出

    Oracle 10g中数据泵支持本地导出/导入分区表的分区,但是NET_LINK不支持分区表的分区.以下为导出部分实验,导入与导出相识. 本地导出: C:\Users\xionglang>exp ...

  8. hive 修改cluster by算法_Hive入门实战(一)概述与使用

    一.Hive基本概念 1.概念 Hive:由Facebook开源用于解决海量结构化日志的数据统计. Hive是基于Hadoop的一个数据仓库工具,可以将结构化的数据文件映射为一张表,并提供类SQL查询 ...

  9. hive 删除分区、写数据到分区表

    1.删除分区 alter table test_table drop partition(id='123',dt='2022-12-02'); 2.写数据到分区表 insert into test_t ...

最新文章

  1. 如何组织成功的bug bash--摘录
  2. PacBio三代测序
  3. 边缘检测:Sobel、拉普拉斯算子
  4. Ugly Pairs
  5. 在Linux中创建静态库和动态库 (转)
  6. Python -- sys模块
  7. XHTML的使用规范
  8. 呼叫中心客服交流三大法宝
  9. D类IP地址和组播传输
  10. c++ 游戏_C/C++编程笔记:C语言实现连连看游戏,项目源码分享
  11. 批处理控制VMware Workstation服务
  12. 计算机辅助动力学第五讲作业,计算机辅助动力学第五讲作业
  13. AT89C51单片机制作简易密码锁
  14. Linux 杀毒软件ClamAV离线安装部署
  15. [ROS] KDL + DH 参数 + 正解
  16. android动态权限依赖库,动态申请app权限:郭霖大神的PermissionX库带你告别原生
  17. 一部保护基本权利的欧盟人工智能法:一份民间机构的声明
  18. 利用洪特规则计算原子离子磁矩过程详解
  19. PCIe系列第四讲、TLP的路由方式
  20. python 12306登录_Python 12306登陆详细分析及操作

热门文章

  1. 建立一个程序员相亲圈子可好
  2. 图像分割 | 人体实例分割数据
  3. c语言定义一个字符型变量,C语言-字符与字符串常量及变量
  4. 010 MATLAB奇淫技巧之实现图片扫描
  5. 车脉科技:业内首创“车企体验式营销“
  6. 【android】Glut glRotatef glTranslatef示例程序:
  7. 1至2019中,有多少个数的数位中包含数字9
  8. Fortran学习5:数组2
  9. java cookie实现自动登录
  10. 通过修改Cookie登录后台