chenboat commented on code in PR #13748: URL: https://github.com/apache/pinot/pull/13748#discussion_r1706234250
########## pinot-connectors/pinot-spark-3-connector/src/main/scala/org/apache/pinot/connector/spark/v3/datasource/PinotDataWriter.scala: ########## @@ -0,0 +1,246 @@ +/** + * 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.pinot.connector.spark.v3.datasource + +import org.apache.commons.io.FileUtils +import org.apache.pinot.common.utils.TarGzCompressionUtils +import org.apache.pinot.connector.spark.common.PinotDataSourceWriteOptions +import org.apache.spark.sql.connector.write.{DataWriter, WriterCommitMessage} +import org.apache.pinot.segment.local.segment.creator.impl.SegmentIndexCreationDriverImpl +import org.apache.pinot.segment.spi.creator.SegmentGeneratorConfig +import org.apache.pinot.spi.config.table.{IndexingConfig, SegmentsValidationAndRetentionConfig, TableConfig, TableCustomConfig, TenantConfig} +import org.apache.pinot.spi.data.readers.GenericRow +import org.apache.pinot.spi.data.Schema +import org.apache.pinot.spi.ingestion.batch.spec.Constants +import org.apache.pinot.spi.utils.DataSizeUtils +import org.apache.spark.sql.catalyst +import org.apache.spark.sql.types.StructType +import org.slf4j.{Logger, LoggerFactory} + +import java.io.File +import java.nio.file.Files +import java.util.regex.Pattern + +class PinotDataWriter[InternalRow]( + partitionId: Int, + taskId: Long, + writeOptions: PinotDataSourceWriteOptions, + writeSchema: StructType, + pinotSchema: Schema) + extends DataWriter[org.apache.spark.sql.catalyst.InternalRow] with AutoCloseable { + private val logger: Logger = LoggerFactory.getLogger(classOf[PinotDataWriter[InternalRow]]) + logger.info("PinotDataWriter created with writeOptions: {}, partitionId: {}, taskId: {}", + (writeOptions, partitionId, taskId)) + + val tableName: String = writeOptions.tableName + val savePath: String = writeOptions.savePath + val bufferedRecordReader: PinotBufferedRecordReader = new PinotBufferedRecordReader() + + override def write(record: catalyst.InternalRow): Unit = { + bufferedRecordReader.write(internalRowToGenericRow(record)) + } + + override def commit(): WriterCommitMessage = { + val segmentName = getSegmentName + val segmentDir = generateSegment(segmentName) + val segmentTarFile = tarSegmentDir(segmentName, segmentDir) + pushSegmentTarFile(segmentTarFile) + new SuccessWriterCommitMessage(segmentName) + } + + // This method is used to generate the segment name based on the format provided in the write options + // The format can contain variables like {partitionId} + // Currently supported variables are `partitionId`, `table` + // It also supports the following, python inspired format specifier for digit formatting: + // `{partitionId:05}` + // which will zero pad partitionId up to five characters. + // + // Some examples: Review Comment: These examples suggest that there are two possible formats: one with partition id in front and the other with partition id after table name. Are these the ONLY two options allowed? Just curious why supporting 2 instead of 1? -- 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...@pinot.apache.org For queries about this service, please contact Infrastructure at: us...@infra.apache.org --------------------------------------------------------------------- To unsubscribe, e-mail: commits-unsubscr...@pinot.apache.org For additional commands, e-mail: commits-h...@pinot.apache.org