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

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下d0d98debc2cc7550611922717be1fc31.png

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

911b08d4bbe5774044de165d22c8aa91.png

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

1ebcd49841a0f8d6c254dfeabb4d5163.png

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

d3e006664af3703c56e3f4cb2f8abbed.png

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

1fded941e46f5510231b10c06988b6a8.png

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解读之存量表高效迁移机制

958aef72dd200ab00e527ac81fb33440.png

d0987e1a9c44abc9cf077ae14dd55a45.png

65022b501b0448aa5bbe9bf3b91300ad.png

`

本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如若转载,请注明出处:http://www.mzph.cn/news/291704.shtml

如若内容造成侵权/违法违规/事实不符,请联系多彩编程网进行投诉反馈email:809451989@qq.com,一经查实,立即删除!

相关文章

C#中的命名空间和程序集

前言今天这篇文章和大家一起学习下C#语言下的命名空间和程序集。在日常的编码工作中,我们对命名空间和程序集都不会很陌生。在创建项目文件时,IDE自动会为我们创建好一个大的命名空间和程序集。大多数业务代码都是在解决方案下各自的命令空间里进行编码的…

基于jQuery的ajax系列之用FormData实现页面无刷新上传

接着上一篇ajax系列之用jQuery的ajax方法向服务器发出get和post请求写,这篇主要写如何利用ajax和FormData实现页面无刷新的文件上传效果,主要用到了jQuery的ajax()方法和XMLHttpRequest Level 2的FormData接口。关于FormData,大家可以看MDN文…

abd shell关闭所有程序_在后台服务器上运行程序

之前总结过screen的用法,但还可以用nohup命令。nohup工具:Linux系统中有提供一个很好的不挂断地运行命令——nohup。我们使用nohup能很简单的控制使用,在此就简单的介绍一下nohup工具。nohup 命令nohup就是不挂起的意思( no hang up)。用途&a…

解锁JDK 12的奇妙之旅:新特性详解

欢迎来到我的博客,代码的世界里,每一行都是一个故事 解锁JDK 12的奇妙之旅:新特性详解 前言switch表达式拓展NumberFormat对复杂数字的格式化字符串支持transform、indent操作新增方法Files.mismatch(Path, Path)Teeing Collector支持unicode…

.NET6之MiniAPI(十四):跨域CORS(上)

为了说明跨请求,创建了两个项目,一个mini api,端口是5001,另一个razor page项目,端口是5280。5280项目会在页面用ajax的方式来请求5001,形成跨域请求,由于是本地测试,host都是localh…

django model filter 条件过滤,及多表连接查询、反向查询,某字段的distinct

2019独角兽企业重金招聘Python工程师标准>>> 1.多表连接查询:当我知道这点的时候顿时觉得django太NX了。 class A(models.Model): name models.CharField(u名称) class B(models.Model): aa models.ForeignKey(A)B.objects.filter(aa__name__c…

利用tabluea分析数据的案例_利用德温特分析Dartsip的案例检索结果

德温特创新平台(Derwent Innovation)与Darts-ip知识产权案例数据库均是科睿唯安旗下的知识产权数据库,虽然这两个数据库的侧重点分别在于专利信息与知识产权判例,但若将两者结合使用则能发挥11>2的作用,打通专利全生命周期。关注我们的朋友…

linux网络编程之怎么配置好unp.h文件

1、获取unp源码 下载地址:http://www.unpbook.com/src.html 然后用tar -zxvf unpv13e.tar.gz命令解压 2、进入unpv13e目录执行configure cd unpv13e ,然后执configure文件 3、打开README文件,使用make命令 打开README文件

客户端禁用Keep-Alive, 服务端开启Keep-Alive,会怎么样?

最近部署的web程序,服务器上出现不少time_wait的tcp连接状态,占用了tcp端口,花费几天时间排查。之前我有结论:HTTP keep-alive 是在应用层对TCP连接的滑动续约复用,如果客户端、服务器稳定续约,就成了名副其…

linux网络编程之一般应用采用的协议和不同套接字的地址结构以及用户进程和内核通过哪些函数传递套接字的地址结构

1、一般应用采用的协议 2、不同套接字的地址结构 3、用户进程和内核通过哪些函数传递套接字的地址结构 从进程到内核传递套接字的地址结构函数有3个 bind、connect、sendto函数 从内核到进程传递套接字的地址结构函数有4个函数 accept、recvfrom 、getsockname 、getpeername…

四则运算2测试

这是测试程序在输入任意字符时能否正常运行 1)按程序提示正确输入 结果无错 2)当输入错误的字符,如字母等,程序出错(错误提示无限循环) 这一错误我经过长时间反正为解决,于是请教了其他同学,发现…

2020-11-04关于出现tomcat启动失败的一种原因

点击run on server后出现了如下所示: 本来是运行正常的,后来因为我将exp5里面所有的文件都复制了一遍,放到了exp5_2里面后,如下所示: 此时(复制文件夹之前)若tomcat已经启动,则不会出…

HTTP协议快速入门

一、定义 The Hypertext Transfer Protocol (HTTP) is an application protocol for distributed, collaborative, hypermedia information systems. HTTP is the foundation of data communication for the World Wide Web. Hypertext is structured text that uses logical l…

删除含有关键词的文件_AweEraser——macOS Catalina最佳的文件粉碎机

您是否正在寻找适用于macOS Catalina的好的文件粉碎机?今天macdown为大家推荐一种永久删除数据的软件——AweEraser。有时,你要销毁或擦除计算机上的所有私人文件,这意味着这些数据必须受到保护,免受他人的侵害。本地硬盘或外部硬…

linux网络编程之用一张图片说明函数inet_ntop、inet_pton、inet_addr、inet_ntoa 、inet_aton函数之间的关系

1、inet_ntop、inet_pton、inet_addr、inet_ntoa 、inet_aton函数之间的关系 2、inet_ntop、inet_pton函数的源代码 1、inet_pton函数源码 int inet_pton(int family, const char *strptr, void *addrptr) {if (family == AF_INET) {struct in_addr in_val;if (inet_aton(s…

聊一聊如何用C#轻松完成一个TCC分布式事务

背景 银行跨行转账业务是一个典型分布式事务场景,假设 A 需要跨行转账给 B,那么就涉及两个银行的数据,无法通过一个数据库的本地事务保证转账的 ACID ,只能够通过分布式事务来解决。在 聊一聊如何用C#轻松完成一个SAGA分布式事务…

Xcode6.1 模拟器路径

Xcode 5的iOS模拟器的应用的目录是在~/Library/Application Support/iPhone Simulator/<iOS_Version>/Applications/{Application_ID} Xcode 6的目录改为~/Library/Developer/CoreSimulator/Devices/{Device_ID}/data/Containers/Bundle/Application/{Application_ID}/这…

新年伊始 .Net7 preview1 发布!

虎年伊始&#xff0c;.NET 7.0就要来了&#xff0c;还学的动吗&#xff1f;从github能看到&#xff0c;截止到2月8号&#xff0c;.NET 7.0 Preview1已经全部开发完成&#xff0c;连Preview2也完成了85%&#xff0c;这进度杠杠的&#xff01;微软这几年大力推进.NET稳定更新&…

数据挖掘课程实验(8个实验报告)

是从实验一到实验八的 链接&#xff1a;https://download.csdn.net/download/qq_44872173/15558967