欢迎来到尧图网

客户服务 关于我们

您的位置:首页 > 文旅 > 手游 > Shell 获取Hive表的location 信息

Shell 获取Hive表的location 信息

2025/9/17 6:42:48 来源:https://blog.csdn.net/kzw11/article/details/139984294  浏览:    关键词:Shell 获取Hive表的location 信息

用shell 获取建表语句:

hive -e "show create table ods_job.ods_job_tb"

得到结果:

CREATE TABLE `ods_job.ods_job_tb`(`id` bigint COMMENT 'id', `auto` int COMMENT 'job开启/关闭:0-关闭;1-开启', ....`timeout_kill` string COMMENT '是否超时kill')
COMMENT 'job表'
PARTITIONED BY ( `d` string COMMENT '日期分区字段')
ROW FORMAT SERDE 'org.apache.hadoop.hive.serde2.columnar.ColumnarSerDe' 
STORED AS INPUTFORMAT 'org.apache.hadoop.hive.ql.io.RCFileInputFormat' 
OUTPUTFORMAT 'org.apache.hadoop.hive.ql.io.RCFileOutputFormat'
LOCATION'hdfs://ns/user/hive/warehouse/job.db/job_tb'
TBLPROPERTIES ('last_modified_by'='user', 'last_modified_time'='1656303639', 'metadata.partition.life'='-1', 'metadata.security.level'='Medium', 'orc.bloom.filter.columns'='id,visitor_id,auto,owner,group_id', 'spark.sql.partitionProvider'='catalog', 'transient_lastDdlTime'='1706259083')
Time taken: 1.343 seconds, Fetched: 57 row(s)

想要获取LOCATION 引号里面的值,并实现自动化

tardb=$1
tarTblname=$2
stmt=`hive-sql -v -e "use ${tardb}; show create table ${tarTblname};"`
loc=$( expr "${stmt}" : ".*LOCATION...'\([^']*\)" );echo $loc

loc变量就是想要的结果,这个shell稍微难写一点的就是里面的正则,还得过滤掉引号。
方式2, 这种会有点瑕疵,一旦表中有location 字段就有可能会产生bug,而上面就不会出现这种问题

#!/bin/bash# 替换为你的 Hive 表名
table_name="db.table"# 使用 Hive 命令获取表的详细信息,并通过 grep 筛选出包含 "Location" 的行
location_info=$(hive -e "describe formatted $table_name" | grep "Location")# 提取出具体的 location 值
location=$(echo $location_info | awk '{print $2}')echo "表 $table_name 的 location 信息为: $location"

推荐还是使用方法1
小记~

版权声明:

本网仅为发布的内容提供存储空间,不对发表、转载的内容提供任何形式的保证。凡本网注明“来源:XXX网络”的作品,均转载自其它媒体,著作权归作者所有,商业转载请联系作者获得授权,非商业转载请注明出处。

我们尊重并感谢每一位作者,均已注明文章来源和作者。如因作品内容、版权或其它问题,请及时与我们联系,联系邮箱:809451989@qq.com,投稿邮箱:809451989@qq.com

热搜词