-
Notifications
You must be signed in to change notification settings - Fork 2.5k
feat(flink): Sort bulk insert records by record key for LSM layout #19390
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Changes from 2 commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,85 @@ | ||
| /* | ||
| * Licensed to the Apache Software Foundation (ASF) under one | ||
| * or more contributor license agreements. See the NOTICE file | ||
| * distributed with this work for additional information | ||
| * regarding copyright ownership. The ASF licenses this file | ||
| * to you under the Apache License, Version 2.0 (the | ||
| * "License"); you may not use this file except in compliance | ||
| * with the License. You may obtain a copy of the License at | ||
| * | ||
| * http://www.apache.org/licenses/LICENSE-2.0 | ||
| * | ||
| * Unless required by applicable law or agreed to in writing, software | ||
| * distributed under the License is distributed on an "AS IS" BASIS, | ||
| * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
| * See the License for the specific language governing permissions and | ||
| * limitations under the License. | ||
| */ | ||
|
|
||
| package org.apache.hudi.sink.bucket; | ||
|
|
||
| import org.apache.hudi.config.HoodieWriteConfig; | ||
| import org.apache.hudi.index.bucket.partition.NumBucketsFunction; | ||
| import org.apache.hudi.sink.bulk.RowDataKeyGen; | ||
| import org.apache.hudi.table.HoodieTable; | ||
|
|
||
| import org.apache.flink.configuration.Configuration; | ||
| import org.apache.flink.table.data.GenericRowData; | ||
| import org.apache.flink.table.data.RowData; | ||
| import org.apache.flink.table.data.StringData; | ||
| import org.apache.flink.table.types.logical.RowType; | ||
|
|
||
| import java.io.IOException; | ||
| import java.util.List; | ||
| import java.util.Map; | ||
|
|
||
| /** | ||
| * Bucket-index bulk-insert writer helper for LSM input rows. | ||
| * | ||
| * <p>The input row contains file ID, encoded record key, and the original table row. | ||
| */ | ||
| public class LsmBucketBulkInsertWriterHelper extends BucketBulkInsertWriterHelper { | ||
|
|
||
| public LsmBucketBulkInsertWriterHelper( | ||
| Configuration conf, | ||
| HoodieTable<?, ?, ?, ?> hoodieTable, | ||
| HoodieWriteConfig writeConfig, | ||
| String instantTime, | ||
| int taskPartitionId, | ||
| long taskId, | ||
| long taskEpochId, | ||
| RowType rowType) { | ||
| super(conf, hoodieTable, writeConfig, instantTime, taskPartitionId, taskId, taskEpochId, rowType); | ||
| } | ||
|
|
||
| @Override | ||
| public void write(RowData sortRow) throws IOException { | ||
| String fileId = sortRow.getString(0).toString(); | ||
| String recordKey = sortRow.getString(1).toString(); | ||
| RowData record = sortRow.getRow(2, recordArity); | ||
| String partitionPath = keyGen.getPartitionPath(record); | ||
| writeRecord(recordKey, partitionPath, fileId, record); | ||
| } | ||
|
|
||
| public static RowData rowWithFileIdAndRecordKey( | ||
| Map<String, String> bucketIdToFileId, | ||
| RowDataKeyGen keyGen, | ||
| RowData record, | ||
| List<String> indexKeyFields, | ||
| NumBucketsFunction numBucketsFunction, | ||
| boolean needFixedFileIdSuffix) { | ||
| String recordKey = keyGen.getRecordKey(record); | ||
| String partitionPath = keyGen.getPartitionPath(record); | ||
| String fileId = getFileId( | ||
| bucketIdToFileId, | ||
| recordKey, | ||
| partitionPath, | ||
| indexKeyFields, | ||
| numBucketsFunction, | ||
| needFixedFileIdSuffix); | ||
| return GenericRowData.of( | ||
| StringData.fromString(fileId), | ||
| StringData.fromString(recordKey), | ||
| record); | ||
| } | ||
| } |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,81 @@ | ||
| /* | ||
| * Licensed to the Apache Software Foundation (ASF) under one | ||
| * or more contributor license agreements. See the NOTICE file | ||
| * distributed with this work for additional information | ||
| * regarding copyright ownership. The ASF licenses this file | ||
| * to you under the Apache License, Version 2.0 (the | ||
| * "License"); you may not use this file except in compliance | ||
| * with the License. You may obtain a copy of the License at | ||
| * | ||
| * http://www.apache.org/licenses/LICENSE-2.0 | ||
| * | ||
| * Unless required by applicable law or agreed to in writing, software | ||
| * distributed under the License is distributed on an "AS IS" BASIS, | ||
| * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
| * See the License for the specific language governing permissions and | ||
| * limitations under the License. | ||
| */ | ||
|
|
||
| package org.apache.hudi.sink.bulk; | ||
|
|
||
| import org.apache.hudi.config.HoodieWriteConfig; | ||
| import org.apache.hudi.table.HoodieTable; | ||
|
|
||
| import org.apache.flink.configuration.Configuration; | ||
| import org.apache.flink.table.data.GenericRowData; | ||
| import org.apache.flink.table.data.RowData; | ||
| import org.apache.flink.table.data.StringData; | ||
| import org.apache.flink.table.types.logical.RowType; | ||
|
|
||
| import java.io.IOException; | ||
|
|
||
| /** | ||
| * Bulk-insert writer helper for LSM input rows. | ||
| * | ||
| * <p>The input row contains partition path, encoded record key, and the original table row. The | ||
| * routing and record keys are retained after sorting so the writer does not generate them again. | ||
| */ | ||
| public class LsmBulkInsertWriterHelper extends BulkInsertWriterHelper { | ||
|
|
||
| private final int recordArity; | ||
|
|
||
| public LsmBulkInsertWriterHelper( | ||
| Configuration conf, | ||
| HoodieTable<?, ?, ?, ?> hoodieTable, | ||
| HoodieWriteConfig writeConfig, | ||
| String instantTime, | ||
| int taskPartitionId, | ||
| long taskId, | ||
| long taskEpochId, | ||
| RowType rowType) { | ||
| super(conf, hoodieTable, writeConfig, instantTime, taskPartitionId, taskId, taskEpochId, rowType); | ||
| this.recordArity = rowType.getFieldCount(); | ||
| } | ||
|
|
||
| @Override | ||
| public void write(RowData sortRow) throws IOException { | ||
| String partitionPath = sortRow.getString(0).toString(); | ||
| String recordKey = sortRow.getString(1).toString(); | ||
| RowData record = sortRow.getRow(2, recordArity); | ||
| writeRecord(recordKey, partitionPath, record); | ||
| } | ||
|
|
||
| /** | ||
| * Decorates a table row with the partition path and encoded Hudi record key used by the LSM | ||
| * sorter. | ||
| * | ||
| * @param partitionPath partition path | ||
| * @param record original table row used to generate the Hudi record key | ||
| * @param keyGen Hudi RowData key generator | ||
| * @return internal LSM sort row containing partition path, record key, and original table row | ||
| */ | ||
| public static RowData rowWithPartitionAndKey( | ||
| String partitionPath, | ||
| RowData record, | ||
| RowDataKeyGen keyGen) { | ||
| return GenericRowData.of( | ||
| StringData.fromString(partitionPath), | ||
| StringData.fromString(keyGen.getRecordKey(record)), | ||
| record); | ||
| } | ||
| } |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -21,6 +21,7 @@ | |
| import org.apache.hudi.config.HoodieWriteConfig; | ||
| import org.apache.hudi.configuration.OptionsResolver; | ||
| import org.apache.hudi.sink.bucket.BucketBulkInsertWriterHelper; | ||
| import org.apache.hudi.sink.bucket.LsmBucketBulkInsertWriterHelper; | ||
| import org.apache.hudi.table.HoodieTable; | ||
|
|
||
| import org.apache.flink.configuration.Configuration; | ||
|
|
@@ -32,8 +33,18 @@ | |
| public class WriterHelpers { | ||
| public static BulkInsertWriterHelper getWriterHelper(Configuration conf, HoodieTable<?, ?, ?, ?> hoodieTable, HoodieWriteConfig writeConfig, | ||
| String instantTime, int taskPartitionId, long taskId, long taskEpochId, RowType rowType) { | ||
| return OptionsResolver.isBucketIndexType(conf) | ||
| ? new BucketBulkInsertWriterHelper(conf, hoodieTable, writeConfig, instantTime, taskPartitionId, taskId, taskEpochId, rowType) | ||
| : new BulkInsertWriterHelper(conf, hoodieTable, writeConfig, instantTime, taskPartitionId, taskId, taskEpochId, rowType); | ||
| if (OptionsResolver.isLsmTreeStorageLayout(conf)) { | ||
| return OptionsResolver.isBucketIndexType(conf) | ||
| ? new LsmBucketBulkInsertWriterHelper( | ||
| conf, hoodieTable, writeConfig, instantTime, taskPartitionId, taskId, taskEpochId, rowType) | ||
| : new LsmBulkInsertWriterHelper( | ||
| conf, hoodieTable, writeConfig, instantTime, taskPartitionId, taskId, taskEpochId, rowType); | ||
| } else { | ||
| return OptionsResolver.isBucketIndexType(conf) | ||
| ? new BucketBulkInsertWriterHelper( | ||
| conf, hoodieTable, writeConfig, instantTime, taskPartitionId, taskId, taskEpochId, rowType) | ||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. 🤖 nit: the
cshuo marked this conversation as resolved.
Outdated
|
||
| : new BulkInsertWriterHelper( | ||
| conf, hoodieTable, writeConfig, instantTime, taskPartitionId, taskId, taskEpochId, rowType); | ||
| } | ||
| } | ||
| } | ||
Uh oh!
There was an error while loading. Please reload this page.