nsivabalan commented on code in PR #5470: URL: https://github.com/apache/hudi/pull/5470#discussion_r870235097
########## hudi-spark-datasource/hudi-spark-common/src/main/scala/org/apache/hudi/HoodieDatasetBulkInsertHelper.scala: ########## @@ -0,0 +1,163 @@ +/* + * 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.hudi.common.config.TypedProperties +import org.apache.hudi.common.model.HoodieRecord +import org.apache.hudi.common.util.ReflectionUtils +import org.apache.hudi.config.HoodieWriteConfig +import org.apache.hudi.keygen.BuiltinKeyGenerator +import org.apache.hudi.table.BulkInsertPartitioner +import org.apache.spark.internal.Logging +import org.apache.spark.rdd.RDD +import org.apache.spark.sql.HoodieUnsafeRDDUtils.createDataFrame +import org.apache.spark.sql.HoodieUnsafeRowUtils.{composeNestedFieldPath, getNestedInternalRowValue} +import org.apache.spark.sql.catalyst.InternalRow +import org.apache.spark.sql.catalyst.expressions.GenericInternalRow +import org.apache.spark.sql.types.{StringType, StructField, StructType} +import org.apache.spark.sql.{DataFrame, Dataset, HoodieUnsafeRDDUtils, Row} +import org.apache.spark.unsafe.types.UTF8String + +import scala.collection.JavaConverters.asScalaBufferConverter + +object HoodieDatasetBulkInsertHelper extends Logging { + + /** + * Prepares [[DataFrame]] for bulk-insert into Hudi table, taking following steps: + * + * <ol> + * <li>Invoking configured [[KeyGenerator]] to produce record key, alas partition-path value</li> + * <li>Prepends Hudi meta-fields to every row in the dataset</li> + * <li>Dedupes rows (if necessary)</li> + * <li>Partitions dataset using provided [[partitioner]]</li> + * </ol> + */ + def prepareForBulkInsert(df: DataFrame, + config: HoodieWriteConfig, + partitioner: BulkInsertPartitioner[Dataset[Row]], + isGlobalIndex: Boolean, + dropPartitionColumns: Boolean): Dataset[Row] = { + val populateMetaFields = config.populateMetaFields() + val schema = df.schema + + val keyGeneratorClassName = config.getStringOrThrow(DataSourceWriteOptions.KEYGENERATOR_CLASS_NAME, + "Key-generator class name is required") + + val prependedRdd: RDD[InternalRow] = + df.queryExecution.toRdd.mapPartitions { iter => Review Comment: I am seeing some perf hit w/ this code change. will wait to sync up with Alexey on this. -- 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: [email protected] For queries about this service, please contact Infrastructure at: [email protected]
