How to get all table definitions in a database in Hive?

Deadly 提交于 2020-01-03 16:45:46

问题


I am looking to get all table definitions in Hive. I know that for single table definition I can use something like -

  describe <<table_name>>
  describe extended <<table_name>>

But, I couldn't find a way to get all table definitions. Is there any table in megastore similar to Information_Schema in mysql or is there command to get all table definitions ?


回答1:


You can do this by writing a simple bash script and some bash commands.

First, write all table names in a database to a text file using:

$hive -e 'show tables in <dbname>' | tee tables.txt

Then create a bash script (describe_tables.sh) to loop over each table in this list:

while read line
do
 echo "$line"
 eval "hive -e 'describe <dbname>.$line'"
done

Then execute the script:

$chmod +x describe_tables.sh
$./describe_tables.sh < tables.txt > definitions.txt

The definitions.txt file will contain all the table definitions.




回答2:


  1. Fetch hive databases list hive -e 'show databasess' > hive_databases.txt

  2. Echo each tables desc:

    cat hive_databases.txt | grep -v '^$' | while read LINE;
    do
      echo "## TableName:" $LINE
      eval "hive -e 'show tables in $LINE' | grep -v ^$ | grep -v Logging | grep -v tab_name | tee $LINE.tables.txt"
      cat $LINE.tables.txt | while read table
      do
        echo "### $LINE.$table" > $LINE.$table.desc.md
        eval "hive -e 'describe $LINE.$table'" >> $LINE.$table.desc.md
        sed -i 's/\t/|/g' ./$LINE.$table.desc.md
        sed -i 's/comment/comment\n|:--:|:--:|:--:|/g' ./$LINE.$table.desc.md
      done
    done


来源:https://stackoverflow.com/questions/35004455/how-to-get-all-table-definitions-in-a-database-in-hive

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!