【问题标题】:Automated List of Hive External tablesHive 外部表的自动列表
【发布时间】:2018-05-15 02:05:22
【问题描述】:

我必须创建一个自动化流程来列出 Hive 中的所有外部表并对这些表进行记录计数。

我应该将此作为日常工作。我通过对所有外部表名称进行硬编码来尝试此操作,但这不被接受,因为表每月会不断更改一次。

我经历了不同的方法,例如[show tables] 并在元存储数据库中执行查询。但这些不会帮助我自动化这个过程。

有没有更好的方法在 Hive 中实现这一点。

【问题讨论】:

  • 编写一个 shell 脚本并从元存储中获取所有外部表详细信息,并在 hive 中迭代/生成“SELECT COUNT(*)”。

标签: hive hiveql external-tables


【解决方案1】:

类似这样的东西,使用 shell。

#Create external table list for a schema
SCHEMA=your_schema_name 

#define filenames   
alltableslist=tables_$SCHEMA
exttablelist=ext_tables_$SCHEMA

#Get all tables
 hive -S -e " set hive.cli.print.header=false; use $SCHEMA; show tables;" 1> $alltableslist


#For each table check its type:
for table in $(cat $alltableslist)
 do 

 echo Processing table $table ...

     #Describe table
     describe=$(hive client -S -e "use $SCHEMA; DESCRIBE FORMATTED $table")

     #Get type
     table_type=$(echo "${describe}" | egrep -o 'Table Type:[^,]+' | cut -f2)

     #Check table type, get count and write table name with count
      if [ $table_type == EXTERNAL_TABLE ]; then 
         #get count
          cnt=$(hive client -S -e "select count(*) from $SCHEMA.table ")
         #save result
          echo "$table $cnt" > $exttablelist 
      fi

done; #tables loop

只需将开头的 your_schema_name 替换为您的架构名称即可。本例中带有计数的外部表将保存在文件ext_tables_[your_schema_name]

计数可以并行处理,甚至可以在单个 SQL 语句中处理,还有许多其他方面可以改进,但希望你已经掌握了这个想法。

【讨论】:

  • 谢谢。这帮助我获得了一些想法。绝对是更好的解决方案。
【解决方案2】:

根据我的理解,首先您的要求是找出所有 hive 数据库中的 Hive 外部表列表。

hive 外部表的数据库名称、表名、表类型(外部)和 HDFS 位置。

登录到 Hive Metastore 并使用 Hive 元数据库。

使用 3 个表 TBLS、DBS 和 SDS 表,在这 3 个表之上,我们可以在 DB_ID 和 SD_ID 上应用连接

通过使用上述格式,您可以获得数据库名称以及受尊敬的 hive 外部表列表和 HDFS 路径位置。

查询和输出信息请阅读此链接

https://askdoubts.com/question/how-to-find-out-list-of-all-hive-external-tables-and-hdfs-paths-from-hive-metastore/#comment-19

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2013-07-26
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2023-03-19
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多