You are viewing a plain text version of this content. The canonical link for it is here.
Posted to notifications@kyuubi.apache.org by GitBox <gi...@apache.org> on 2022/08/17 16:35:47 UTC

[GitHub] [incubator-kyuubi] permanentstar commented on a diff in pull request #3260: Initial implementation of the Hive Connector based on the Spark datasource V2

permanentstar commented on code in PR #3260:
URL: https://github.com/apache/incubator-kyuubi/pull/3260#discussion_r948182682


##########
extensions/spark/kyuubi-spark-connector-hive/src/main/scala/org/apache/kyuubi/spark/connector/hive/HiveTableCatalog.scala:
##########
@@ -0,0 +1,410 @@
+/*
+ * 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.kyuubi.spark.connector.hive
+
+import java.net.URI
+import java.util
+
+import scala.collection.JavaConverters._
+import scala.collection.mutable
+
+import org.apache.hadoop.conf.Configuration
+import org.apache.spark.SparkConf
+import org.apache.spark.internal.Logging
+import org.apache.spark.sql.SparkSession
+import org.apache.spark.sql.catalyst.{SQLConfHelper, TableIdentifier}
+import org.apache.spark.sql.catalyst.analysis.{NamespaceAlreadyExistsException, NoSuchDatabaseException, NoSuchNamespaceException, NoSuchTableException, TableAlreadyExistsException}
+import org.apache.spark.sql.catalyst.catalog._
+import org.apache.spark.sql.catalyst.catalog.CatalogTypes.TablePartitionSpec
+import org.apache.spark.sql.catalyst.expressions.Expression
+import org.apache.spark.sql.catalyst.util.quoteIfNeeded
+import org.apache.spark.sql.connector.catalog.{Identifier, NamespaceChange, SupportsNamespaces, Table, TableCatalog, TableChange}
+import org.apache.spark.sql.connector.catalog.NamespaceChange.RemoveProperty
+import org.apache.spark.sql.connector.expressions.Transform
+import org.apache.spark.sql.execution.datasources.DataSource
+import org.apache.spark.sql.hive.kyuubi.connector.HiveConnectorHelper.{catalogV2Util, convertTransforms, postExternalCatalogEvent, HiveExternalCatalog, HiveMetastoreCatalog, HiveSessionCatalog}
+import org.apache.spark.sql.internal.StaticSQLConf.CATALOG_IMPLEMENTATION
+import org.apache.spark.sql.types.StructType
+import org.apache.spark.sql.util.CaseInsensitiveStringMap
+
+import org.apache.kyuubi.spark.connector.hive.HiveTableCatalog.{toCatalogDatabase, CatalogDatabaseHelper, IdentifierHelper, NamespaceHelper}
+
+/**
+ * A [[TableCatalog]] that wrap HiveExternalCatalog to as V2 CatalogPlugin instance to access Hive.
+ */
+class HiveTableCatalog(sparkSession: SparkSession)
+  extends TableCatalog with SQLConfHelper with SupportsNamespaces with Logging {
+
+  def this() = this(SparkSession.active)
+
+  private val sc = sparkSession.sparkContext
+
+  private val sessionState = sparkSession.sessionState
+
+  private var catalogName: String = _
+
+  private var catalogOptions: CaseInsensitiveStringMap = _
+
+  private var catalog: HiveSessionCatalog = _
+
+  val NAMESPACE_RESERVED_PROPERTIES =
+    Seq(
+      SupportsNamespaces.PROP_COMMENT,
+      SupportsNamespaces.PROP_LOCATION,
+      SupportsNamespaces.PROP_OWNER)
+
+  private lazy val hadoopConf: Configuration = {
+    val conf = sparkSession.sessionState.newHadoopConf()
+    catalogOptions.asScala.foreach {
+      case (k, v) => conf.set(k, v)
+      case _ =>
+    }
+    conf
+  }
+
+  private lazy val sparkConf: SparkConf = {
+    val conf = sparkSession.sparkContext.getConf
+    catalogOptions.asScala.foreach {
+      case (k, v) => conf.set(k, v)
+    }
+    conf
+  }
+
+  def hadoopConfiguration(): Configuration = hadoopConf
+
+  override def name(): String = {
+    require(catalogName != null, "The Hive table catalog is not initialed")
+    catalogName
+  }
+
+  override def initialize(name: String, options: CaseInsensitiveStringMap): Unit = {
+    assert(catalogName == null, "The Hive table catalog is already initialed.")
+    assert(
+      conf.getConf(CATALOG_IMPLEMENTATION) == "hive",
+      s"Require setting ${CATALOG_IMPLEMENTATION.key} to `hive` to enable hive support.")
+    catalogName = name
+    catalogOptions = options
+    catalog = new HiveSessionCatalog(
+      externalCatalogBuilder = () => externalCatalog,
+      globalTempViewManagerBuilder = () => sparkSession.sharedState.globalTempViewManager,
+      metastoreCatalog = new HiveMetastoreCatalog(sparkSession),
+      functionRegistry = sessionState.functionRegistry,
+      tableFunctionRegistry = sessionState.tableFunctionRegistry,
+      hadoopConf = hadoopConf,
+      parser = sessionState.sqlParser,
+      functionResourceLoader = sessionState.resourceLoader)
+  }
+
+  /**
+   * A catalog that interacts with external systems.
+   */
+  lazy val externalCatalog: ExternalCatalogWithListener = {
+    val externalCatalog = new HiveExternalCatalog(sparkConf, hadoopConf)

Review Comment:
   How do we deliver different conf variables for different hive clusters here, I think the variables should already been merged into the current hadoopConf ?



-- 
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: notifications-unsubscribe@kyuubi.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org


---------------------------------------------------------------------
To unsubscribe, e-mail: notifications-unsubscribe@kyuubi.apache.org
For additional commands, e-mail: notifications-help@kyuubi.apache.org