dongjoon-hyun commented on a change in pull request #23369: [SPARK-26429][SS]add jdbc sink for Structured Streaming. URL: https://github.com/apache/spark/pull/23369#discussion_r243724586
########## File path: sql/core/src/main/scala/org/apache/spark/sql/execution/datasources/jdbc/JdbcStreamingWriteSupport.scala ########## @@ -0,0 +1,238 @@ +/* + * 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.spark.sql.execution.datasources.jdbc + +import java.sql.{Connection, PreparedStatement, SQLException} +import java.util.Locale + +import scala.collection.mutable.ArrayBuffer + +import org.apache.spark.internal.Logging +import org.apache.spark.sql.Row +import org.apache.spark.sql.catalyst.InternalRow +import org.apache.spark.sql.catalyst.encoders.RowEncoder +import org.apache.spark.sql.execution.datasources.jdbc.JdbcUtils.makeSetter +import org.apache.spark.sql.jdbc.JdbcDialects +import org.apache.spark.sql.sources.v2.writer.{DataWriter, WriterCommitMessage} +import org.apache.spark.sql.sources.v2.writer.streaming.{StreamingDataWriterFactory, StreamingWriteSupport} +import org.apache.spark.sql.types.StructType + +/** + * Dummy commit message. The DataSourceV2 framework requires a commit message implementation but we + * don't need to really send one. + */ +case object JdbcWriterCommitMessage extends WriterCommitMessage + +/** + * A [[StreamingWriteSupport]] for jdbc writing. + * Responsible for generating the writer factory. + */ +class JdbcStreamingWriteSupport( + schema: StructType, + options: Map[String, String] + ) extends StreamingWriteSupport with Logging { + + override def commit(epochId: Long, messages: Array[WriterCommitMessage]): Unit = { + logInfo(s"epoch ${epochId} of JdbcStreamWriter commited!") + } + override def abort(epochId: Long, messages: Array[WriterCommitMessage]): Unit = { + logInfo(s"epoch ${epochId} of JdbcStreamWriter aborted!") + } + override def createStreamingWriterFactory(): StreamingDataWriterFactory = { + new JdbcStreamWriterFactory(schema, options) + } +} + +/** + * A [[StreamingDataWriterFactory]] for jdbc writing. + * Will be serialized and sent to executors to generate the per-task data writers. + */ +case class JdbcStreamWriterFactory( + schema: StructType, + options: Map[String, String] + ) extends StreamingDataWriterFactory { + + override def createWriter( + partitionId: Int, + taskId: Long, + epochId: Long): DataWriter[InternalRow] = { + JdbcStreamDataWriter(schema, options) + } +} + +/** + * A [[DataWriter]] for Jdbc writing. + * One data writer will be created in each partition to process incoming rows. + */ +case class JdbcStreamDataWriter( + schema: StructType, + options: Map[String, String] + ) extends DataWriter[InternalRow] with Logging { + + private val jdbcOptions = new JDBCOptions(options) + + // use a local cache for batch write to jdbc. + private val batchSize = jdbcOptions.batchSize + private val localBuffer = new ArrayBuffer[Row](batchSize) + private val maxRetryNum = jdbcOptions.maxRetryNumber + + // the first part is the column name list, the second part is the placeholder string. + private val sqlPart: (String, String) = { + val columnListBuilder = new StringBuilder() + val holderListBuilder = new StringBuilder() + schema.fields.foreach { field => + columnListBuilder.append(",").append(field.name) + holderListBuilder.append(",?") + } + assert(columnListBuilder.nonEmpty, "Empty schema of data!") + (columnListBuilder.substring(1), holderListBuilder.substring(1)) + } + + private val sql: String = s"REPLACE INTO ${jdbcOptions.tableOrQuery}" + + s" ( ${sqlPart._1} ) values ( ${sqlPart._2} )" + + logInfo(s"Sql string for jdbc writing is ${sql}") Review comment: Is this useful for users? If this is only for developers, let's not use `logInfo`. ---------------------------------------------------------------- This is an automated message from the Apache Git Service. To respond to the message, please log on 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 --------------------------------------------------------------------- To unsubscribe, e-mail: [email protected] For additional commands, e-mail: [email protected]
