如何获取配置单元表的分区列名

aiazj4mn  于 2021-06-28  发布在  Hive
关注(0)|答案(3)|浏览(350)

我正在开发一个unix脚本,其中我将处理由列a或列b划分的配置单元表。我想找出一个表在哪个列上分区,这样我就可以对这些分区示例执行后续操作。
配置单元中是否有直接返回分区列的属性?
我想我得做一个 show create table 如果没有其他可能的方法,则以某种方式提取分区名称。

cpjpxq1n

cpjpxq1n1#

通过scala/javaapi,我们可以访问hivemetastore并获得分区列名org.apache.hadoop.hive.metastore.hivemetastoreclient

val conf = new Configuration()
conf.set("hive.metastore.uris","thrift://hdppmgt02.domain.com:9083")
val hiveConf = new HiveConf(conf, classOf[HiveConf])
val metastoreClient = new HiveMetaStoreClient(hiveConf)

metastoreClient.getTable(db, tbl).getPartitionKeys.foreach(x=>println("Keys : "+x))
csbfibhn

csbfibhn2#


# use python pyhive:

import hive_client

def get_partition_column(table_name):
    #hc=hive connection
    hc=hive_client.HiveClient()
    cur=hc.query("desc "+table_name)
    return cur[len(cur)-1][0]

################# 

hive_client.py

from pyhive import hive
default_encoding = 'utf-8'
host_name = 'localhost'
port = 10000
database="xxx"

class HiveClient:
    def __init__(self):
        self.conn = hive.Connection(host=host_name,port=port,username='hive',database=database)
    def query(self, sql):
        cursor = self.conn.cursor()
        #with self.conn.cursor() as cursor:
        cursor.execute(sql)
        return cursor.fetchall()

    def execute(self,sql):
        #with self.conn.cursor() as cursor:
        cursor = self.conn.cursor()
        cursor.execute(sql)

    def close(self):`enter code here`
        self.conn.close()
1qczuiv0

1qczuiv03#

可能不是最好的方法,但还有一种方法是使用descripe命令
创建表:

create table employee ( id int, name string ) PARTITIONED BY (city string);

命令:

hive -e 'describe formatted employee'  | awk '/Partition/ {p=1}; p; /Detailed/ {p=0}'

输出:


# Partition Information

# col_name              data_type               comment

city                    string

你可以根据需要改进它。
另一个我不得不探索的选项是通过查询元存储库表来获取表的分区列信息。

相关问题