taklwu commented on code in PR #166:
URL: https://github.com/apache/hbase-connectors/pull/166#discussion_r4066429031


##########
spark4/hbase-spark4/src/main/scala/org/apache/hadoop/hbase/spark/datasources/HBaseDataWriter.scala:
##########
@@ -0,0 +1,146 @@
+/*
+ * 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.hadoop.hbase.spark.datasources
+
+import java.util.ArrayList
+import org.apache.hadoop.hbase.TableName
+import org.apache.hadoop.hbase.client.{Put, Table}
+import org.apache.hadoop.hbase.spark.{HBaseConnectionCache, Logging, 
SmartConnection}
+import org.apache.hadoop.hbase.util.Bytes
+import org.apache.spark.sql.catalyst.InternalRow
+import org.apache.spark.sql.connector.write.{DataWriter, WriterCommitMessage}
+import org.apache.spark.sql.types._
+import org.apache.yetus.audience.InterfaceAudience
+
+/**
+ * This is a new class in the spark4 module. Implements 
DataWriter[InternalRow] for the DS V2 write path.
+ * Each instance handles one Spark partition on an executor. Converts 
InternalRow to HBase Put operations
+ * and writes them via BufferedMutator for efficient client-side batching.
+ *
+ * In the spark 3 DS V1 model, this logic was inside DefaultSource.insert() 
which used
+ * rdd.map(convertToPut).saveAsHadoopDataset() with the old mapred 
TableOutputFormat.
+ *
+ * @param schema
+ * @param properties
+ * @param catalog
+ * @param wrappedConf
+ */
[email protected]
+class HBaseDataWriter(
+    schema: StructType,
+    properties: Map[String, String],
+    catalog: HBaseTableCatalog,
+    wrappedConf: SerializableConfiguration)
+    extends DataWriter[InternalRow]
+    with Logging {
+
+  private val conf = wrappedConf.value
+  private val connection: SmartConnection = 
HBaseConnectionCache.getConnection(conf)
+  private val tableName = 
TableName.valueOf(s"${catalog.namespace}:${catalog.name}")
+  private val table: Table = connection.getTable(tableName)
+
+  private val timestamp = 
properties.get(HBaseSparkConf.TIMESTAMP).map(_.toLong)
+
+  private val rkFields = catalog.getRowKey
+  private val rkIdxedFields = rkFields.map { f =>
+    (schema.fieldIndex(f.colName), f)
+  }
+  private val colIdxedFields = schema.fieldNames
+    .filter(name => !rkFields.map(_.colName).contains(name))
+    .map(name => (schema.fieldIndex(name), catalog.sMap.getField(name)))
+
+  private val batchSize = properties
+    .get(HBaseSparkConf.BULKGET_SIZE)
+    .map(_.toInt)
+    .getOrElse(HBaseSparkConf.DEFAULT_BULKGET_SIZE)
+
+  private val putBuffer = new ArrayList[Put](batchSize)
+
+  override def write(record: InternalRow): Unit = {
+    val rowKeyBytes = buildRowKey(record)
+    val put = timestamp.fold(new Put(rowKeyBytes))(new Put(rowKeyBytes, _))
+
+    colIdxedFields.foreach { case (idx, field) =>
+      if (!record.isNullAt(idx)) {

Review Comment:
   nit: buildRowKey always reads row-key columns with no null guard, maybe 
double check if there is null pointer and throws/handle it.



##########
spark4/hbase-spark4/src/main/scala/org/apache/hadoop/hbase/spark/datasources/HBaseDataWriter.scala:
##########
@@ -0,0 +1,146 @@
+/*
+ * 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.hadoop.hbase.spark.datasources
+
+import java.util.ArrayList
+import org.apache.hadoop.hbase.TableName
+import org.apache.hadoop.hbase.client.{Put, Table}
+import org.apache.hadoop.hbase.spark.{HBaseConnectionCache, Logging, 
SmartConnection}
+import org.apache.hadoop.hbase.util.Bytes
+import org.apache.spark.sql.catalyst.InternalRow
+import org.apache.spark.sql.connector.write.{DataWriter, WriterCommitMessage}
+import org.apache.spark.sql.types._
+import org.apache.yetus.audience.InterfaceAudience
+
+/**
+ * This is a new class in the spark4 module. Implements 
DataWriter[InternalRow] for the DS V2 write path.
+ * Each instance handles one Spark partition on an executor. Converts 
InternalRow to HBase Put operations
+ * and writes them via BufferedMutator for efficient client-side batching.

Review Comment:
   nit: Javadoc says BufferedMutator, but below code uses Table.put



##########
spark4/hbase-spark4/src/main/scala/org/apache/hadoop/hbase/spark/datasources/HBaseDataWriter.scala:
##########
@@ -0,0 +1,146 @@
+/*
+ * 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.hadoop.hbase.spark.datasources
+
+import java.util.ArrayList
+import org.apache.hadoop.hbase.TableName
+import org.apache.hadoop.hbase.client.{Put, Table}
+import org.apache.hadoop.hbase.spark.{HBaseConnectionCache, Logging, 
SmartConnection}
+import org.apache.hadoop.hbase.util.Bytes
+import org.apache.spark.sql.catalyst.InternalRow
+import org.apache.spark.sql.connector.write.{DataWriter, WriterCommitMessage}
+import org.apache.spark.sql.types._
+import org.apache.yetus.audience.InterfaceAudience
+
+/**
+ * This is a new class in the spark4 module. Implements 
DataWriter[InternalRow] for the DS V2 write path.
+ * Each instance handles one Spark partition on an executor. Converts 
InternalRow to HBase Put operations
+ * and writes them via BufferedMutator for efficient client-side batching.
+ *
+ * In the spark 3 DS V1 model, this logic was inside DefaultSource.insert() 
which used
+ * rdd.map(convertToPut).saveAsHadoopDataset() with the old mapred 
TableOutputFormat.
+ *
+ * @param schema
+ * @param properties
+ * @param catalog
+ * @param wrappedConf
+ */
[email protected]
+class HBaseDataWriter(
+    schema: StructType,
+    properties: Map[String, String],
+    catalog: HBaseTableCatalog,
+    wrappedConf: SerializableConfiguration)
+    extends DataWriter[InternalRow]
+    with Logging {
+
+  private val conf = wrappedConf.value
+  private val connection: SmartConnection = 
HBaseConnectionCache.getConnection(conf)
+  private val tableName = 
TableName.valueOf(s"${catalog.namespace}:${catalog.name}")
+  private val table: Table = connection.getTable(tableName)
+
+  private val timestamp = 
properties.get(HBaseSparkConf.TIMESTAMP).map(_.toLong)
+
+  private val rkFields = catalog.getRowKey
+  private val rkIdxedFields = rkFields.map { f =>
+    (schema.fieldIndex(f.colName), f)
+  }
+  private val colIdxedFields = schema.fieldNames
+    .filter(name => !rkFields.map(_.colName).contains(name))
+    .map(name => (schema.fieldIndex(name), catalog.sMap.getField(name)))
+
+  private val batchSize = properties
+    .get(HBaseSparkConf.BULKGET_SIZE)
+    .map(_.toInt)
+    .getOrElse(HBaseSparkConf.DEFAULT_BULKGET_SIZE)
+
+  private val putBuffer = new ArrayList[Put](batchSize)
+
+  override def write(record: InternalRow): Unit = {
+    val rowKeyBytes = buildRowKey(record)
+    val put = timestamp.fold(new Put(rowKeyBytes))(new Put(rowKeyBytes, _))
+
+    colIdxedFields.foreach { case (idx, field) =>
+      if (!record.isNullAt(idx)) {
+        val valueBytes = getValueBytes(record, idx, field)
+        put.addColumn(field.cfBytes, field.colBytes, valueBytes)
+      }
+    }
+
+    putBuffer.add(put)
+    if (putBuffer.size() >= batchSize) {
+      flushPuts()
+    }
+  }
+
+  override def commit(): WriterCommitMessage = {
+    flushPuts()
+    table.close()
+    connection.close()
+    HBaseWriterCommitMessage()
+  }
+
+  override def abort(): Unit = {
+    table.close()
+    connection.close()
+  }
+
+  override def close(): Unit = {}
+
+  private def flushPuts(): Unit = {
+    if (!putBuffer.isEmpty) {
+      table.put(putBuffer)
+      putBuffer.clear()
+    }
+  }
+
+  private def buildRowKey(record: InternalRow): Array[Byte] = {
+    val rowBytes = rkIdxedFields.map { case (idx, field) =>
+      getValueBytes(record, idx, field)
+    }
+    val totalLen = rowBytes.foldLeft(0)(_ + _.length)
+    val result = new Array[Byte](totalLen)
+    var offset = 0
+    rowBytes.foreach { bytes =>
+      System.arraycopy(bytes, 0, result, offset, bytes.length)
+      offset += bytes.length
+    }
+    result
+  }
+
+  private val MILLIS_PER_DAY = 86400000L
+
+  private def getValueBytes(row: InternalRow, idx: Int, field: Field): 
Array[Byte] = {
+    field.dt match {
+      case BooleanType => Bytes.toBytes(row.getBoolean(idx))

Review Comment:
   yup, this is needed, but you can do in the next PR.



-- 
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]

Reply via email to