我希望使用schemaname.tablename
检查Hive中是否存在表pysparkSQL
。
Scala spark.catalog.tableExists("schemaname.tablename")
.However中有一个选项,相同的功能在pySpark
中不可用。
寻找一种快速和干净的方法来使用PySpark
检查Hive表是否存在
发布于 2019-09-23 17:51:35
pyspark.sql.catalog
模块包括在spark >= 2.3.0中
如果您使用的是spark < 2.3.0,您可以使用如下所示:
spark._jsparkSession.catalog().tableExists("schema.table")
True
spark._jsparkSession.catalog().tableExists("schema.table_false")
False
或
spark.catalog._jcatalog.tableExists("schema.table")
True
发布于 2019-09-23 17:50:37
在PySpark中,可以这样做:
spark.catalog._jcatalog.tableExists("schemaname.tablename")
发布于 2019-09-23 19:08:56
以下是一些更多的选择。首先,让我们使用df.write.saveAsTable("your_table")
从任意df创建一些随机表。
选项1-火花>= 2.0
使用spark.catalog.listTables
i,即:
"your_table" in [t.name for t in spark.catalog.listTables("default")] == True
选项2-火花>= 1.3
使用sqlContext.tableNames
i,即:
"your_table" in sqlContext.tableNames("default") == True
选项3
使用spark.sql
i,即:
spark.sql("SHOW TABLES LIKE 'your_table'").count() == 1
https://stackoverflow.com/questions/58067388
复制相似问题