wchevreuil commented on code in PR #162: URL: https://github.com/apache/hbase-connectors/pull/162#discussion_r3969837351
########## spark4/hbase-spark4/src/main/scala/org/apache/hadoop/hbase/spark/datasources/HBasePartitionReader.scala: ########## @@ -0,0 +1,421 @@ +/* + * 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.fs.Path +import org.apache.hadoop.hbase.{CellUtil, HBaseConfiguration, TableName} +import org.apache.hadoop.hbase.client.{Get, Query, Result, ResultScanner, Scan, Table} +import org.apache.hadoop.hbase.spark.{AndLogicExpression, DynamicLogicExpression, + EqualLogicExpression, GreaterThanLogicExpression, GreaterThanOrEqualLogicExpression, + HBaseConnectionCache, IsNullLogicExpression, LessThanLogicExpression, + LessThanOrEqualLogicExpression, Logging, OrLogicExpression, PassThroughLogicExpression, + PushdownMappedField, SmartConnection, SparkSQLPushDownFilter, StartsWithLogicExpression} +import org.apache.hadoop.hbase.util.Bytes +import org.apache.spark.sql.catalyst.InternalRow +import org.apache.spark.sql.catalyst.expressions.GenericInternalRow +import org.apache.spark.sql.catalyst.util.DateTimeUtils +import org.apache.spark.sql.types.Decimal +import org.apache.spark.sql.connector.read.PartitionReader +import org.apache.spark.sql.sources._ +import org.apache.spark.sql.types._ +import org.apache.spark.unsafe.types.UTF8String +import org.apache.yetus.audience.InterfaceAudience +import scala.collection.mutable.ListBuffer +import scala.jdk.CollectionConverters._ + +/** + * This is a new class in the spark4 module. Extends PartitionReader[InternalRow] for reading data from HBase regions. + * The actual execution: opens an HBase scanner on the partition's range, attaches the SparkSQLPushDownFilter, + * reads Result objects, and converts them to InternalRow. Implements next()/get()/close(). + * + * + * In the spark 3 DS V1 model, this logic was inside DefaultSource.buildScan() + * which returned an RDD[Row] with its own compute() method. + * + * Ranges are executed as Scan operations, whilst points are executed as batched Get operations. This mirrors the spark3 + * HBaseTableScanRDD.compute() behavior. + */ [email protected] +class HBasePartitionReader( + partition: HBaseInputPartition, + requiredSchema: StructType, + properties: Map[String, String], + catalog: HBaseTableCatalog, + pushedFilters: Array[Filter], + encoderClsName: String, + usePushDownColumnFilter: Boolean) + extends PartitionReader[InternalRow] + with Logging { + + private val conf = HBaseConfiguration.create() + private val configResources = properties.get(HBaseSparkConf.HBASE_CONFIG_LOCATION) + configResources.foreach(_.split(",").foreach(r => conf.addResource(new Path(r)))) + + private val connection: SmartConnection = HBaseConnectionCache.getConnection(conf) + private val tableName = s"${catalog.namespace}:${catalog.name}" + private val table: Table = connection.getTable(TableName.valueOf(tableName)) + + private val requiredFields = requiredSchema.fieldNames.map(catalog.sMap.getField(_)) + private val filterFields = extractFilterFields(pushedFilters) + private val scanFields = (requiredFields ++ filterFields).distinct.filterNot(_.isRowKey) + private val pushDownFilter: Option[SparkSQLPushDownFilter] = buildPushDownFilter() + + private val bulkGetSize = properties + .get(HBaseSparkConf.BULKGET_SIZE) + .map(_.toInt) + .getOrElse(HBaseSparkConf.DEFAULT_BULKGET_SIZE) + + private val blockCacheEnable = properties + .get(HBaseSparkConf.QUERY_CACHEBLOCKS) + .map(_.toBoolean) + .getOrElse(HBaseSparkConf.DEFAULT_QUERY_CACHEBLOCKS) + + private val scanners = new ListBuffer[ResultScanner]() + + private val resultIterator: Iterator[Result] = { + val scanIterators = partition.scanRanges.map { range => + val scanner = buildScanner(range) + scanners += scanner + scannerToIterator(scanner) + } + val getIterator = if (partition.points.nonEmpty) { + buildGets(partition.points) + } else { + Iterator.empty + } + scanIterators.foldLeft(Iterator.empty: Iterator[Result])(_ ++ _) ++ getIterator + } + + private var currentResult: Result = _ + + override def next(): Boolean = { + if (resultIterator.hasNext) { + currentResult = resultIterator.next() + true + } else { + false + } + } + + override def get(): InternalRow = { + val fields = requiredSchema.fieldNames.map(catalog.sMap.getField(_)) + val rowKey = currentResult.getRow + val keyFields = catalog.getRowKey Review Comment: Addressing on the next commit. -- 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]
