drop表

u59ebvdq  于 2021-06-25  发布在  Hive
关注(0)|答案(1)|浏览(372)

这个 SparkSession.catalog 对象具有一系列与元存储交互的方法,即:

['cacheTable',
 'clearCache',
 'createExternalTable',
 'createTable',
 'currentDatabase',
 'dropGlobalTempView',
 'dropTempView',
 'isCached',
 'listColumns',
 'listDatabases',
 'listFunctions',
 'listTables',
 'recoverPartitions',
 'refreshByPath',
 'refreshTable',
 'registerFunction',
 'setCurrentDatabase',
 'uncacheTable']

不幸的是,似乎没有程序化的方法来删除表。
有多种方法可以实现这一点

spark.sql(f"drop table my_table")

spark._jsparkSession.sharedState().externalCatalog().dropTable(db, table, True, True)

但与一个简单的,尽管缺失的, dropTable 方法?
有更好的办法吗?

k75qkfdt

k75qkfdt1#

以上提到的方法都是最常用的方法。我没有别的感觉。。
但从这些文件中我可以看到另一种方式。。。
你可以试试这个 org.apache.spark.sql.hive.HiveUtils 有好吃的东西给你。
我对python不太在行,您可以看到下面的scala示例,并遵循python的相同方法。

package org.apache.spark.sql.hive {
import org.apache.spark.sql.hive.HiveUtils
import org.apache.spark.SparkContext
object utils {
    def dropTable(sc: SparkContext, dbName: String, tableName: String, ignoreIfNotExists: Boolean, purge: Boolean): Unit = {
      HiveUtils
          .newClientForMetadata(sc.getConf, sc.hadoopConfiguration)
          .dropTable(dbName, tableName, ignoreIfNotExists, false)
    }
  }
}

打电话的人会说

import org.apache.spark.sql.hive.utils
utils.dropTable(sc, "default", "my_table", true, true)

相关问题