You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@hudi.apache.org by GitBox <gi...@apache.org> on 2023/01/16 23:13:09 UTC
[GitHub] [hudi] nsivabalan opened a new pull request, #7681: [HUDI-5535] Support all record key generations in any keyGen class
nsivabalan opened a new pull request, #7681:
URL: https://github.com/apache/hudi/pull/7681
### Change Logs
As of now, record key generation and partition path generation are tightly coupled based on the key gen class used. But these could be de-coupled. For eg, we can't have a complex record key generation with non-partitioned dataset since the NonpartitionedKeyGen uses simple key generation logic for record key generation. So, this patch introduces a separate RecordKeyGenerator interface and uses a factory to determine the record key generation strategy. Users don't need to set any additional param.
After this patch, this is how the record key generation strategy is determined.
- Users will have to configure the right value for "hoodie.datasource.write.recordkey.field" or enable "hoodie.auto.generate.record.keys".
a. If "hoodie.auto.generate.record.keys" is enabled, record keys will be auto generated internally and users don't need to set any value for "hoodie.datasource.write.recordkey.field".
b. else, if single field is set for "hoodie.datasource.write.recordkey.field", simple record key generation.
c. else its deduced as multi record key generation.
### Impact
Enables users to choose any record key generation along w/ any partition path generation strategy.
### Risk level (write none, low medium or high below)
low.
### Documentation Update
_Describe any necessary documentation update if there is any new feature, config, or user-facing change_
- _The config description must be updated if new configs are added or the default value of the configs are changed_
- _Any new feature or user-facing change requires updating the Hudi website. Please create a Jira ticket, attach the
ticket number here and follow the [instruction](https://hudi.apache.org/contribute/developer-setup#website) to make
changes to the website._
### Contributor's checklist
- [ ] Read through [contributor's guide](https://hudi.apache.org/contribute/how-to-contribute)
- [ ] Change Logs and Impact were stated clearly
- [ ] Adequate tests were added if applicable
- [ ] CI passed
--
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.
To unsubscribe, e-mail: commits-unsubscribe@hudi.apache.org
For queries about this service, please contact Infrastructure at:
users@infra.apache.org
[GitHub] [hudi] hudi-bot commented on pull request #7681: [HUDI-5535] Support any record key generations along w/ any partition path generation
Posted by GitBox <gi...@apache.org>.
hudi-bot commented on PR #7681:
URL: https://github.com/apache/hudi/pull/7681#issuecomment-1384675970
<!--
Meta data
{
"version" : 1,
"metaDataEntries" : [ {
"hash" : "b26ffe6600ce80dbeed7c43e5de72b6ff3ddeb99",
"status" : "FAILURE",
"url" : "https://dev.azure.com/apache-hudi-ci-org/785b6ef4-2f42-4a89-8f0e-5f0d7039a0cc/_build/results?buildId=14349",
"triggerID" : "b26ffe6600ce80dbeed7c43e5de72b6ff3ddeb99",
"triggerType" : "PUSH"
} ]
}-->
## CI report:
* b26ffe6600ce80dbeed7c43e5de72b6ff3ddeb99 Azure: [FAILURE](https://dev.azure.com/apache-hudi-ci-org/785b6ef4-2f42-4a89-8f0e-5f0d7039a0cc/_build/results?buildId=14349)
<details>
<summary>Bot commands</summary>
@hudi-bot supports the following commands:
- `@hudi-bot run azure` re-run the last Azure build
</details>
--
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.
To unsubscribe, e-mail: commits-unsubscribe@hudi.apache.org
For queries about this service, please contact Infrastructure at:
users@infra.apache.org
[GitHub] [hudi] nsivabalan commented on a diff in pull request #7681: [HUDI-5535] Support any record key generation along w/ any partition path generation
Posted by GitBox <gi...@apache.org>.
nsivabalan commented on code in PR #7681:
URL: https://github.com/apache/hudi/pull/7681#discussion_r1073998492
##########
hudi-client/hudi-client-common/src/main/java/org/apache/hudi/keygen/AutoRecordKeyGenerator.java:
##########
@@ -0,0 +1,235 @@
+/*
+ * 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.keygen;
+
+import org.apache.hudi.common.config.TypedProperties;
+import org.apache.hudi.common.util.collection.Pair;
+import org.apache.hudi.keygen.constant.KeyGeneratorOptions;
+
+import org.apache.avro.Schema;
+import org.apache.avro.generic.GenericRecord;
+
+import java.nio.charset.StandardCharsets;
+import java.util.ArrayDeque;
+import java.util.Arrays;
+import java.util.Comparator;
+import java.util.List;
+import java.util.PriorityQueue;
+import java.util.Queue;
+import java.util.Set;
+import java.util.UUID;
+import java.util.stream.Collectors;
+
+import static org.apache.hudi.keygen.CustomAvroKeyGenerator.SPLIT_REGEX;
+
+/**
+ * Auto record key generator. This generator will fetch values from the entire record based on some of the fields and determine the record key.
+ * Use-cases where users may not be able to configure record keys, can use this auto record key generator.
+ */
+public class AutoRecordKeyGenerator implements RecordKeyGenerator {
Review Comment:
fixed it in the new patch put up
--
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.
To unsubscribe, e-mail: commits-unsubscribe@hudi.apache.org
For queries about this service, please contact Infrastructure at:
users@infra.apache.org
[GitHub] [hudi] hudi-bot commented on pull request #7681: [HUDI-5535] Support any record key generations along w/ any partition path generation
Posted by GitBox <gi...@apache.org>.
hudi-bot commented on PR #7681:
URL: https://github.com/apache/hudi/pull/7681#issuecomment-1384672836
<!--
Meta data
{
"version" : 1,
"metaDataEntries" : [ {
"hash" : "b26ffe6600ce80dbeed7c43e5de72b6ff3ddeb99",
"status" : "UNKNOWN",
"url" : "TBD",
"triggerID" : "b26ffe6600ce80dbeed7c43e5de72b6ff3ddeb99",
"triggerType" : "PUSH"
} ]
}-->
## CI report:
* b26ffe6600ce80dbeed7c43e5de72b6ff3ddeb99 UNKNOWN
<details>
<summary>Bot commands</summary>
@hudi-bot supports the following commands:
- `@hudi-bot run azure` re-run the last Azure build
</details>
--
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.
To unsubscribe, e-mail: commits-unsubscribe@hudi.apache.org
For queries about this service, please contact Infrastructure at:
users@infra.apache.org
[GitHub] [hudi] nsivabalan commented on a diff in pull request #7681: [HUDI-5535] Support any record key generation along w/ any partition path generation
Posted by GitBox <gi...@apache.org>.
nsivabalan commented on code in PR #7681:
URL: https://github.com/apache/hudi/pull/7681#discussion_r1074000359
##########
hudi-client/hudi-client-common/src/main/java/org/apache/hudi/keygen/AutoRecordKeyGenerator.java:
##########
@@ -0,0 +1,235 @@
+/*
+ * 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.keygen;
+
+import org.apache.hudi.common.config.TypedProperties;
+import org.apache.hudi.common.util.collection.Pair;
+import org.apache.hudi.keygen.constant.KeyGeneratorOptions;
+
+import org.apache.avro.Schema;
+import org.apache.avro.generic.GenericRecord;
+
+import java.nio.charset.StandardCharsets;
+import java.util.ArrayDeque;
+import java.util.Arrays;
+import java.util.Comparator;
+import java.util.List;
+import java.util.PriorityQueue;
+import java.util.Queue;
+import java.util.Set;
+import java.util.UUID;
+import java.util.stream.Collectors;
+
+import static org.apache.hudi.keygen.CustomAvroKeyGenerator.SPLIT_REGEX;
+
+/**
+ * Auto record key generator. This generator will fetch values from the entire record based on some of the fields and determine the record key.
+ * Use-cases where users may not be able to configure record keys, can use this auto record key generator.
+ */
+public class AutoRecordKeyGenerator implements RecordKeyGenerator {
+
+ private final TypedProperties config;
+ private static final String HOODIE_PREFIX = "_hoodie";
+ private static final String DOT = ".";
+ private final int maxFieldsToConsider;
+ private final int numFieldsForKey;
+ private final Set<String> partitionFieldNames;
+ private int[][] fieldOrdering;
+
+ public AutoRecordKeyGenerator(TypedProperties config, List<String> partitionPathFields) {
+ this.config = config;
+ this.numFieldsForKey = config.getInteger(KeyGeneratorOptions.NUM_FIELDS_IN_KEYLESS_GENERATOR.key(), KeyGeneratorOptions.NUM_FIELDS_IN_KEYLESS_GENERATOR.defaultValue());
+ // cap the number of fields to order in case of large schemas
+ this.maxFieldsToConsider = numFieldsForKey * 3;
+ this.partitionFieldNames = partitionPathFields.stream().map(field -> field.split(SPLIT_REGEX)[0]).collect(Collectors.toSet());
+ }
+
+ @Override
+ public String getRecordKey(GenericRecord record) {
+ return buildKey(getFieldOrdering(record), record);
+ }
+
+ int[][] getFieldOrdering(GenericRecord genericRecord) {
+ if (fieldOrdering == null) {
+ fieldOrdering = buildFieldOrdering(genericRecord.getSchema().getFields());
+ }
+ return fieldOrdering;
+ }
+
+ /**
+ * Deterministically builds a key for the input value based on the provided fieldOrdering. The first {@link #numFieldsForKey} non-null values will be used to generate a string that is passed to
+ * {@link UUID#nameUUIDFromBytes(byte[])}.
+ * @param fieldOrdering an array of integer arrays. The integer arrays represent paths to a single field within the input object.
+ * @param input the input object that needs a key
+ * @return a deterministically generated {@link UUID}
+ * @param <T> the input object type
+ */
+ private <T> String buildKey(int[][] fieldOrdering, GenericRecord input) {
+ StringBuilder key = new StringBuilder();
+ int nonNullFields = 0;
+ for (int[] index : fieldOrdering) {
+ Object value = getFieldForRecord(input, index);
+ if (value == null) {
+ continue;
+ }
+ nonNullFields++;
+ key.append(value.hashCode());
Review Comment:
we are not trying to generate unique keys here. we want to generate record keys which are deterministic. also, adding field names will add additional storage. So, would prefer to avoid it.
also, if there are two records w/ same record key, both will be part of the snapshot read query.
btw, we are also planning to not support de-dup of any kind when auto generation of record keys are enabled. So
--
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.
To unsubscribe, e-mail: commits-unsubscribe@hudi.apache.org
For queries about this service, please contact Infrastructure at:
users@infra.apache.org
[GitHub] [hudi] nsivabalan commented on pull request #7681: [HUDI-5535] Support any record key generation along w/ any partition path generation
Posted by GitBox <gi...@apache.org>.
nsivabalan commented on PR #7681:
URL: https://github.com/apache/hudi/pull/7681#issuecomment-1387669769
Closing this in favor of https://github.com/apache/hudi/pull/7699
--
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.
To unsubscribe, e-mail: commits-unsubscribe@hudi.apache.org
For queries about this service, please contact Infrastructure at:
users@infra.apache.org
[GitHub] [hudi] nsivabalan closed pull request #7681: [HUDI-5535] Support any record key generation along w/ any partition path generation
Posted by GitBox <gi...@apache.org>.
nsivabalan closed pull request #7681: [HUDI-5535] Support any record key generation along w/ any partition path generation
URL: https://github.com/apache/hudi/pull/7681
--
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.
To unsubscribe, e-mail: commits-unsubscribe@hudi.apache.org
For queries about this service, please contact Infrastructure at:
users@infra.apache.org
[GitHub] [hudi] nsivabalan commented on a diff in pull request #7681: [HUDI-5535] Support any record key generation along w/ any partition path generation
Posted by GitBox <gi...@apache.org>.
nsivabalan commented on code in PR #7681:
URL: https://github.com/apache/hudi/pull/7681#discussion_r1071715411
##########
hudi-client/hudi-spark-client/src/main/java/org/apache/hudi/keygen/SparkRecordKeyGeneratorInterface.java:
##########
@@ -0,0 +1,57 @@
+/*
+ * 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.keygen;
+
+import org.apache.hudi.ApiMaturityLevel;
+import org.apache.hudi.PublicAPIMethod;
+
+import org.apache.spark.sql.Row;
+import org.apache.spark.sql.catalyst.InternalRow;
+import org.apache.spark.sql.types.StructType;
+import org.apache.spark.unsafe.types.UTF8String;
+
+/**
+ * Spark's record key generator interface to assist in generating record key for a given spark row.
+ */
+public interface SparkRecordKeyGeneratorInterface {
+
+ /**
+ * Extracts record key from Spark's {@link Row}
+ *
+ * @param row instance of {@link Row} from which record-key is extracted
+ * @return record's (primary) key
+ */
+ @PublicAPIMethod(maturity = ApiMaturityLevel.EVOLVING)
Review Comment:
yet to fix the row writer code path on supporting any record key generation w/ any key gen class. @lokeshj1703 is taking a stab at it.
--
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.
To unsubscribe, e-mail: commits-unsubscribe@hudi.apache.org
For queries about this service, please contact Infrastructure at:
users@infra.apache.org
[GitHub] [hudi] alexeykudinkin commented on a diff in pull request #7681: [HUDI-5535] Support any record key generation along w/ any partition path generation
Posted by GitBox <gi...@apache.org>.
alexeykudinkin commented on code in PR #7681:
URL: https://github.com/apache/hudi/pull/7681#discussion_r1072524628
##########
hudi-client/hudi-client-common/src/main/java/org/apache/hudi/keygen/AutoRecordKeyGenerator.java:
##########
@@ -0,0 +1,235 @@
+/*
+ * 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.keygen;
+
+import org.apache.hudi.common.config.TypedProperties;
+import org.apache.hudi.common.util.collection.Pair;
+import org.apache.hudi.keygen.constant.KeyGeneratorOptions;
+
+import org.apache.avro.Schema;
+import org.apache.avro.generic.GenericRecord;
+
+import java.nio.charset.StandardCharsets;
+import java.util.ArrayDeque;
+import java.util.Arrays;
+import java.util.Comparator;
+import java.util.List;
+import java.util.PriorityQueue;
+import java.util.Queue;
+import java.util.Set;
+import java.util.UUID;
+import java.util.stream.Collectors;
+
+import static org.apache.hudi.keygen.CustomAvroKeyGenerator.SPLIT_REGEX;
+
+/**
+ * Auto record key generator. This generator will fetch values from the entire record based on some of the fields and determine the record key.
+ * Use-cases where users may not be able to configure record keys, can use this auto record key generator.
+ */
+public class AutoRecordKeyGenerator implements RecordKeyGenerator {
Review Comment:
Please outline the algorithm this class employs in the java-doc
##########
hudi-client/hudi-client-common/src/main/java/org/apache/hudi/keygen/AutoRecordKeyGenerator.java:
##########
@@ -0,0 +1,235 @@
+/*
+ * 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.keygen;
+
+import org.apache.hudi.common.config.TypedProperties;
+import org.apache.hudi.common.util.collection.Pair;
+import org.apache.hudi.keygen.constant.KeyGeneratorOptions;
+
+import org.apache.avro.Schema;
+import org.apache.avro.generic.GenericRecord;
+
+import java.nio.charset.StandardCharsets;
+import java.util.ArrayDeque;
+import java.util.Arrays;
+import java.util.Comparator;
+import java.util.List;
+import java.util.PriorityQueue;
+import java.util.Queue;
+import java.util.Set;
+import java.util.UUID;
+import java.util.stream.Collectors;
+
+import static org.apache.hudi.keygen.CustomAvroKeyGenerator.SPLIT_REGEX;
+
+/**
+ * Auto record key generator. This generator will fetch values from the entire record based on some of the fields and determine the record key.
+ * Use-cases where users may not be able to configure record keys, can use this auto record key generator.
+ */
+public class AutoRecordKeyGenerator implements RecordKeyGenerator {
+
+ private final TypedProperties config;
+ private static final String HOODIE_PREFIX = "_hoodie";
+ private static final String DOT = ".";
+ private final int maxFieldsToConsider;
+ private final int numFieldsForKey;
+ private final Set<String> partitionFieldNames;
+ private int[][] fieldOrdering;
+
+ public AutoRecordKeyGenerator(TypedProperties config, List<String> partitionPathFields) {
+ this.config = config;
+ this.numFieldsForKey = config.getInteger(KeyGeneratorOptions.NUM_FIELDS_IN_KEYLESS_GENERATOR.key(), KeyGeneratorOptions.NUM_FIELDS_IN_KEYLESS_GENERATOR.defaultValue());
+ // cap the number of fields to order in case of large schemas
+ this.maxFieldsToConsider = numFieldsForKey * 3;
+ this.partitionFieldNames = partitionPathFields.stream().map(field -> field.split(SPLIT_REGEX)[0]).collect(Collectors.toSet());
+ }
+
+ @Override
+ public String getRecordKey(GenericRecord record) {
+ return buildKey(getFieldOrdering(record), record);
+ }
+
+ int[][] getFieldOrdering(GenericRecord genericRecord) {
+ if (fieldOrdering == null) {
+ fieldOrdering = buildFieldOrdering(genericRecord.getSchema().getFields());
+ }
+ return fieldOrdering;
+ }
+
+ /**
+ * Deterministically builds a key for the input value based on the provided fieldOrdering. The first {@link #numFieldsForKey} non-null values will be used to generate a string that is passed to
+ * {@link UUID#nameUUIDFromBytes(byte[])}.
+ * @param fieldOrdering an array of integer arrays. The integer arrays represent paths to a single field within the input object.
+ * @param input the input object that needs a key
+ * @return a deterministically generated {@link UUID}
+ * @param <T> the input object type
+ */
+ private <T> String buildKey(int[][] fieldOrdering, GenericRecord input) {
+ StringBuilder key = new StringBuilder();
+ int nonNullFields = 0;
+ for (int[] index : fieldOrdering) {
+ Object value = getFieldForRecord(input, index);
+ if (value == null) {
+ continue;
+ }
+ nonNullFields++;
+ key.append(value.hashCode());
Review Comment:
Neither key nor hash-code generation could not be implemented that way: we have to delimit the elements since otherwise following cases are indistinguishable:
- key1=12
- key1=1, key2=2
--
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.
To unsubscribe, e-mail: commits-unsubscribe@hudi.apache.org
For queries about this service, please contact Infrastructure at:
users@infra.apache.org
[GitHub] [hudi] alexeykudinkin commented on a diff in pull request #7681: [HUDI-5535] Support any record key generation along w/ any partition path generation
Posted by GitBox <gi...@apache.org>.
alexeykudinkin commented on code in PR #7681:
URL: https://github.com/apache/hudi/pull/7681#discussion_r1080653334
##########
hudi-client/hudi-client-common/src/main/java/org/apache/hudi/keygen/AutoRecordKeyGenerator.java:
##########
@@ -0,0 +1,235 @@
+/*
+ * 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.keygen;
+
+import org.apache.hudi.common.config.TypedProperties;
+import org.apache.hudi.common.util.collection.Pair;
+import org.apache.hudi.keygen.constant.KeyGeneratorOptions;
+
+import org.apache.avro.Schema;
+import org.apache.avro.generic.GenericRecord;
+
+import java.nio.charset.StandardCharsets;
+import java.util.ArrayDeque;
+import java.util.Arrays;
+import java.util.Comparator;
+import java.util.List;
+import java.util.PriorityQueue;
+import java.util.Queue;
+import java.util.Set;
+import java.util.UUID;
+import java.util.stream.Collectors;
+
+import static org.apache.hudi.keygen.CustomAvroKeyGenerator.SPLIT_REGEX;
+
+/**
+ * Auto record key generator. This generator will fetch values from the entire record based on some of the fields and determine the record key.
+ * Use-cases where users may not be able to configure record keys, can use this auto record key generator.
+ */
+public class AutoRecordKeyGenerator implements RecordKeyGenerator {
+
+ private final TypedProperties config;
+ private static final String HOODIE_PREFIX = "_hoodie";
+ private static final String DOT = ".";
+ private final int maxFieldsToConsider;
+ private final int numFieldsForKey;
+ private final Set<String> partitionFieldNames;
+ private int[][] fieldOrdering;
+
+ public AutoRecordKeyGenerator(TypedProperties config, List<String> partitionPathFields) {
+ this.config = config;
+ this.numFieldsForKey = config.getInteger(KeyGeneratorOptions.NUM_FIELDS_IN_KEYLESS_GENERATOR.key(), KeyGeneratorOptions.NUM_FIELDS_IN_KEYLESS_GENERATOR.defaultValue());
+ // cap the number of fields to order in case of large schemas
+ this.maxFieldsToConsider = numFieldsForKey * 3;
+ this.partitionFieldNames = partitionPathFields.stream().map(field -> field.split(SPLIT_REGEX)[0]).collect(Collectors.toSet());
+ }
+
+ @Override
+ public String getRecordKey(GenericRecord record) {
+ return buildKey(getFieldOrdering(record), record);
+ }
+
+ int[][] getFieldOrdering(GenericRecord genericRecord) {
+ if (fieldOrdering == null) {
+ fieldOrdering = buildFieldOrdering(genericRecord.getSchema().getFields());
+ }
+ return fieldOrdering;
+ }
+
+ /**
+ * Deterministically builds a key for the input value based on the provided fieldOrdering. The first {@link #numFieldsForKey} non-null values will be used to generate a string that is passed to
+ * {@link UUID#nameUUIDFromBytes(byte[])}.
+ * @param fieldOrdering an array of integer arrays. The integer arrays represent paths to a single field within the input object.
+ * @param input the input object that needs a key
+ * @return a deterministically generated {@link UUID}
+ * @param <T> the input object type
+ */
+ private <T> String buildKey(int[][] fieldOrdering, GenericRecord input) {
+ StringBuilder key = new StringBuilder();
+ int nonNullFields = 0;
+ for (int[] index : fieldOrdering) {
+ Object value = getFieldForRecord(input, index);
+ if (value == null) {
+ continue;
+ }
+ nonNullFields++;
+ key.append(value.hashCode());
Review Comment:
It's tangential whether we de-dup or not -- we should not increase probability of collision (which will have side-effects). Instead of adding field-names we can just add delimiters b/w the values
--
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.
To unsubscribe, e-mail: commits-unsubscribe@hudi.apache.org
For queries about this service, please contact Infrastructure at:
users@infra.apache.org