spark - scala - 如何检查 hive 中是否存在表
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/46477270/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
spark - scala - How can I check if a table exists in hive
提问by Uday Sagar
I have to check whether a table exists in hive using spark(1.6.2) scala
我必须使用 spark(1.6.2) scala 检查 hive 中是否存在表
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)
我需要一个返回布尔值的函数,根据它我可以做出上述决定(是创建新表还是覆盖现有表)
回答by Alper t. Turker
1.x:
1.x:
def tableExists(table: String, sqlContext: SQLContext) =
sqlContext.tableNames.contains(table)
2.x:
2.x:
def tableExists(table: String, spark: SparkSession) =
spark.catalog.tableExists(table)
2.1.x or later.
2.1.x 或更高版本。
You can use spark.catalog.tableExists. Credits go to Huseyin Oktayfor pointing that out.
回答by Fuat A.
We also can define it with a database name as below.
我们也可以使用如下的数据库名称来定义它。
1.6.x
1.6.x
sqlContext.tableNames("db_name").contains("tbl_name")
2.x:
2.x:
spark.catalog.tableExists("db_name", "tbl_name")

