-
Notifications
You must be signed in to change notification settings - Fork 2.5k
[HUDI-5884] Support bulk_insert for insert_overwrite and insert_overwrite_table #8076
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
Merged
codope
merged 1 commit into
apache:master
from
boneanxs:bulk_insert_as_row_for_insert_overwrite
Jun 29, 2023
Merged
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
131 changes: 131 additions & 0 deletions
131
...ommon/src/main/java/org/apache/hudi/commit/BaseDatasetBulkInsertCommitActionExecutor.java
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,131 @@ | ||
| /* | ||
| * 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.commit; | ||
|
|
||
| import org.apache.hudi.DataSourceUtils; | ||
| import org.apache.hudi.DataSourceWriteOptions; | ||
| import org.apache.hudi.HoodieDatasetBulkInsertHelper; | ||
| import org.apache.hudi.client.HoodieWriteResult; | ||
| import org.apache.hudi.client.SparkRDDWriteClient; | ||
| import org.apache.hudi.client.WriteStatus; | ||
| import org.apache.hudi.common.data.HoodieData; | ||
| import org.apache.hudi.common.model.WriteOperationType; | ||
| import org.apache.hudi.common.table.HoodieTableConfig; | ||
| import org.apache.hudi.common.util.CommitUtils; | ||
| import org.apache.hudi.common.util.Option; | ||
| import org.apache.hudi.config.HoodieWriteConfig; | ||
| import org.apache.hudi.data.HoodieJavaRDD; | ||
| import org.apache.hudi.exception.HoodieException; | ||
| import org.apache.hudi.execution.bulkinsert.BucketIndexBulkInsertPartitionerWithRows; | ||
| import org.apache.hudi.execution.bulkinsert.BulkInsertInternalPartitionerWithRowsFactory; | ||
| import org.apache.hudi.execution.bulkinsert.NonSortPartitionerWithRows; | ||
| import org.apache.hudi.index.HoodieIndex; | ||
| import org.apache.hudi.table.BulkInsertPartitioner; | ||
| import org.apache.hudi.table.HoodieTable; | ||
| import org.apache.hudi.table.action.HoodieWriteMetadata; | ||
| import org.apache.spark.api.java.JavaRDD; | ||
| import org.apache.spark.sql.Dataset; | ||
| import org.apache.spark.sql.Row; | ||
|
|
||
| import java.io.Serializable; | ||
| import java.util.Collections; | ||
| import java.util.List; | ||
| import java.util.Map; | ||
|
|
||
| public abstract class BaseDatasetBulkInsertCommitActionExecutor implements Serializable { | ||
|
|
||
| protected final transient HoodieWriteConfig writeConfig; | ||
| protected final transient SparkRDDWriteClient writeClient; | ||
| protected final String instantTime; | ||
| protected HoodieTable table; | ||
|
|
||
| public BaseDatasetBulkInsertCommitActionExecutor(HoodieWriteConfig config, | ||
| SparkRDDWriteClient writeClient, | ||
| String instantTime) { | ||
| this.writeConfig = config; | ||
| this.writeClient = writeClient; | ||
| this.instantTime = instantTime; | ||
| } | ||
|
|
||
| protected void preExecute() { | ||
| table.validateInsertSchema(); | ||
| writeClient.startCommitWithTime(instantTime, getCommitActionType()); | ||
| writeClient.preWrite(instantTime, getWriteOperationType(), table.getMetaClient()); | ||
| } | ||
|
|
||
| protected abstract Option<HoodieData<WriteStatus>> doExecute(Dataset<Row> records, boolean arePartitionRecordsSorted); | ||
|
|
||
| protected void afterExecute(HoodieWriteMetadata<JavaRDD<WriteStatus>> result) { | ||
| writeClient.postWrite(result, instantTime, table); | ||
| } | ||
|
|
||
| private HoodieWriteMetadata<JavaRDD<WriteStatus>> buildHoodieWriteMetadata(Option<HoodieData<WriteStatus>> writeStatuses) { | ||
| return writeStatuses.map(statuses -> { | ||
| HoodieWriteMetadata<JavaRDD<WriteStatus>> hoodieWriteMetadata = new HoodieWriteMetadata<>(); | ||
| hoodieWriteMetadata.setWriteStatuses(HoodieJavaRDD.getJavaRDD(statuses)); | ||
| hoodieWriteMetadata.setPartitionToReplaceFileIds(getPartitionToReplacedFileIds(statuses)); | ||
| return hoodieWriteMetadata; | ||
| }).orElse(new HoodieWriteMetadata<>()); | ||
| } | ||
|
|
||
| public final HoodieWriteResult execute(Dataset<Row> records, boolean isTablePartitioned) { | ||
| if (writeConfig.getBoolean(DataSourceWriteOptions.INSERT_DROP_DUPS())) { | ||
| throw new HoodieException("Dropping duplicates with bulk_insert in row writer path is not supported yet"); | ||
| } | ||
|
|
||
| boolean populateMetaFields = writeConfig.getBoolean(HoodieTableConfig.POPULATE_META_FIELDS); | ||
|
|
||
| BulkInsertPartitioner<Dataset<Row>> bulkInsertPartitionerRows = getPartitioner(populateMetaFields, isTablePartitioned); | ||
| boolean shouldDropPartitionColumns = writeConfig.getBoolean(DataSourceWriteOptions.DROP_PARTITION_COLUMNS()); | ||
| Dataset<Row> hoodieDF = HoodieDatasetBulkInsertHelper.prepareForBulkInsert(records, writeConfig, bulkInsertPartitionerRows, shouldDropPartitionColumns, instantTime); | ||
|
|
||
| table = writeClient.initTable(getWriteOperationType(), Option.ofNullable(instantTime)); | ||
| preExecute(); | ||
| HoodieWriteMetadata<JavaRDD<WriteStatus>> result = buildHoodieWriteMetadata(doExecute(hoodieDF, bulkInsertPartitionerRows.arePartitionRecordsSorted())); | ||
| afterExecute(result); | ||
|
|
||
| return new HoodieWriteResult(result.getWriteStatuses(), result.getPartitionToReplaceFileIds()); | ||
| } | ||
|
|
||
| public abstract WriteOperationType getWriteOperationType(); | ||
|
|
||
| public String getCommitActionType() { | ||
| return CommitUtils.getCommitActionType(getWriteOperationType(), writeClient.getConfig().getTableType()); | ||
| } | ||
|
|
||
| protected BulkInsertPartitioner<Dataset<Row>> getPartitioner(boolean populateMetaFields, boolean isTablePartitioned) { | ||
| if (populateMetaFields) { | ||
| if (writeConfig.getIndexType() == HoodieIndex.IndexType.BUCKET) { | ||
| return new BucketIndexBulkInsertPartitionerWithRows(writeConfig.getBucketIndexHashFieldWithDefault(), | ||
| writeConfig.getBucketIndexNumBuckets()); | ||
| } else { | ||
| return DataSourceUtils | ||
| .createUserDefinedBulkInsertPartitionerWithRows(writeConfig) | ||
| .orElseGet(() -> BulkInsertInternalPartitionerWithRowsFactory.get(writeConfig, isTablePartitioned)); | ||
| } | ||
| } else { | ||
| // Sort modes are not yet supported when meta fields are disabled | ||
| return new NonSortPartitionerWithRows(); | ||
| } | ||
| } | ||
|
|
||
| protected Map<String, List<String>> getPartitionToReplacedFileIds(HoodieData<WriteStatus> writeStatuses) { | ||
| return Collections.emptyMap(); | ||
| } | ||
| } |
94 changes: 94 additions & 0 deletions
94
...rk-common/src/main/java/org/apache/hudi/commit/DatasetBulkInsertCommitActionExecutor.java
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,94 @@ | ||
| /* | ||
| * 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.commit; | ||
|
|
||
| import org.apache.hudi.HoodieSparkUtils; | ||
| import org.apache.hudi.client.SparkRDDWriteClient; | ||
| import org.apache.hudi.client.WriteStatus; | ||
| import org.apache.hudi.common.data.HoodieData; | ||
| import org.apache.hudi.common.model.WriteOperationType; | ||
| import org.apache.hudi.common.util.Option; | ||
| import org.apache.hudi.config.HoodieInternalConfig; | ||
| import org.apache.hudi.config.HoodieWriteConfig; | ||
| import org.apache.hudi.exception.HoodieException; | ||
| import org.apache.hudi.internal.DataSourceInternalWriterHelper; | ||
| import org.apache.hudi.table.action.HoodieWriteMetadata; | ||
| import org.apache.spark.api.java.JavaRDD; | ||
| import org.apache.spark.sql.Dataset; | ||
| import org.apache.spark.sql.Row; | ||
| import org.apache.spark.sql.SaveMode; | ||
|
|
||
| import java.util.Collections; | ||
| import java.util.HashMap; | ||
| import java.util.Map; | ||
| import java.util.stream.Collectors; | ||
|
|
||
| public class DatasetBulkInsertCommitActionExecutor extends BaseDatasetBulkInsertCommitActionExecutor { | ||
|
|
||
| public DatasetBulkInsertCommitActionExecutor(HoodieWriteConfig config, | ||
| SparkRDDWriteClient writeClient, | ||
| String instantTime) { | ||
| super(config, writeClient, instantTime); | ||
| } | ||
|
|
||
| @Override | ||
| protected void preExecute() { | ||
| // no op | ||
| } | ||
|
|
||
| @Override | ||
| protected Option<HoodieData<WriteStatus>> doExecute(Dataset<Row> records, boolean arePartitionRecordsSorted) { | ||
| Map<String, String> opts = writeConfig.getProps().entrySet().stream().collect(Collectors.toMap( | ||
| e -> String.valueOf(e.getKey()), | ||
| e -> String.valueOf(e.getValue()))); | ||
| Map<String, String> optsOverrides = Collections.singletonMap( | ||
| HoodieInternalConfig.BULKINSERT_ARE_PARTITIONER_RECORDS_SORTED, String.valueOf(arePartitionRecordsSorted)); | ||
|
|
||
| String targetFormat; | ||
| Map<String, String> customOpts = new HashMap<>(1); | ||
| if (HoodieSparkUtils.isSpark2()) { | ||
| targetFormat = "org.apache.hudi.internal"; | ||
| } else if (HoodieSparkUtils.isSpark3()) { | ||
| targetFormat = "org.apache.hudi.spark3.internal"; | ||
| customOpts.put(HoodieInternalConfig.BULKINSERT_INPUT_DATA_SCHEMA_DDL.key(), records.schema().json()); | ||
| } else { | ||
| throw new HoodieException("Bulk insert using row writer is not supported with current Spark version." | ||
| + " To use row writer please switch to spark 2 or spark 3"); | ||
| } | ||
|
|
||
| records.write().format(targetFormat) | ||
| .option(DataSourceInternalWriterHelper.INSTANT_TIME_OPT_KEY, instantTime) | ||
| .options(opts) | ||
| .options(customOpts) | ||
| .options(optsOverrides) | ||
| .mode(SaveMode.Append) | ||
| .save(); | ||
| return Option.empty(); | ||
| } | ||
|
|
||
| @Override | ||
| protected void afterExecute(HoodieWriteMetadata<JavaRDD<WriteStatus>> result) { | ||
| // no op | ||
| } | ||
|
|
||
| @Override | ||
| public WriteOperationType getWriteOperationType() { | ||
| return WriteOperationType.BULK_INSERT; | ||
| } | ||
| } |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.