如何在Hive中的数据库中获取所有表定义?

Gol*_*num 4 hadoop hive bigdata

我希望在Hive中获取所有表定义.我知道对于单表定义我可以使用类似的东西 -

  describe <<table_name>>
  describe extended <<table_name>>
Run Code Online (Sandbox Code Playgroud)

但是,我找不到获取所有表定义的方法.在megastore中是否有类似于mysql中的Information_Schema的表,或者是否有命令来获取所有表定义?

小智 13

您可以通过编写简单的bash脚本和一些bash命令来完成此操作.

首先,使用以下方法将数据库中的所有表名写入文本文件:

$hive -e 'show tables in <dbname>' | tee tables.txt
Run Code Online (Sandbox Code Playgroud)

然后创建一个bash脚本(describe_tables.sh)来循环遍历此列表中的每个表:

while read line
do
 echo "$line"
 eval "hive -e 'describe <dbname>.$line'"
done
Run Code Online (Sandbox Code Playgroud)

然后执行脚本:

$chmod +x describe_tables.sh
$./describe_tables.sh < tables.txt > definitions.txt
Run Code Online (Sandbox Code Playgroud)

definitions.txt文件将包含所有表定义.

  • 这是一个很好的解决方案,但速度很慢。如果数据库有100个表,bash脚本每次执行“hive -e ....”100多次,执行将需要很长时间才能完成。需要在“hive -e ...”中进行一些循环......类似于“hive -e `for loop desc &lt;table&gt;`”。 (2认同)