-
Notifications
You must be signed in to change notification settings - Fork 2.5k
[HUDI-1104] Adding support for UserDefinedPartitioners and SortModes to BulkInsert with Rows #3149
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
nsivabalan
merged 8 commits into
apache:master
from
nsivabalan:bulk_insert_simplified_prep_internal_custom_ds
Jul 7, 2021
Merged
Changes from all commits
Commits
Show all changes
8 commits
Select commit
Hold shift + click to select a range
212523a
trigger rebuild
lamberken 8f46bff
[HUDI-1156] Remove unused dependencies from HoodieDeltaStreamerWrappe…
linshan-ma 3fda2ee
Adding bulk insert sort modes and user defined bulk insert partitione…
nsivabalan 1752f5e
Rebasing and adding tests for BulkInsertPartitioners with Rows
nsivabalan a304570
Fixing build issues
nsivabalan 82f373c
fetching and rebasing with master
nsivabalan af3e97a
Caching row create handles
nsivabalan bbf3285
Addressing feedback
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
42 changes: 42 additions & 0 deletions
42
...-client/hudi-client-common/src/main/java/org/apache/hudi/config/HoodieInternalConfig.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,42 @@ | ||
| /* | ||
| * 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.config; | ||
|
|
||
| import org.apache.hudi.common.config.HoodieConfig; | ||
|
|
||
| /** | ||
| * Configs/params used for internal purposes. | ||
| */ | ||
| public class HoodieInternalConfig extends HoodieConfig { | ||
|
|
||
| private static final long serialVersionUID = 0L; | ||
|
|
||
| public static final String BULKINSERT_ARE_PARTITIONER_RECORDS_SORTED = "hoodie.bulkinsert.are.partitioner.records.sorted"; | ||
| public static final Boolean DEFAULT_BULKINSERT_ARE_PARTITIONER_RECORDS_SORTED = false; | ||
|
|
||
| /** | ||
| * Returns if partition records are sorted or not. | ||
| * @param propertyValue value for property BULKINSERT_ARE_PARTITIONER_RECORDS_SORTED. | ||
| * @return the property value. | ||
| */ | ||
| public static Boolean getBulkInsertIsPartitionRecordsSorted(String propertyValue) { | ||
| return propertyValue != null ? Boolean.parseBoolean(propertyValue) : DEFAULT_BULKINSERT_ARE_PARTITIONER_RECORDS_SORTED; | ||
| } | ||
|
|
||
| } |
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
44 changes: 44 additions & 0 deletions
44
...va/org/apache/hudi/execution/bulkinsert/BulkInsertInternalPartitionerWithRowsFactory.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,44 @@ | ||
| /* | ||
| * 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.execution.bulkinsert; | ||
|
|
||
| import org.apache.hudi.table.BulkInsertPartitioner; | ||
|
|
||
| import org.apache.spark.sql.Dataset; | ||
| import org.apache.spark.sql.Row; | ||
|
|
||
| /** | ||
| * A factory to generate built-in partitioner to repartition input Rows into at least | ||
| * expected number of output spark partitions for bulk insert operation. | ||
| */ | ||
| public abstract class BulkInsertInternalPartitionerWithRowsFactory { | ||
|
|
||
| public static BulkInsertPartitioner<Dataset<Row>> get(BulkInsertSortMode sortMode) { | ||
| switch (sortMode) { | ||
| case NONE: | ||
| return new NonSortPartitionerWithRows(); | ||
| case GLOBAL_SORT: | ||
| return new GlobalSortPartitionerWithRows(); | ||
| case PARTITION_SORT: | ||
| return new PartitionSortPartitionerWithRows(); | ||
| default: | ||
| throw new UnsupportedOperationException("The bulk insert sort mode \"" + sortMode.name() + "\" is not supported."); | ||
| } | ||
| } | ||
| } | ||
45 changes: 45 additions & 0 deletions
45
...ent/src/main/java/org/apache/hudi/execution/bulkinsert/GlobalSortPartitionerWithRows.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,45 @@ | ||
| /* | ||
| * 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.execution.bulkinsert; | ||
|
|
||
| import org.apache.hudi.common.model.HoodieRecord; | ||
| import org.apache.hudi.table.BulkInsertPartitioner; | ||
|
|
||
| import org.apache.spark.sql.Dataset; | ||
| import org.apache.spark.sql.Row; | ||
| import org.apache.spark.sql.functions; | ||
|
|
||
| /** | ||
| * A built-in partitioner that does global sorting for the input Rows across partitions after repartition for bulk insert operation, corresponding to the {@code BulkInsertSortMode.GLOBAL_SORT} mode. | ||
| */ | ||
| public class GlobalSortPartitionerWithRows implements BulkInsertPartitioner<Dataset<Row>> { | ||
|
|
||
| @Override | ||
| public Dataset<Row> repartitionRecords(Dataset<Row> rows, int outputSparkPartitions) { | ||
| // Now, sort the records and line them up nicely for loading. | ||
| // Let's use "partitionPath + key" as the sort key. | ||
| return rows.sort(functions.col(HoodieRecord.PARTITION_PATH_METADATA_FIELD), functions.col(HoodieRecord.RECORD_KEY_METADATA_FIELD)) | ||
| .coalesce(outputSparkPartitions); | ||
| } | ||
|
|
||
| @Override | ||
| public boolean arePartitionRecordsSorted() { | ||
| return true; | ||
| } | ||
| } |
42 changes: 42 additions & 0 deletions
42
...client/src/main/java/org/apache/hudi/execution/bulkinsert/NonSortPartitionerWithRows.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,42 @@ | ||
| /* | ||
| * 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.execution.bulkinsert; | ||
|
|
||
| import org.apache.hudi.table.BulkInsertPartitioner; | ||
|
|
||
| import org.apache.spark.sql.Dataset; | ||
| import org.apache.spark.sql.Row; | ||
|
|
||
| /** | ||
| * A built-in partitioner that only does coalesce for input Rows for bulk insert operation, | ||
| * corresponding to the {@code BulkInsertSortMode.NONE} mode. | ||
| * | ||
| */ | ||
| public class NonSortPartitionerWithRows implements BulkInsertPartitioner<Dataset<Row>> { | ||
|
|
||
| @Override | ||
| public Dataset<Row> repartitionRecords(Dataset<Row> rows, int outputSparkPartitions) { | ||
| return rows.coalesce(outputSparkPartitions); | ||
| } | ||
|
|
||
| @Override | ||
| public boolean arePartitionRecordsSorted() { | ||
| return false; | ||
| } | ||
| } |
41 changes: 41 additions & 0 deletions
41
.../src/main/java/org/apache/hudi/execution/bulkinsert/PartitionSortPartitionerWithRows.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,41 @@ | ||
| /* | ||
| * 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.execution.bulkinsert; | ||
|
|
||
| import org.apache.hudi.common.model.HoodieRecord; | ||
| import org.apache.hudi.table.BulkInsertPartitioner; | ||
|
|
||
| import org.apache.spark.sql.Dataset; | ||
| import org.apache.spark.sql.Row; | ||
|
|
||
| /** | ||
| * A built-in partitioner that does local sorting for each spark partitions after coalesce for bulk insert operation, corresponding to the {@code BulkInsertSortMode.PARTITION_SORT} mode. | ||
| */ | ||
| public class PartitionSortPartitionerWithRows implements BulkInsertPartitioner<Dataset<Row>> { | ||
|
|
||
| @Override | ||
| public Dataset<Row> repartitionRecords(Dataset<Row> rows, int outputSparkPartitions) { | ||
| return rows.coalesce(outputSparkPartitions).sortWithinPartitions(HoodieRecord.PARTITION_PATH_METADATA_FIELD, HoodieRecord.RECORD_KEY_METADATA_FIELD); | ||
| } | ||
|
|
||
| @Override | ||
| public boolean arePartitionRecordsSorted() { | ||
| return true; | ||
| } | ||
| } |
139 changes: 139 additions & 0 deletions
139
...t/java/org/apache/hudi/execution/bulkinsert/TestBulkInsertInternalPartitionerForRows.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,139 @@ | ||
| /* | ||
| * 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.execution.bulkinsert; | ||
|
|
||
| import org.apache.hudi.common.model.HoodieRecord; | ||
| import org.apache.hudi.common.testutils.HoodieTestDataGenerator; | ||
| import org.apache.hudi.table.BulkInsertPartitioner; | ||
| import org.apache.hudi.testutils.HoodieClientTestHarness; | ||
| import org.apache.hudi.testutils.SparkDatasetTestUtils; | ||
|
|
||
| import org.apache.spark.api.java.function.MapPartitionsFunction; | ||
| import org.apache.spark.sql.Dataset; | ||
| import org.apache.spark.sql.Row; | ||
| import org.junit.jupiter.api.AfterEach; | ||
| import org.junit.jupiter.api.BeforeEach; | ||
| import org.junit.jupiter.params.ParameterizedTest; | ||
| import org.junit.jupiter.params.provider.Arguments; | ||
| import org.junit.jupiter.params.provider.MethodSource; | ||
|
|
||
| import java.util.ArrayList; | ||
| import java.util.Collections; | ||
| import java.util.Comparator; | ||
| import java.util.HashMap; | ||
| import java.util.List; | ||
| import java.util.Map; | ||
| import java.util.stream.Stream; | ||
|
|
||
| import static org.junit.jupiter.api.Assertions.assertEquals; | ||
|
|
||
| /** | ||
| * Unit tests {@link BulkInsertPartitioner}s with Rows. | ||
| */ | ||
| public class TestBulkInsertInternalPartitionerForRows extends HoodieClientTestHarness { | ||
|
|
||
| @BeforeEach | ||
| public void setUp() throws Exception { | ||
| initSparkContexts("TestBulkInsertInternalPartitionerForRows"); | ||
| initPath(); | ||
| initFileSystem(); | ||
| } | ||
|
|
||
| @AfterEach | ||
| public void tearDown() throws Exception { | ||
| cleanupResources(); | ||
| } | ||
|
|
||
| private static Stream<Arguments> configParams() { | ||
| Object[][] data = new Object[][] { | ||
| {BulkInsertSortMode.GLOBAL_SORT, true, true}, | ||
| {BulkInsertSortMode.PARTITION_SORT, false, true}, | ||
| {BulkInsertSortMode.NONE, false, false} | ||
| }; | ||
| return Stream.of(data).map(Arguments::of); | ||
| } | ||
|
|
||
| @ParameterizedTest(name = "[{index}] {0}") | ||
| @MethodSource("configParams") | ||
| public void testBulkInsertInternalPartitioner(BulkInsertSortMode sortMode, | ||
| boolean isGloballySorted, boolean isLocallySorted) | ||
| throws Exception { | ||
| Dataset<Row> records1 = generateTestRecords(); | ||
| Dataset<Row> records2 = generateTestRecords(); | ||
| testBulkInsertInternalPartitioner(BulkInsertInternalPartitionerWithRowsFactory.get(sortMode), | ||
| records1, isGloballySorted, isLocallySorted, generateExpectedPartitionNumRecords(records1)); | ||
| testBulkInsertInternalPartitioner(BulkInsertInternalPartitionerWithRowsFactory.get(sortMode), | ||
| records2, isGloballySorted, isLocallySorted, generateExpectedPartitionNumRecords(records2)); | ||
| } | ||
|
|
||
| private void testBulkInsertInternalPartitioner(BulkInsertPartitioner partitioner, | ||
| Dataset<Row> rows, | ||
| boolean isGloballySorted, boolean isLocallySorted, | ||
| Map<String, Long> expectedPartitionNumRecords) { | ||
| int numPartitions = 2; | ||
| Dataset<Row> actualRecords = (Dataset<Row>) partitioner.repartitionRecords(rows, numPartitions); | ||
| List<Row> collectedActualRecords = actualRecords.collectAsList(); | ||
| if (isGloballySorted) { | ||
| // Verify global order | ||
| verifyRowsAscendingOrder(collectedActualRecords); | ||
| } else if (isLocallySorted) { | ||
| // Verify local order | ||
| actualRecords.mapPartitions((MapPartitionsFunction<Row, Object>) input -> { | ||
| List<Row> partitionRows = new ArrayList<>(); | ||
| while (input.hasNext()) { | ||
| partitionRows.add(input.next()); | ||
| } | ||
| verifyRowsAscendingOrder(partitionRows); | ||
| return Collections.emptyList().iterator(); | ||
| }, SparkDatasetTestUtils.ENCODER); | ||
| } | ||
|
|
||
| // Verify number of records per partition path | ||
| Map<String, Long> actualPartitionNumRecords = new HashMap<>(); | ||
| for (Row record : collectedActualRecords) { | ||
| String partitionPath = record.getAs(HoodieRecord.PARTITION_PATH_METADATA_FIELD); | ||
| actualPartitionNumRecords.put(partitionPath, | ||
| actualPartitionNumRecords.getOrDefault(partitionPath, 0L) + 1); | ||
| } | ||
| assertEquals(expectedPartitionNumRecords, actualPartitionNumRecords); | ||
| } | ||
|
|
||
| public static Map<String, Long> generateExpectedPartitionNumRecords(Dataset<Row> rows) { | ||
| Dataset<Row> toReturn = rows.groupBy(HoodieRecord.PARTITION_PATH_METADATA_FIELD).count(); | ||
| List<Row> result = toReturn.collectAsList(); | ||
| Map<String, Long> returnMap = new HashMap<>(); | ||
| for (Row row : result) { | ||
| returnMap.put(row.getAs(HoodieRecord.PARTITION_PATH_METADATA_FIELD), (Long) row.getAs("count")); | ||
| } | ||
| return returnMap; | ||
| } | ||
|
|
||
| public Dataset<Row> generateTestRecords() { | ||
| Dataset<Row> rowsPart1 = SparkDatasetTestUtils.getRandomRows(sqlContext, 100, HoodieTestDataGenerator.DEFAULT_FIRST_PARTITION_PATH, false); | ||
| Dataset<Row> rowsPart2 = SparkDatasetTestUtils.getRandomRows(sqlContext, 150, HoodieTestDataGenerator.DEFAULT_SECOND_PARTITION_PATH, false); | ||
| return rowsPart1.union(rowsPart2); | ||
| } | ||
|
|
||
| private void verifyRowsAscendingOrder(List<Row> records) { | ||
| List<Row> expectedRecords = new ArrayList<>(records); | ||
| Collections.sort(expectedRecords, Comparator.comparing(o -> (o.getAs(HoodieRecord.PARTITION_PATH_METADATA_FIELD) + "+" + o.getAs(HoodieRecord.RECORD_KEY_METADATA_FIELD)))); | ||
| assertEquals(expectedRecords, records); | ||
| } | ||
|
|
||
| } |
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.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
drop
Internalfrom the name?Uh oh!
There was an error while loading. Please reload this page.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Existing factory class for write client path is called BulkInsertInternalPartitionerFactory. hence named it this way. Reason is that, we have an interface called BulkInsertPartitioner. we have few out of the box partitioners and we could have user defined as well. hence the naming for these factories as internal. I can fix the name for both the factories if you prefer.