jaimin-shah commented on a change in pull request #728: adding support for complex keys URL: https://github.com/apache/incubator-hudi/pull/728#discussion_r296106944
########## File path: hoodie-spark/src/main/java/com/uber/hoodie/ComplexKeyGenerator.java ########## @@ -0,0 +1,81 @@ +/* + * 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 com.uber.hoodie; + +import com.uber.hoodie.common.model.HoodieKey; +import com.uber.hoodie.common.util.TypedProperties; +import com.uber.hoodie.exception.HoodieException; +import java.util.Arrays; +import java.util.List; +import org.apache.avro.generic.GenericRecord; + +/** + * Complex key generator, which takes names of fields to be used for recordKey and partitionPath as + * configs. + */ +public class ComplexKeyGenerator extends KeyGenerator { + + private static final String DEFAULT_PARTITION_PATH = "default"; + + private static final String DEFAULT_PARTITION_PATH_SEPARATOR = "/"; + + protected final List<String> recordKeyFields; + + protected final List<String> partitionPathFields; + + public ComplexKeyGenerator(TypedProperties props) { + super(props); + this.recordKeyFields = Arrays.asList(props.getString(DataSourceWriteOptions.RECORDKEY_FIELD_OPT_KEY()).split(",")); + this.partitionPathFields = Arrays.asList(props + .getString(DataSourceWriteOptions.PARTITIONPATH_FIELD_OPT_KEY()).split(",")); + } + + @Override + public HoodieKey getKey(GenericRecord record) { + if (recordKeyFields == null || partitionPathFields == null) { + throw new HoodieException( + "Unable to find field names for record key or partition path in cfg"); + } + StringBuilder recordKey = new StringBuilder(); + for (String recordKeyField : recordKeyFields) { + recordKey.append(recordKeyField + ":" + DataSourceUtils.getNestedFieldValAsString(record, recordKeyField) + ","); Review comment: That example will work fine but what if separator` : ` is part of data? `US, :A => US::A` and `US: , A => US::A` due to this I am in favor of keeping recordKeyField as part of key. ---------------------------------------------------------------- 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: [email protected] With regards, Apache Git Services
