spark-scala-如何检查配置单元中是否存在表 [英] spark - scala - How can I check if a table exists in hive
本文介绍了spark-scala-如何检查配置单元中是否存在表的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我必须使用spark(1.6.2)scala检查蜂巢中是否存在表
I have to check whether a table exists in hive using spark(1.6.2) scala
如果没有,我必须创建一个空的数据框并将其保存为配置单元表.
If it doesn't I have to create an empty dataframe and save that as a hive table.
如果存在,则覆盖现有表.
If it exists, then overwrite the existing table.
我需要一个返回布尔值的函数,据此我可以做出上述决定(是创建新表还是覆盖现有表)
I need a function that returns boolean based on which I could take the above mentioned decisions(of whether to create new table or overwrite existing one)
推荐答案
1.x:
def tableExists(table: String, sqlContext: SQLContext) =
sqlContext.tableNames.contains(table)
2.x:
def tableExists(table: String, spark: SparkSession) =
spark.catalog.tableExists(table)
2.1.x或更高版本.
2.1.x or later.
您可以使用spark.catalog.tableExists
.致谢,请访问 Huseyin Oktay ,以获取
You can use spark.catalog.tableExists
. Credits go to Huseyin Oktay for pointing that out.
这篇关于spark-scala-如何检查配置单元中是否存在表的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文