nsivabalan commented on a change in pull request #1433: [HUDI-728]: Implement 
custom key generator
URL: https://github.com/apache/incubator-hudi/pull/1433#discussion_r400521746
 
 

 ##########
 File path: 
hudi-spark/src/main/java/org/apache/hudi/keygen/CustomKeyGenerator.java
 ##########
 @@ -0,0 +1,110 @@
+/*
+ * 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.DataSourceWriteOptions;
+import org.apache.hudi.common.model.HoodieKey;
+import org.apache.hudi.common.util.TypedProperties;
+
+import org.apache.avro.generic.GenericRecord;
+import org.apache.hudi.exception.HoodieDeltaStreamerException;
+import org.apache.hudi.exception.HoodieKeyException;
+
+import java.util.Arrays;
+import java.util.List;
+import java.util.stream.Collectors;
+
+public class CustomKeyGenerator extends KeyGenerator {
+
+  protected final List<String> recordKeyFields;
+  protected final List<String> partitionPathFields;
+  protected final TypedProperties properties;
+  private static final String DEFAULT_PARTITION_PATH_SEPARATOR = "/";
+  private static final String SPLIT_REGEX = ":";
+
+  public CustomKeyGenerator(TypedProperties props) {
+    super(props);
+    this.properties = props;
+    this.recordKeyFields = 
Arrays.stream(props.getString(DataSourceWriteOptions.RECORDKEY_FIELD_OPT_KEY()).split(",")).map(String::trim).collect(Collectors.toList());
+    this.partitionPathFields =
+      
Arrays.stream(props.getString(DataSourceWriteOptions.PARTITIONPATH_FIELD_OPT_KEY()).split(",")).map(String::trim).collect(Collectors.toList());
+  }
+
+  @Override
+  public HoodieKey getKey(GenericRecord record) {
+    //call function to get the record key
+    String recordKey = getRecordKey(record);
+    //call function to get the partition key based on the type for that 
partition path field
+    String partitionPath = getPartitionPath(record, null);
+    return new HoodieKey(recordKey, partitionPath);
+  }
+
+  @Override
+  public String getPartitionPath(GenericRecord record, String pathField) {
+    if (partitionPathFields == null) {
+      throw new HoodieKeyException("Unable to find field names for partition 
path in cfg");
+    }
+
+    String partitionPathField;
+    KeyGenerator keyGenerator;
+    StringBuilder partitionPath = new StringBuilder();
+    boolean nonPartitionedTable = false;
+    for (String field : partitionPathFields) {
+      String[] fieldWithType = field.split(SPLIT_REGEX);
+      if (fieldWithType.length != 2) {
+        throw new HoodieKeyException("Unable to find field names for partition 
path in proper format");
+      }
+
+      partitionPathField = fieldWithType[0];
+      switch (fieldWithType[1]) {
+        case "simple":
+          keyGenerator = new SimpleKeyGenerator(properties);
+          break;
+        case "timestampBased":
+          keyGenerator = new TimestampBasedKeyGenerator(properties);
+          break;
+        case "noPartition":
+          keyGenerator = new NonpartitionedKeyGenerator(properties);
 
 Review comment:
   Do you think we can add tests for these cases as well? 

----------------------------------------------------------------
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.
 
For queries about this service, please contact Infrastructure at:
us...@infra.apache.org


With regards,
Apache Git Services

Reply via email to