【问题标题】:How to get all table definitions in a database in Hive?如何在 Hive 的数据库中获取所有表定义?
【发布时间】:2016-05-02 11:05:28
【问题描述】:

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

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

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

【问题讨论】:

标签: hadoop hive


【解决方案1】:

您可以通过编写一个简单的 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 文件将包含所有表定义。

【讨论】:

  • 这是一个很好的解决方案,但速度很慢。如果数据库有 100 个表,每次 bash 脚本执行“hive -e ....”超过 100 次,执行将花费大量时间才能完成。需要在“hive -e ...”中进行一些循环......类似于“hive -e for loop desc &lt;table&gt;”。
  • 嗨 Apurvaa、@mostafazh 或其他...关于 diretc SQL 在 CSV 文件中显示例如 3 列 &lt;database_name, table_name, column_name&gt;? Hive 中有类似this SELECT FROM INFORMATION_SCHEMA的东西?
【解决方案2】:

上述过程有效,但是由于每个查询都建立了配置单元连接,因此速度会很慢。相反,您可以按照我刚刚为下面的相同需求做的事情。

使用上述方法之一来获取您的表格列表。 然后修改列表,使其成为每个表的配置单元查询,如下所示:

describe my_table_01;
describe my_TABLE_02;

因此,您将拥有一个包含上述所有描述语句的平面文件。例如,如果您在名为 my_table_description.hql 的平面文件中有查询。

一勺得到输出,如下所示:

"hive -f my_table_description.hql > my_table_description.output

速度超快,一键输出。

【讨论】:

    【解决方案3】:
    1. 获取hive数据库列表hive -e 'show databases' &gt; hive_databases.txt

    2. 回显每个表的描述:

        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
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2011-02-16
      • 2011-09-07
      • 2011-11-17
      • 2021-12-31
      • 2021-12-05
      相关资源
      最近更新 更多