【发布时间】:2016-08-19 02:25:59
【问题描述】:
我正在开发一个 unix 脚本,我将在其中处理由 A 列或 B 列分区的 Hive 表。我想找到一个表在哪个列上进行分区,以便我可以执行后续操作那些分区实例。
Hive 中有没有直接返回分区列的属性?
我想如果没有其他方法,我将不得不做一个show create table 并以某种方式提取分区名称。
【问题讨论】:
标签: hive
我正在开发一个 unix 脚本,我将在其中处理由 A 列或 B 列分区的 Hive 表。我想找到一个表在哪个列上进行分区,以便我可以执行后续操作那些分区实例。
Hive 中有没有直接返回分区列的属性?
我想如果没有其他方法,我将不得不做一个show create table 并以某种方式提取分区名称。
【问题讨论】:
标签: hive
可能不是最好的,但另一种方法是使用 describe 命令
创建表:
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
您可以根据需要对其进行改进。
我力图探索的另一个选项是通过查询元存储存储库表来获取表的分区列信息。
【讨论】:
通过scala/java api,我们可以进入hive meta store,获取分区列名 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))
【讨论】:
#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()
【讨论】:
List<String> parts = new ArrayList<>();
try {
List<FieldSchema> partitionKeys = client.getTable(dbName, tableName).getPartitionKeys();
for (FieldSchema partition : partitionKeys) {
parts.add(partition.getName());
}
} catch (Exception e) {
throw new RuntimeException("Fail to get Hive partitions", e);
}
【讨论】: