nsivabalan commented on code in PR #8107: URL: https://github.com/apache/hudi/pull/8107#discussion_r1131845254
########## hudi-spark-datasource/hudi-spark-common/src/main/scala/org/apache/hudi/AutoRecordKeyGenerationUtils.scala: ########## @@ -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.hudi + +import org.apache.avro.generic.GenericRecord +import org.apache.hudi.DataSourceWriteOptions.INSERT_DROP_DUPS +import org.apache.hudi.common.config.HoodieConfig +import org.apache.hudi.common.model.{HoodieRecord, WriteOperationType} +import org.apache.hudi.common.table.HoodieTableConfig +import org.apache.hudi.config.HoodieWriteConfig +import org.apache.hudi.exception.HoodieException +import org.apache.spark.TaskContext + +object AutoRecordKeyGenerationUtils { + + // supported operation types when auto generation of record keys is enabled. + val supportedOperations: Set[String] = + Set(WriteOperationType.INSERT, WriteOperationType.BULK_INSERT, WriteOperationType.DELETE, + WriteOperationType.INSERT_OVERWRITE, WriteOperationType.INSERT_OVERWRITE_TABLE, + WriteOperationType.DELETE_PARTITION).map(_.name()) + + def validateParamsForAutoGenerationOfRecordKeys(parameters: Map[String, String], + operation: WriteOperationType, hoodieConfig: HoodieConfig): Unit = { + val autoGenerateRecordKeys: Boolean = parameters.getOrElse(HoodieTableConfig.AUTO_GENERATE_RECORD_KEYS.key(), + HoodieTableConfig.AUTO_GENERATE_RECORD_KEYS.defaultValue()).toBoolean + + if (autoGenerateRecordKeys) { + // check for supported operations. + if (!supportedOperations.contains(operation.name())) { + throw new HoodieException(operation.name() + " is not supported with Auto generation of record keys. " + + "Supported operations are : " + supportedOperations) + } + // de-dup is not supported with auto generation of record keys + if (parameters.getOrElse(HoodieWriteConfig.COMBINE_BEFORE_INSERT.key(), + HoodieWriteConfig.COMBINE_BEFORE_INSERT.defaultValue()).toBoolean) { + throw new HoodieException("Enabling " + HoodieWriteConfig.COMBINE_BEFORE_INSERT.key() + " is not supported with auto generation of record keys "); + } + // drop dupes is not supported + if (hoodieConfig.getBoolean(INSERT_DROP_DUPS)) { + throw new HoodieException("Enabling " + INSERT_DROP_DUPS.key() + " is not supported with auto generation of record keys "); + } + // virtual keys are not supported with auto generation of record keys. + if (!parameters.getOrElse(HoodieTableConfig.POPULATE_META_FIELDS.key(), HoodieTableConfig.POPULATE_META_FIELDS.defaultValue().toString).toBoolean) { + throw new HoodieException("Disabling " + HoodieTableConfig.POPULATE_META_FIELDS.key() + " is not supported with auto generation of record keys"); + } + } + } + + /** + * Auto Generate record keys when auto generation config is enabled. + * <ol> + * <li>Generated keys will be unique not only w/in provided [[org.apache.spark.sql.DataFrame]], but + * globally unique w/in the target table</li> + * <li>Generated keys have minimal overhead (to compute, persist and read)</li> + * </ol> + * + * Keys adhere to the following format: + * + * [instantTime]_[PartitionId]_[RowId] + * + * where + * instantTime refers to the commit time of the batch being ingested. + * PartitionId refers to spark's partition Id. + * RowId refers to the row index within the spark partition. + * + * @param autoGenerateKeys true if auto generation of record keys is enabled. false otherwise. + * @param genRecsItr Iterator of GenericRecords. + * @param instantTime commit time of the batch. + * @return Iterator of Pair of GenericRecord and Optionally generated record key. + */ + def mayBeAutoGenerateRecordKeys(autoGenerateKeys : Boolean, genRecsItr: Iterator[GenericRecord], instantTime: String): Iterator[(GenericRecord, Option[String])] = { + var rowId = 0 + val sparkPartitionId = TaskContext.getPartitionId() + + // we will override record keys if auto generation if keys is enabled. + genRecsItr.map(avroRecord => + if (autoGenerateKeys) { + val recordKey : String = HoodieRecord.generateSequenceId(instantTime, sparkPartitionId, rowId) + rowId += 1 Review Comment: since these are executed from within a single executor. I didn't find any need. might confuse developers later if we change to Atomic. so, would prefer to keep it this way. -- 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-unsubscr...@hudi.apache.org For queries about this service, please contact Infrastructure at: us...@infra.apache.org