配置单元外部表的自动列表

dsf9zpds  于 2021-06-26  发布在  Hive
关注(0)|答案(2)|浏览(344)

我必须创建一个自动化的进程来列出配置单元中的所有外部表,并对这些表进行记录计数。
我应该把这当作日常工作来做。我试图通过硬编码所有外部表名来实现这一点,但这是不被接受的,因为这些表每月都会更改一次。
我经历过不同的方法,比如 [show tables] 并在metastore db中执行查询。但这些并不能帮助我自动化这个过程。
有没有更好的方法在hive中实现这一点。

cwdobuhd

cwdobuhd1#

根据我的理解,首先您的要求是找出所有配置单元数据库中配置单元外部表的列表。
配置单元外部表的数据库名称、表名称、表类型(外部)和hdfs位置。
登录到配置单元元存储并使用配置单元元数据库。
使用3个表tbls、dbs和sds表,在这3个表之上,我们可以对db\u id和sd\u id应用连接
通过使用上述格式,您还可以获得数据库名称、受尊重的配置单元外部表列表和hdfs路径位置。
有关查询和输出信息,请阅读此链接
https://askdoubts.com/question/how-to-find-out-list-of-all-hive-external-tables-and-hdfs-paths-from-hive-metastore/#comment-19

flvtvl50

flvtvl502#

像这样的,使用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语句中处理,还有许多其他的事情可以改进,但是希望您已经明白了这个想法。

相关问题