1. 引入
Apache Hudi支持多種分區(qū)方式數(shù)據(jù)集,如多級(jí)分區(qū)、單分區(qū)、時(shí)間日期分區(qū)、無分區(qū)數(shù)據(jù)集等,用戶可根據(jù)實(shí)際需求選擇合適的分區(qū)方式,下面來詳細(xì)了解Hudi如何配置何種類型分區(qū)。
2. 分區(qū)處理
為說明Hudi對(duì)不同分區(qū)類型的處理,假定寫入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" ]
} ]
}
其中一條具體數(shù)據(jù)如下
{
"name": "zhangsan",
"ts": 1574297893837,
"age": 16,
"location": "beijing",
"sex":"male",
"date":"2020/08/16"
}
2.1 單分區(qū)
單分區(qū)表示使用一個(gè)字段表示作為分區(qū)字段的場(chǎng)景,可具體分為非日期格式字段(如location)和日期格式字段(如date)
2.1.1 非日期格式字段分區(qū)
如使用上述location字段作為分區(qū)字段,在寫入Hudi并同步至Hive時(shí)配置如下
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);
值得注意如下幾個(gè)配置項(xiàng)
- DataSourceWriteOptions.PARTITIONPATH_FIELD_OPT_KEY()配置為location;
- hoodie.datasource.hive_sync.partition_fields配置為location,與寫入Hudi的分區(qū)字段相同;
- DataSourceWriteOptions.KEYGENERATOR_CLASS_OPT_KEY()配置為org.apache.hudi.keygen.SimpleKeyGenerator,或者不配置該選項(xiàng),默認(rèn)為org.apache.hudi.keygen.SimpleKeyGenerator;
- hoodie.datasource.hive_sync.partition_extractor_class配置為org.apache.hudi.hive.MultiPartKeysValueExtractor;
Hudi同步到Hive創(chuàng)建的表如下
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: 查詢時(shí)請(qǐng)先將hudi-hive-sync-bundle-xxx.jar包放入$HIVE_HOME/lib下
2.1.2 日期格式分區(qū)
如使用上述date字段作為分區(qū)字段,核心配置項(xiàng)如下
- DataSourceWriteOptions.PARTITIONPATH_FIELD_OPT_KEY()配置為date;
- hoodie.datasource.hive_sync.partition_fields配置為date,與寫入Hudi的分區(qū)字段相同;
- DataSourceWriteOptions.KEYGENERATOR_CLASS_OPT_KEY()配置為org.apache.hudi.keygen.SimpleKeyGenerator,或者不配置該選項(xiàng),默認(rèn)為org.apache.hudi.keygen.SimpleKeyGenerator;
- hoodie.datasource.hive_sync.partition_extractor_class配置為org.apache.hudi.hive.SlashEncodedDayPartitionValueExtractor;
Hudi同步到Hive創(chuàng)建的表如下
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 多分區(qū)
多分區(qū)表示使用多個(gè)字段表示作為分區(qū)字段的場(chǎng)景,如上述使用location字段和sex字段,核心配置項(xiàng)如下
- DataSourceWriteOptions.PARTITIONPATH_FIELD_OPT_KEY()配置為location,sex;
- hoodie.datasource.hive_sync.partition_fields配置為location,sex,與寫入Hudi的分區(qū)字段相同;
- DataSourceWriteOptions.KEYGENERATOR_CLASS_OPT_KEY()配置為org.apache.hudi.keygen.ComplexKeyGenerator;
- hoodie.datasource.hive_sync.partition_extractor_class配置為org.apache.hudi.hive.MultiPartKeysValueExtractor;
Hudi同步到Hive創(chuàng)建的表如下
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 無分區(qū)
無分區(qū)場(chǎng)景是指無分區(qū)字段,寫入Hudi的數(shù)據(jù)集無分區(qū)。核心配置如下
- DataSourceWriteOptions.PARTITIONPATH_FIELD_OPT_KEY()配置為空字符串;
- hoodie.datasource.hive_sync.partition_fields配置為空字符串,與寫入Hudi的分區(qū)字段相同;
- DataSourceWriteOptions.KEYGENERATOR_CLASS_OPT_KEY()配置為org.apache.hudi.keygen.NonpartitionedKeyGenerator;
- hoodie.datasource.hive_sync.partition_extractor_class配置為org.apache.hudi.hive.NonPartitionedExtractor;
Hudi同步到Hive創(chuàng)建的表如下
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風(fēng)格分區(qū)
除了上述幾種常見的分區(qū)方式,還有一種Hive風(fēng)格分區(qū)格式,如location=beijing/sex=male格式,以location,sex作為分區(qū)字段,核心配置如下
- DataSourceWriteOptions.PARTITIONPATH_FIELD_OPT_KEY()配置為location,sex;
- hoodie.datasource.hive_sync.partition_fields配置為location,sex,與寫入Hudi的分區(qū)字段相同;
- 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數(shù)據(jù)集目錄結(jié)構(gòu)會(huì)為如下格式
/location=beijing/sex=male
Hudi同步到Hive創(chuàng)建的表如下
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. 總結(jié)
本篇文章介紹了Hudi如何處理不同分區(qū)場(chǎng)景,上述配置的分區(qū)類配置可以滿足絕大多數(shù)場(chǎng)景,當(dāng)然Hudi非常靈活,還支持自定義分區(qū)解析器,具體可查看KeyGenerator和PartitionValueExtractor類,其中所有寫入Hudi的分區(qū)字段生成器都是KeyGenerator的子類,所有同步至Hive的分區(qū)值解析器都是PartitionValueExtractor的子類。