SQL 如何检查 Hive 中是否存在表?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/21194677/
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

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-01 00:47:21  来源:igfitidea点击:

How to check if a table exists in Hive?

sqlodbchive

提问by Michael Robinson

I am connecting to Hive via an ODBC driver from a .NET application. Is there a query to determine if a table already exists?

我正在通过 .NET 应用程序的 ODBC 驱动程序连接到 Hive。是否有查询来确定表是否已存在?

For example, in MSSQL you can query the INFORMATION_SCHEMAtable and in Netezza you can query the _v_tabletable.

例如,在 MSSQL 中您可以查询INFORMATION_SCHEMA表,而在 Netezza 中您可以查询_v_table表。

Any assistance would be appreciated.

任何援助将不胜感激。

采纳答案by Mukesh S

There are two approaches by which you can check that:

有两种方法可以检查:

1.) As @dimamah suggested, just to add one point here, for this approach you need to

1.) 正如@dimamah 所建议的,只是在这里添加一点,对于这种方法,您需要

 1.1) start the **hiveserver** before running the query
 1.2) you have to run two queries
      1.2.1) USE <database_name>
      1.2.2) SHOW TABLES LIKE 'table_name'
      1.2.3) Then you check your result using Result set.

2.) Second approach is to use HiveMetastoreClient APIs, where you can directly use the APIs to check whether the table_nameexist in a particular databaseor not.

2.) 第二种方法是使用 HiveMetastoreClient API,您可以直接使用 API 来检查table_name是否存在于特定数据库中

For further help please go through this Hive 11

如需进一步帮助,请阅读此Hive 11

回答by dimamah

Execute the following command : show tables in DB like 'TABLENAME'
If the table exists, its name will be returned, otherwise nothing will be returned.
This is done directly from hive. for more options see this.

执行以下命令:show tables in DB like 'TABLENAME'
如果表存在,则返回其名称,否则不返回任何内容。
这是直接从 hive 完成的。有关更多选项,请参阅

DBis the database in which you want to see if the table exists.
TABLENAMEis the table name you seek,

DB是您要查看表是否存在的数据库。
TABLENAME是您要查找的表名,

What actually happens is that Hive queries its metastore(depends on your configuration but it can be in a standard RDBMS like MySQL) so you can optionally connect directly to the same metastore and write your own query to see if the table exists.

实际发生的是 Hive 查询其元存储(取决于您的配置,但它可以在像 MySQL 这样的标准 RDBMS 中),因此您可以选择直接连接到同一个元存储并编写自己的查询以查看该表是否存在。

回答by Haimei

When programming on Hive by Spark SQL, you can use following method to check whether Hive table exists.

使用 Spark SQL 在 Hive 上编程时,可以使用以下方法检查 Hive 表是否存在。

if (hiveContext.hql("SHOW TABLES LIKE '" + tableName + "'").count() == 1) {
    println(tableName + " exists")
}

回答by Alex Raj Kaliamoorthy

If someone is using shell script like me then my answer could be useful. Assume that your table is in the default namespace.

如果有人像我一样使用 shell 脚本,那么我的回答可能会有用。假设您的表位于默认命名空间中。

table=your_hive_table
validateTable=$(hive --database default -e "SHOW TABLES LIKE '$table'")
if [[ -z $validateTable ]]; then
  echo "Error:: $table cannot be found"
  exit 1
fi

回答by Nicolas Castro

If you're using SparkSQL you can do the following.

如果您使用的是 SparkSQL,您可以执行以下操作。

if "table_name" in sqlContext.tableNames("db_name"):
    ...do something

http://spark.apache.org/docs/2.1.0/api/python/pyspark.sql.html#pyspark.sql.SQLContext.tableNames

http://spark.apache.org/docs/2.1.0/api/python/pyspark.sql.html#pyspark.sql.SQLContext.tableNames

回答by Tagar

Code similar to below one can find in many of my Spark notebooks:

在我的许多 Spark 笔记本中都可以找到类似于下面的代码:

stg_table_exists = sqlCtx.sql("SHOW TABLES IN "+ stg_db) 
                  .filter("tableName='%s'" % stg_tab_name) .collect()

(made two-liner for readability)

(为了可读性做了两行)

I wish Spark would have an API call to check the same.

我希望 Spark 有一个 API 调用来检查它。

回答by Tim

If you're using a scala spark app and SparkSQL you can do the following

如果您使用的是 scala spark 应用程序和 SparkSQL,则可以执行以下操作

if spark.catalog.tableExists("tablename") {do something}