1. 引入

Apache Hudi支持多种分区方式数据集,如多级分区、单分区、时间日期分区、无分区数据集等,用户可根据实际需求选择合适的分区方式,下面来详细了解Hudi如何配置何种类型分区。

2. 分区处理

为说明Hudi对不同分区类型的处理,假定写入Hudi的Schema如下

{  "type" : "record",  "name" : "HudiSchemaDemo",  "namespace" : "hoodie.HudiSchemaDemo",  "fields" : [ {    "name" : "age",    "type" : [ "long", "null" ]  }, {    "name" : "location",    "type" : [ "string", "null" ]  }, {    "name" : "name",    "type" : [ "string", "null" ]  }, {    "name" : "sex",    "type" : [ "string", "null" ]  }, {    "name" : "ts",    "type" : [ "long", "null" ]  }, {    "name" : "date",    "type" : [ "string", "null" ]  } ]}

其中一条具体数据如下

{  "name": "zhangsan",   "ts": 1574297893837,   "age": 16,   "location": "beijing",   "sex":"male",   "date":"2020/08/16"}

2.1 单分区

单分区表示使用一个字段表示作为分区字段的场景,可具体分为非日期格式字段(如location)和日期格式字段(如date)

2.1.1 非日期格式字段分区

如使用上述location字段做为分区字段,在写入Hudi并同步至Hive时配置如下

df.write().format("org.apache.hudi").                options(getQuickstartWriteConfigs()).                option(DataSourceWriteOptions.TABLE_TYPE_OPT_KEY(), "COPY_ON_WRITE").                option(DataSourceWriteOptions.PRECOMBINE_FIELD_OPT_KEY(), "ts").                option(DataSourceWriteOptions.RECORDKEY_FIELD_OPT_KEY(), "name").                option(DataSourceWriteOptions.PARTITIONPATH_FIELD_OPT_KEY(), partitionFields).                option(DataSourceWriteOptions.KEYGENERATOR_CLASS_OPT_KEY(), keyGenerator).                option(TABLE_NAME, tableName).                option("hoodie.datasource.hive_sync.enable", true).                option("hoodie.datasource.hive_sync.table", tableName).                option("hoodie.datasource.hive_sync.username", "root").                option("hoodie.datasource.hive_sync.password", "123456").                option("hoodie.datasource.hive_sync.jdbcurl", "jdbc:hive2://localhost:10000").                option("hoodie.datasource.hive_sync.partition_fields", hivePartitionFields).                option("hoodie.datasource.write.table.type", "COPY_ON_WRITE").                option("hoodie.embed.timeline.server", false).                option("hoodie.datasource.hive_sync.partition_extractor_class", hivePartitionExtractorClass).                mode(saveMode).                save(basePath);

值得注意如下几个配置项

  • DataSourceWriteOptions.PARTITIONPATH_FIELD_OPT_KEY()配置为location
  • hoodie.datasource.hive_sync.partition_fields配置为location,与写入Hudi的分区字段相同;
  • DataSourceWriteOptions.KEYGENERATOR_CLASS_OPT_KEY()配置为org.apache.hudi.keygen.SimpleKeyGenerator,或者不配置该选项,默认为org.apache.hudi.keygen.SimpleKeyGenerator
  • hoodie.datasource.hive_sync.partition_extractor_class配置为org.apache.hudi.hive.MultiPartKeysValueExtractor

Hudi同步到Hive创建的表如下

CREATE EXTERNAL TABLE `notdateformatsinglepartitiondemo`(  `_hoodie_commit_time` string,  `_hoodie_commit_seqno` string,  `_hoodie_record_key` string,  `_hoodie_partition_path` string,  `_hoodie_file_name` string,  `age` bigint,  `date` string,  `name` string,  `sex` string,  `ts` bigint)PARTITIONED BY (  `location` string)ROW FORMAT SERDE  'org.apache.hadoop.hive.ql.io.parquet.serde.ParquetHiveSerDe'STORED AS INPUTFORMAT  'org.apache.hudi.hadoop.HoodieParquetInputFormat'OUTPUTFORMAT  'org.apache.hadoop.hive.ql.io.parquet.MapredParquetOutputFormat'LOCATION  'file:/tmp/hudi-partitions/notDateFormatSinglePartitionDemo'TBLPROPERTIES (  'last_commit_time_sync'='20200816154250',  'transient_lastDdlTime'='1597563780')

查询表notdateformatsinglepartitiondemo

tips: 查询时请先将hudi-hive-sync-bundle-xxx.jar包放入$HIVE_HOME/lib下

2.1.2 日期格式分区

如使用上述date字段做为分区字段,核心配置项如下

  • DataSourceWriteOptions.PARTITIONPATH_FIELD_OPT_KEY()配置为date
  • hoodie.datasource.hive_sync.partition_fields配置为date,与写入Hudi的分区字段相同;
  • DataSourceWriteOptions.KEYGENERATOR_CLASS_OPT_KEY()配置为org.apache.hudi.keygen.SimpleKeyGenerator,或者不配置该选项,默认为org.apache.hudi.keygen.SimpleKeyGenerator
  • hoodie.datasource.hive_sync.partition_extractor_class配置为org.apache.hudi.hive.SlashEncodedDayPartitionValueExtractor

Hudi同步到Hive创建的表如下

CREATE EXTERNAL TABLE `dateformatsinglepartitiondemo`(  `_hoodie_commit_time` string,  `_hoodie_commit_seqno` string,  `_hoodie_record_key` string,  `_hoodie_partition_path` string,  `_hoodie_file_name` string,  `age` bigint,  `location` string,  `name` string,  `sex` string,  `ts` bigint)PARTITIONED BY (  `date` string)ROW FORMAT SERDE  'org.apache.hadoop.hive.ql.io.parquet.serde.ParquetHiveSerDe'STORED AS INPUTFORMAT  'org.apache.hudi.hadoop.HoodieParquetInputFormat'OUTPUTFORMAT  'org.apache.hadoop.hive.ql.io.parquet.MapredParquetOutputFormat'LOCATION  'file:/tmp/hudi-partitions/dateFormatSinglePartitionDemo'TBLPROPERTIES (  'last_commit_time_sync'='20200816155107',  'transient_lastDdlTime'='1597564276')

查询表dateformatsinglepartitiondemo

2.2 多分区

多分区表示使用多个字段表示作为分区字段的场景,如上述使用location字段和sex字段,核心配置项如下

  • DataSourceWriteOptions.PARTITIONPATH_FIELD_OPT_KEY()配置为location,sex
  • hoodie.datasource.hive_sync.partition_fields配置为location,sex,与写入Hudi的分区字段相同;
  • DataSourceWriteOptions.KEYGENERATOR_CLASS_OPT_KEY()配置为org.apache.hudi.keygen.ComplexKeyGenerator
  • hoodie.datasource.hive_sync.partition_extractor_class配置为org.apache.hudi.hive.MultiPartKeysValueExtractor

Hudi同步到Hive创建的表如下

CREATE EXTERNAL TABLE `multipartitiondemo`(  `_hoodie_commit_time` string,  `_hoodie_commit_seqno` string,  `_hoodie_record_key` string,  `_hoodie_partition_path` string,  `_hoodie_file_name` string,  `age` bigint,  `date` string,  `name` string,  `ts` bigint)PARTITIONED BY (  `location` string,  `sex` string)ROW FORMAT SERDE  'org.apache.hadoop.hive.ql.io.parquet.serde.ParquetHiveSerDe'STORED AS INPUTFORMAT  'org.apache.hudi.hadoop.HoodieParquetInputFormat'OUTPUTFORMAT  'org.apache.hadoop.hive.ql.io.parquet.MapredParquetOutputFormat'LOCATION  'file:/tmp/hudi-partitions/multiPartitionDemo'TBLPROPERTIES (  'last_commit_time_sync'='20200816160557',  'transient_lastDdlTime'='1597565166')

查询表multipartitiondemo

2.3 无分区

无分区场景是指无分区字段,写入Hudi的数据集无分区。核心配置如下

  • DataSourceWriteOptions.PARTITIONPATH_FIELD_OPT_KEY()配置为空字符串;
  • hoodie.datasource.hive_sync.partition_fields配置为空字符串,与写入Hudi的分区字段相同;
  • DataSourceWriteOptions.KEYGENERATOR_CLASS_OPT_KEY()配置为org.apache.hudi.keygen.NonpartitionedKeyGenerator
  • hoodie.datasource.hive_sync.partition_extractor_class配置为org.apache.hudi.hive.NonPartitionedExtractor

Hudi同步到Hive创建的表如下

CREATE EXTERNAL TABLE `nonpartitiondemo`(  `_hoodie_commit_time` string,  `_hoodie_commit_seqno` string,  `_hoodie_record_key` string,  `_hoodie_partition_path` string,  `_hoodie_file_name` string,  `age` bigint,  `date` string,  `location` string,  `name` string,  `sex` string,  `ts` bigint)ROW FORMAT SERDE  'org.apache.hadoop.hive.ql.io.parquet.serde.ParquetHiveSerDe'STORED AS INPUTFORMAT  'org.apache.hudi.hadoop.HoodieParquetInputFormat'OUTPUTFORMAT  'org.apache.hadoop.hive.ql.io.parquet.MapredParquetOutputFormat'LOCATION  'file:/tmp/hudi-partitions/nonPartitionDemo'TBLPROPERTIES (  'last_commit_time_sync'='20200816161558',  'transient_lastDdlTime'='1597565767')

查询表nonpartitiondemo

2.4 Hive风格分区

除了上述几种常见的分区方式,还有一种Hive风格分区格式,如location=beijing/sex=male格式,以location,sex作为分区字段,核心配置如下

  • DataSourceWriteOptions.PARTITIONPATH_FIELD_OPT_KEY()配置为location,sex
  • hoodie.datasource.hive_sync.partition_fields配置为location,sex,与写入Hudi的分区字段相同;
  • DataSourceWriteOptions.KEYGENERATOR_CLASS_OPT_KEY()配置为org.apache.hudi.keygen.ComplexKeyGenerator
  • hoodie.datasource.hive_sync.partition_extractor_class配置为org.apache.hudi.hive.SlashEncodedDayPartitionValueExtractor
  • DataSourceWriteOptions.HIVE_STYLE_PARTITIONING_OPT_KEY()配置为true

生成的Hudi数据集目录结构会为如下格式

/location=beijing/sex=male

Hudi同步到Hive创建的表如下

CREATE EXTERNAL TABLE `hivestylepartitiondemo`(  `_hoodie_commit_time` string,  `_hoodie_commit_seqno` string,  `_hoodie_record_key` string,  `_hoodie_partition_path` string,  `_hoodie_file_name` string,  `age` bigint,  `date` string,  `name` string,  `ts` bigint)PARTITIONED BY (  `location` string,  `sex` string)ROW FORMAT SERDE  'org.apache.hadoop.hive.ql.io.parquet.serde.ParquetHiveSerDe'STORED AS INPUTFORMAT  'org.apache.hudi.hadoop.HoodieParquetInputFormat'OUTPUTFORMAT  'org.apache.hadoop.hive.ql.io.parquet.MapredParquetOutputFormat'LOCATION  'file:/tmp/hudi-partitions/hiveStylePartitionDemo'TBLPROPERTIES (  'last_commit_time_sync'='20200816172710',  'transient_lastDdlTime'='1597570039')

查询表hivestylepartitiondemo

3. 总结

本篇文章介绍了Hudi如何处理不同分区场景,上述配置的分区类配置可以满足绝大多数场景,当然Hudi非常灵活,还支持自定义分区解析器,具体可查看KeyGeneratorPartitionValueExtractor类,其中所有写入Hudi的分区字段生成器都是KeyGenerator的子类,所有同步至Hive的分区值解析器都是PartitionValueExtractor的子类。上述示例代码都已经上传至https://github.com/leesf/hudi-demos,该仓库会持续补充各种使用Hudi的Demo,方便开发者快速了解Hudi,构建企业级数据湖,欢迎star & fork。

推荐阅读

Apache Hudi表自动同步至阿里云数据湖分析DLA

Apache Hudi + AWS S3 + Athena实践

官宣!AWS Athena正式可查Apache Hudi数据集

生态 | Apache Hudi插上Alluxio的翅膀

Apache Hudi重磅RFC解读之存量表高效迁移机制

`

bigint hive java类型_详解Apache Hudi如何配置各种类型分区相关推荐

  1. pythonnamedtuple定义类型_详解Python中namedtuple的使用

    namedtuple是Python中存储数据类型,比较常见的数据类型还有有list和tuple数据类型.相比于list,tuple中的元素不可修改,在映射中可以当键使用. namedtuple: na ...

  2. java 矩阵求逆_详解用java描述矩阵求逆的算法

    今天很开心把困扰几天的问题解决了,在学习线性代数这门课程的时候.想通过程序实现里面的计算方法,比如矩阵求逆,用java代码该如何描述呢? 首先,咱们先用我们所交流语言描述一下算法思路: 1.求出一个矩 ...

  3. python中byte类型_详解python string类型 bytes类型 bytearray类型

    搜索热词 一.python3对文本和二进制数据做了区分.文本是Unicode编码,str类型,用于显示.二进制类型是bytes类型,用于存储和传输.bytes是byte的序列,而str是unicode ...

  4. dateformat 返回类型_详解Java中格式化日期的DateFormat与SimpleDateFormat类

    DateFormat其本身是一个抽象类,SimpleDateFormat 类是DateFormat类的子类,一般情况下来讲DateFormat类很少会直接使用,而都使用SimpleDateFormat ...

  5. java 运算符_详解Java表达式与运算符

    课程导言 [变量的赋值与计算都离不开表达式,表达式的运算依赖于变量.常量和运算符.本节课讨论Java的表达式的构成.常量的定义.运算符的分类及应用.通过本课的学习你将掌握运用表达式和运算符完成变量赋值 ...

  6. java熔断器_详解spring cloud分布式关于熔断器

    spring cloud分布式中,熔断器就是断路器,其实都是一个意思. 为什么要使用熔断器呢? 在分布式中,我们会根据业务或功能将项目拆分为多个服务单元,各个服务单元之间通过服务注册和订阅的方式相互依 ...

  7. php 打开jnlp,PHP获取文件mimes类型步骤详解

    这次给大家带来PHP获取文件mimes类型步骤详解,PHP获取文件mimes类型的注意事项有哪些,下面就是实战案例,一起来看一下.<?php /* * Copyright 2010-2013 A ...

  8. tomcat java内存_[Tomcat]Java内存溢出详解Tomcat内存设置

    Java内存溢出详解 一.常见的Java内存溢出有以下三种: 1.java.lang.OutOfMemoryError: Java heap space ----JVM Heap(堆)溢出 JVM在启 ...

  9. java内部格式_详解java内部类的访问格式和规则

    详解java内部类的访问格式和规则 1.内部类的定义 定义一个类来描述事物,但是这个事物其中可能还有事物,这时候在类中再定义类来描述. 2.内部类访问规则 ①内部类可以直接访问外部类中的成员,包括私有 ...

最新文章

  1. 使用 Windows 命令行删除结果
  2. C++中输入输出的十六进制八进制
  3. 关于自定义可以点击的的布局
  4. 用ACL构建防火墙体系
  5. 阅读王概凯老师架构漫谈系列总结
  6. maven的三种packaging方式
  7. 2016杭州ccpc
  8. telnet服务器怎么配置文件,配置telnet服务器
  9. 再谈子网划分方法与子网划分示例
  10. 8-4 redis sentine 安装
  11. java创建对象方法列表(转)
  12. linux学习笔记:磁盘格式化与磁盘检验命令
  13. URAL-1991 The battle near the swamp 水题
  14. 【图像评价】基于matlab图像去雾质量评价【含Matlab源码 066期】
  15. linux中swap的权限,有关 Linux Swap
  16. C语言写学生成绩管理系统(超详细注解)
  17. mysql配置文件在哪_windows下的mysql配置文件在哪
  18. H5跳转支付宝小程序
  19. 《卓有成效的管理者》读书笔记
  20. 冰点还原离线激活_冰点还原密钥,手把手教你如何激活冰点还原

热门文章

  1. dotNET:怎样处理程序中的异常(实战篇)?
  2. 你很可能需要知道这个调试小技巧
  3. Azure 国际版与中国版服务列表对(2020年6月版)
  4. dapr微服务.net sdk入门
  5. 领域驱动设计,让程序员心中有码(二)
  6. .Net Core功能开关实战
  7. 使用ML.NET预测纽约出租车费
  8. .Net中的AOP系列之《AOP实现类型》
  9. Django05: 请求生命周期流程图/路由层
  10. [转]解决Android studio升级到3.5的一些问题