stczwd commented on a change in pull request #1332: Doris sink for Spark 
Structured Streaming
URL: https://github.com/apache/incubator-doris/pull/1332#discussion_r296202071
 
 

 ##########
 File path: 
extension/spark-sink/src/main/scala/org/apache/spark/sql/palo/PaloBulkLoadTask.scala
 ##########
 @@ -0,0 +1,276 @@
+/*
+ * 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.palo
+
+import java.io.{BufferedInputStream, BufferedOutputStream, BufferedWriter, 
DataOutputStream, File, FileInputStream, FileOutputStream, InputStream, 
IOException, OutputStream, OutputStreamWriter}
+import java.net.{HttpURLConnection, ProtocolException, SocketTimeoutException, 
URL}
+import java.nio.ByteBuffer
+
+import org.json4s._
+import org.json4s.jackson.JsonMethods._
+
+import org.apache.spark.internal.Logging
+import org.apache.spark.sql.{AnalysisException, SparkSession}
+import org.apache.spark.sql.catalyst.InternalRow
+import org.apache.spark.sql.types.{StringType, StructType}
+
+/**
+ * Use `BULK LOAD` to load data to Palo by using http protocol to send data.
+ * To use http `PUT` data to palo, we need write intermediate contents to tmp 
files on
+ * local file system and when load finished, delete them
+ *
+ * Note: the size of tmp file should not exceed 1GB (advise)
+ */
+private[palo] class PaloBulkLoadTask (
+    sparkSession: SparkSession,
+    batchId: Long,
+    checkpointRoot: String,
+    parameters: Map[String, String],
+    schema: StructType)
+  extends PaloWriteTask(sparkSession, batchId, checkpointRoot, parameters, 
schema) with Logging {
+
+  // use the "label" as the file name which will send to palo by http protocol
+  // the tmp file to store data generated by Dataset
+  private[palo] var file = new File(label)
+  private[palo] val httpMaxRetries: Int =
+      parameters.getOrElse(PaloConfig.HTTP_MAX_RETRIES, "3").toInt
+  private[palo] val httpConnectTimeoutMs: Int =
+      parameters.getOrElse(PaloConfig.HTTP_CONNECT_TIMEOUT_MS, "60000").toInt
+  private[palo] val httpReadTimeoutMs: Int =
+      parameters.getOrElse(PaloConfig.HTTP_READ_TIMEOUT_MS, "10000").toInt
+  private[palo] val bulkLoadReadBufferSize: Int =
+      parameters.getOrElse(PaloConfig.BULK_LOAD_READ_BUFFERSIZE, 
"1048576").toInt // 1MB
+  private[palo] val httpPort: String = 
parameters.getOrElse(PaloConfig.HTTP_PORT, "8030")
+  private[palo] val separator: String =
+      parameters.getOrElse(PaloConfig.SEPARATOR, ",") // default ","
+  private[palo] val maxFilterRatio =
+      parameters.getOrElse(PaloConfig.MAX_FILTER_RATIO, "0").toDouble
+  assert(maxFilterRatio >= 0 && maxFilterRatio <= 1, "maxFilterRatio must 
between [0,1]")
+
+  private[palo] val urlSuffix = s"api/${database}/${table}/_load" +
+          s"?label=${label}&${PaloConfig.MAX_FILTER_RATIO}=${maxFilterRatio}" +
+          s"&column_separator=${separator}"
+
+  private[palo] var outputStream: FileOutputStream = null
+  private[palo] var writer: BufferedWriter = null
+
+  // init outputStream to write tmp file
+  init
+
+  private def init() {
+    try {
+      // dataPath may exists for failover, delete the dataPath and create new 
one firstly
+      if (file.exists) {
+        file.delete
+        logInfo(s"file:${file.getAbsolutePath} exists, delete it successfully")
+      }
+      assert(file.createNewFile, "create file failed when init")
+      outputStream = new FileOutputStream(file)
+      writer = new BufferedWriter(new OutputStreamWriter(outputStream, 
"UTF-8"))
+
+      logInfo(s"init successfully, create a new file:${file.getAbsolutePath} 
to store contents")
+    } catch {
+      case e: IOException =>
+        logError(s"PaloBulkLoadTask init failed: ${e.getMessage}")
+        try {
+          if (outputStream != null) {
+            outputStream.close
+          }
+        } catch {
+          case e1: IOException =>
+          logError(s"close outputStream error when init 
failed:${e1.getMessage}")
+        }
+        try {
+          if (writer != null) {
+            writer.close
+          }
+        } catch {
+           case e1: IOException =>
+           logError(s"close BufferedWriter error when init 
failed:${e1.getMessage}")
+        }
+        throw e
+    }
+  }
+
+  override def writeToFile(content: String): Long = {
+    writer.write(content, 0, content.length)
+    content.getBytes("UTF-8").length
+  }
+
+  override def loadFileToPaloTable() {
+    // 1. flush content inf buffer to local disk
+    try {
+      writer.flush
+      outputStream.flush
+      outputStream.close
+      writer.close
+    } catch {
+      case e: IOException =>
+        logError(s"loadFileToPaloTable fail: ${e.getMessage}")
+        throw e
+    }
+
+    // 2. send File to Palo by http
+    loadByHttp(generateDistUrl)
+  }
+
+  override def deleteFile() {
+    if (needDelete && file.exists) {
+      try {
+        if (file.delete) {
+          logInfo(s"delete ${file.getAbsolutePath} successfully")
+        } else {
+          logWarning(s"delete ${file.getAbsolutePath} failed")
 
 Review comment:
   need an exception?

----------------------------------------------------------------
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:
us...@infra.apache.org


With regards,
Apache Git Services

---------------------------------------------------------------------
To unsubscribe, e-mail: dev-unsubscr...@doris.apache.org
For additional commands, e-mail: dev-h...@doris.apache.org

Reply via email to