Pregunta

Is there a Hive query to list only the views available in a particular database.

In MySql the query I think is below:

SELECT TABLE_NAME FROM information_schema.TABLES WHERE TABLE_TYPE LIKE 'VIEW' AND TABLE_SCHEMA LIKE 'database_name';

I want something similar for HiveQL.

¿Fue útil?

Solución

There is no INFORMATION_SCHEMA implementation in Hive currently.

There is an Open JIRA which you can view at the following link:

https://issues.apache.org/jira/browse/HIVE-1010

However, if the Hive Metastore is configured using a Derby MySQL server then you can access the information you require.

The different ways of configuring a Hive Metastore can be found at:

http://www.thecloudavenue.com/2013/11/differentWaysOfConfiguringHiveMetastore.html

Here is a detailed E/R Diagram of the Metastore:

https://issues.apache.org/jira/secure/attachment/12471108/HiveMetaStore.pdf

After configuring this Metastore, you can obtain the information you want by a query like:

SELECT * from TBLS where TBLS_TYPE = "VIEW"

Otros consejos

If you are stuck like me with older version of Hive and cannot use SHOW VIEWS then use the following script

export db=$1
tables=`hive -e "use $db; show tables;"`
show_table_command=''
for table in $tables; do
  show_table_command="$show_table_command SHOW CREATE TABLE ${db}.${table};"
done
hive -e "$show_table_command" > show_table
sed_command="s/$db\.//g"
cat show_table | grep "CREATE VIEW" | cut -d" " -f3 | sed 's/`//g' | sed ${sed_command} > all_views

Run this code from as sh find_views.sh dbname. Now the table all_views has the list of views.

You can also use this same technique to find only tables by replacing "CREATE VIEW" to "CREATE TABLE" or "CREATE EXTERNAL TABLE" and adjusting the sed and cut statements accordingly.

SHOW VIEWS [in/from <dbName>] [<pattern>]

(much like SHOW TABLE command) can be tracked through https://issues.apache.org/jira/browse/HIVE-14558 patch is available and there is a chance it will land in Hive 2.0.

Licenciado bajo: CC-BY-SA con atribución
No afiliado a StackOverflow
scroll top