我通过.NET应用程序中的ODBC驱动程序连接到Hive.是否有查询来确定表是否已存在?
例如,在MSSQL中,您可以查询INFORMATION_SCHEMA表,在Netezza中,您可以查询_v_table表.
任何援助将不胜感激.
Hai*_*mei 12
通过Spark SQL在Hive上编程时,可以使用以下方法检查Hive表是否存在.
if (hiveContext.hql("SHOW TABLES LIKE '" + tableName + "'").count() == 1) {
println(tableName + " exists")
}
Run Code Online (Sandbox Code Playgroud)
Muk*_*h S 10
有两种方法可以检查:
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.
Run Code Online (Sandbox Code Playgroud)
2.)第二种方法是使用HiveMetastoreClient API,您可以直接使用API来检查table_name是否存在于特定数据库中.
如需进一步帮助,请浏览此Hive 11
如果有人像我一样使用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
Run Code Online (Sandbox Code Playgroud)
小智 6
如果您使用的是 SparkSQL,您可以执行以下操作。
if "table_name" in sqlContext.tableNames("db_name"):
...do something
Run Code Online (Sandbox Code Playgroud)
http://spark.apache.org/docs/2.1.0/api/python/pyspark.sql.html#pyspark.sql.SQLContext.tableNames