-
Notifications
You must be signed in to change notification settings - Fork 3.2k
Flink: Custom partitioner for bucket partitions #7161
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
Merged
Changes from all commits
Commits
Show all changes
24 commits
Select commit
Hold shift + click to select a range
7084612
First batch of code for a new Bucket Partitioning functionality
schongloo 5eaf718
Updating code style.
schongloo 4d84e49
Addressing PR comments
schongloo fe54373
Addressing PR comment: the BucketPartitioner should be provided a Par…
schongloo c7b86d7
Addressing build check problems
schongloo 93d7c2e
Addressing PR comments:
schongloo 75fad88
Addressing PR comments, pending:
schongloo 264fc7d
Addressing PR comments, pending:
schongloo 19fa9d6
Addressing PR comments, pending:
schongloo 2afa7ad
Addressing PR comments, pending:
schongloo 767a2da
- Creating the HadoopCatalogExtension.java for Junit5
schongloo 4c40ee6
Removing the need for an additiona/external @TempDir for the HadoopCa…
schongloo 7310f9b
- Splitting tests across TestBucketPartitionerFlinkIcebergSink (E2E),…
schongloo 26dae02
Additional clean up
schongloo 3bc5956
Checkstyle fixes
schongloo 7c11879
Addressing PR comments
schongloo b44bd99
Using RowData as the main type across tests.
schongloo 1651804
Refactoring the partitioning/schema utilities
schongloo 836daa6
Latest round of PR comments
schongloo a488d12
Refactoring enum static methods
schongloo dc4b6f4
Latest round of PR comments
schongloo 0cb7f37
Latest round of PR comments
schongloo c42ce13
Last PR comment
schongloo 87e83b7
Merge branch 'master' into feature/bucket_partitioner
schongloo 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
70 changes: 70 additions & 0 deletions
70
...k/v1.16/flink/src/main/java/org/apache/iceberg/flink/sink/BucketPartitionKeySelector.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,70 @@ | ||
| /* | ||
| * 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.iceberg.flink.sink; | ||
|
|
||
| import java.util.stream.IntStream; | ||
| import org.apache.flink.api.java.functions.KeySelector; | ||
| import org.apache.flink.table.data.RowData; | ||
| import org.apache.flink.table.types.logical.RowType; | ||
| import org.apache.iceberg.PartitionKey; | ||
| import org.apache.iceberg.PartitionSpec; | ||
| import org.apache.iceberg.Schema; | ||
| import org.apache.iceberg.flink.RowDataWrapper; | ||
|
|
||
| /** | ||
| * A {@link KeySelector} that extracts the bucketId from a data row's bucket partition as the key. | ||
| * To be used with the {@link BucketPartitioner}. | ||
| */ | ||
| class BucketPartitionKeySelector implements KeySelector<RowData, Integer> { | ||
|
|
||
| private final Schema schema; | ||
| private final PartitionKey partitionKey; | ||
| private final RowType flinkSchema; | ||
| private final int bucketFieldPosition; | ||
|
|
||
| private transient RowDataWrapper rowDataWrapper; | ||
|
|
||
| BucketPartitionKeySelector(PartitionSpec partitionSpec, Schema schema, RowType flinkSchema) { | ||
| this.schema = schema; | ||
| this.partitionKey = new PartitionKey(partitionSpec, schema); | ||
| this.flinkSchema = flinkSchema; | ||
| this.bucketFieldPosition = getBucketFieldPosition(partitionSpec); | ||
| } | ||
|
|
||
| private int getBucketFieldPosition(PartitionSpec partitionSpec) { | ||
| int bucketFieldId = BucketPartitionerUtil.getBucketFieldId(partitionSpec); | ||
| return IntStream.range(0, partitionSpec.fields().size()) | ||
| .filter(i -> partitionSpec.fields().get(i).fieldId() == bucketFieldId) | ||
| .toArray()[0]; | ||
| } | ||
|
|
||
| private RowDataWrapper lazyRowDataWrapper() { | ||
| if (rowDataWrapper == null) { | ||
| rowDataWrapper = new RowDataWrapper(flinkSchema, schema.asStruct()); | ||
| } | ||
|
|
||
| return rowDataWrapper; | ||
|
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: iceberg style adds an empty line after the end of a control block |
||
| } | ||
|
|
||
| @Override | ||
| public Integer getKey(RowData rowData) { | ||
| partitionKey.partition(lazyRowDataWrapper().wrap(rowData)); | ||
| return partitionKey.get(bucketFieldPosition, Integer.class); | ||
| } | ||
| } | ||
103 changes: 103 additions & 0 deletions
103
flink/v1.16/flink/src/main/java/org/apache/iceberg/flink/sink/BucketPartitioner.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,103 @@ | ||
| /* | ||
| * 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.iceberg.flink.sink; | ||
|
|
||
| import org.apache.flink.api.common.functions.Partitioner; | ||
| import org.apache.iceberg.PartitionSpec; | ||
| import org.apache.iceberg.relocated.com.google.common.base.Preconditions; | ||
|
|
||
| /** | ||
| * This partitioner will redirect records to writers deterministically based on the Bucket partition | ||
| * spec. It'll attempt to optimize the file size written depending on whether numPartitions is | ||
| * greater, less or equal than the maxNumBuckets. Note: The current implementation only supports ONE | ||
| * bucket in the partition spec. | ||
| */ | ||
| class BucketPartitioner implements Partitioner<Integer> { | ||
|
|
||
| static final String BUCKET_NULL_MESSAGE = "bucketId cannot be null"; | ||
| static final String BUCKET_LESS_THAN_LOWER_BOUND_MESSAGE = | ||
| "Invalid bucket ID %s: must be non-negative."; | ||
| static final String BUCKET_GREATER_THAN_UPPER_BOUND_MESSAGE = | ||
| "Invalid bucket ID %s: must be less than bucket limit: %s."; | ||
|
|
||
| private final int maxNumBuckets; | ||
|
|
||
| // To hold the OFFSET of the next writer to use for any bucket, only used when writers > the | ||
| // number of buckets | ||
| private final int[] currentBucketWriterOffset; | ||
|
|
||
| BucketPartitioner(PartitionSpec partitionSpec) { | ||
| this.maxNumBuckets = BucketPartitionerUtil.getMaxNumBuckets(partitionSpec); | ||
| this.currentBucketWriterOffset = new int[maxNumBuckets]; | ||
| } | ||
|
|
||
| /** | ||
| * Determine the partition id based on the following criteria: If the number of writers <= the | ||
| * number of buckets, an evenly distributed number of buckets will be assigned to each writer (one | ||
| * writer -> many buckets). Conversely, if the number of writers > the number of buckets the logic | ||
| * is handled by the {@link #getPartitionWithMoreWritersThanBuckets | ||
| * getPartitionWritersGreaterThanBuckets} method. | ||
| * | ||
| * @param bucketId the bucketId for each request | ||
| * @param numPartitions the total number of partitions | ||
| * @return the partition id (writer) to use for each request | ||
| */ | ||
| @Override | ||
| public int partition(Integer bucketId, int numPartitions) { | ||
|
stevenzwu marked this conversation as resolved.
|
||
| Preconditions.checkNotNull(bucketId, BUCKET_NULL_MESSAGE); | ||
| Preconditions.checkArgument(bucketId >= 0, BUCKET_LESS_THAN_LOWER_BOUND_MESSAGE, bucketId); | ||
| Preconditions.checkArgument( | ||
| bucketId < maxNumBuckets, BUCKET_GREATER_THAN_UPPER_BOUND_MESSAGE, bucketId, maxNumBuckets); | ||
|
|
||
| if (numPartitions <= maxNumBuckets) { | ||
| return bucketId % numPartitions; | ||
| } else { | ||
| return getPartitionWithMoreWritersThanBuckets(bucketId, numPartitions); | ||
| } | ||
| } | ||
|
|
||
| /*- | ||
| * If the number of writers > the number of buckets each partitioner will keep a state of multiple | ||
|
stevenzwu marked this conversation as resolved.
|
||
| * writers per bucket as evenly as possible, and will round-robin the requests across them, in this | ||
| * case each writer will target only one bucket at all times (many writers -> one bucket). Example: | ||
| * Configuration: numPartitions (writers) = 5, maxBuckets = 2 | ||
| * Expected behavior: | ||
| * - Records for Bucket 0 will be "round robin" between Writers 0, 2 and 4 | ||
| * - Records for Bucket 1 will always use Writer 1 and 3 | ||
| * Notes: | ||
| * - maxNumWritersPerBucket determines when to reset the currentBucketWriterOffset to 0 for this bucketId | ||
| * - When numPartitions is not evenly divisible by maxBuckets, some buckets will have one more writer (extraWriter). | ||
| * In this example Bucket 0 has an "extra writer" to consider before resetting its offset to 0. | ||
| * | ||
| * @return the destination partition index (writer subtask id) | ||
| */ | ||
| private int getPartitionWithMoreWritersThanBuckets(int bucketId, int numPartitions) { | ||
| int currentOffset = currentBucketWriterOffset[bucketId]; | ||
| // Determine if this bucket requires an "extra writer" | ||
| int extraWriter = bucketId < (numPartitions % maxNumBuckets) ? 1 : 0; | ||
| // The max number of writers this bucket can have | ||
| int maxNumWritersPerBucket = (numPartitions / maxNumBuckets) + extraWriter; | ||
|
|
||
| // Increment the writer offset or reset if it's reached the max for this bucket | ||
| int nextOffset = currentOffset == maxNumWritersPerBucket - 1 ? 0 : currentOffset + 1; | ||
| currentBucketWriterOffset[bucketId] = nextOffset; | ||
|
|
||
| return bucketId + (maxNumBuckets * currentOffset); | ||
| } | ||
| } | ||
125 changes: 125 additions & 0 deletions
125
flink/v1.16/flink/src/main/java/org/apache/iceberg/flink/sink/BucketPartitionerUtil.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,125 @@ | ||
| /* | ||
| * 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.iceberg.flink.sink; | ||
|
|
||
| import java.util.List; | ||
| import java.util.Objects; | ||
| import java.util.stream.Collectors; | ||
| import org.apache.flink.api.java.tuple.Tuple2; | ||
| import org.apache.iceberg.PartitionSpec; | ||
| import org.apache.iceberg.relocated.com.google.common.base.Preconditions; | ||
| import org.apache.iceberg.transforms.PartitionSpecVisitor; | ||
|
|
||
| final class BucketPartitionerUtil { | ||
| static final String BAD_NUMBER_OF_BUCKETS_ERROR_MESSAGE = | ||
| "Invalid number of buckets: %s (must be 1)"; | ||
|
|
||
| private BucketPartitionerUtil() {} | ||
|
|
||
| /** | ||
| * Determines whether the PartitionSpec has one and only one Bucket definition | ||
| * | ||
| * @param partitionSpec the partition spec in question | ||
| * @return whether the PartitionSpec has only one Bucket | ||
| */ | ||
| static boolean hasOneBucketField(PartitionSpec partitionSpec) { | ||
| List<Tuple2<Integer, Integer>> bucketFields = getBucketFields(partitionSpec); | ||
| return bucketFields != null && bucketFields.size() == 1; | ||
| } | ||
|
|
||
| /** | ||
| * Extracts the Bucket definition from a PartitionSpec. | ||
| * | ||
| * @param partitionSpec the partition spec in question | ||
| * @return the Bucket definition in the form of a tuple (fieldId, maxNumBuckets) | ||
| */ | ||
| private static Tuple2<Integer, Integer> getBucketFieldInfo(PartitionSpec partitionSpec) { | ||
| List<Tuple2<Integer, Integer>> bucketFields = getBucketFields(partitionSpec); | ||
| Preconditions.checkArgument( | ||
| bucketFields.size() == 1, | ||
| BucketPartitionerUtil.BAD_NUMBER_OF_BUCKETS_ERROR_MESSAGE, | ||
| bucketFields.size()); | ||
| return bucketFields.get(0); | ||
| } | ||
|
|
||
| static int getBucketFieldId(PartitionSpec partitionSpec) { | ||
| return getBucketFieldInfo(partitionSpec).f0; | ||
| } | ||
|
|
||
| static int getMaxNumBuckets(PartitionSpec partitionSpec) { | ||
| return getBucketFieldInfo(partitionSpec).f1; | ||
| } | ||
|
|
||
| private static List<Tuple2<Integer, Integer>> getBucketFields(PartitionSpec spec) { | ||
| return PartitionSpecVisitor.visit(spec, new BucketPartitionSpecVisitor()).stream() | ||
| .filter(Objects::nonNull) | ||
| .collect(Collectors.toList()); | ||
| } | ||
|
|
||
| private static class BucketPartitionSpecVisitor | ||
| implements PartitionSpecVisitor<Tuple2<Integer, Integer>> { | ||
| @Override | ||
| public Tuple2<Integer, Integer> identity(int fieldId, String sourceName, int sourceId) { | ||
| return null; | ||
| } | ||
|
|
||
| @Override | ||
| public Tuple2<Integer, Integer> bucket( | ||
| int fieldId, String sourceName, int sourceId, int numBuckets) { | ||
| return new Tuple2<>(fieldId, numBuckets); | ||
| } | ||
|
|
||
| @Override | ||
| public Tuple2<Integer, Integer> truncate( | ||
| int fieldId, String sourceName, int sourceId, int width) { | ||
| return null; | ||
| } | ||
|
|
||
| @Override | ||
| public Tuple2<Integer, Integer> year(int fieldId, String sourceName, int sourceId) { | ||
| return null; | ||
| } | ||
|
|
||
| @Override | ||
| public Tuple2<Integer, Integer> month(int fieldId, String sourceName, int sourceId) { | ||
| return null; | ||
| } | ||
|
|
||
| @Override | ||
| public Tuple2<Integer, Integer> day(int fieldId, String sourceName, int sourceId) { | ||
| return null; | ||
| } | ||
|
|
||
| @Override | ||
| public Tuple2<Integer, Integer> hour(int fieldId, String sourceName, int sourceId) { | ||
| return null; | ||
| } | ||
|
|
||
| @Override | ||
| public Tuple2<Integer, Integer> alwaysNull(int fieldId, String sourceName, int sourceId) { | ||
| return null; | ||
| } | ||
|
|
||
| @Override | ||
| public Tuple2<Integer, Integer> unknown( | ||
| int fieldId, String sourceName, int sourceId, String transform) { | ||
| return null; | ||
| } | ||
| } | ||
| } |
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
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.