【发布时间】:2016-05-02 11:05:28
【问题描述】:
我希望在 Hive 中获取所有表定义。我知道对于单表定义,我可以使用类似 -
describe <<table_name>>
describe extended <<table_name>>
但是,我找不到获取所有表定义的方法。 megastore 中是否有类似于 mysql 中 Information_Schema 的表,或者是否有命令获取所有表定义?
【问题讨论】:
我希望在 Hive 中获取所有表定义。我知道对于单表定义,我可以使用类似 -
describe <<table_name>>
describe extended <<table_name>>
但是,我找不到获取所有表定义的方法。 megastore 中是否有类似于 mysql 中 Information_Schema 的表,或者是否有命令获取所有表定义?
【问题讨论】:
您可以通过编写一个简单的 bash 脚本和一些 bash 命令来做到这一点。
首先,将数据库中的所有表名写入文本文件:
$hive -e 'show tables in <dbname>' | tee tables.txt
然后创建一个 bash 脚本 (describe_tables.sh) 来循环遍历此列表中的每个表:
while read line
do
echo "$line"
eval "hive -e 'describe <dbname>.$line'"
done
然后执行脚本:
$chmod +x describe_tables.sh
$./describe_tables.sh < tables.txt > definitions.txt
definitions.txt 文件将包含所有表定义。
【讨论】:
for loop desc <table>”。
<database_name, table_name, column_name>? Hive 中有类似this SELECT FROM INFORMATION_SCHEMA的东西?
上述过程有效,但是由于每个查询都建立了配置单元连接,因此速度会很慢。相反,您可以按照我刚刚为下面的相同需求做的事情。
使用上述方法之一来获取您的表格列表。 然后修改列表,使其成为每个表的配置单元查询,如下所示:
describe my_table_01;
describe my_TABLE_02;
因此,您将拥有一个包含上述所有描述语句的平面文件。例如,如果您在名为 my_table_description.hql 的平面文件中有查询。
一勺得到输出,如下所示:
"hive -f my_table_description.hql > my_table_description.output
速度超快,一键输出。
【讨论】:
获取hive数据库列表hive -e 'show databases' > hive_databases.txt
回显每个表的描述:
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
【讨论】: