【问题标题】:How to get the partition column name of a Hive table如何获取 Hive 表的分区列名
【发布时间】:2016-08-19 02:25:59
【问题描述】:

我正在开发一个 unix 脚本,我将在其中处理由 A 列或 B 列分区的 Hive 表。我想找到一个表在哪个列上进行分区,以便我可以执行后续操作那些分区实例。

Hive 中有没有直接返回分区列的属性?

我想如果没有其他方法,我将不得不做一个show create table 并以某种方式提取分区名称。

【问题讨论】:

    标签: hive


    【解决方案1】:

    可能不是最好的,但另一种方法是使用 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
    

    您可以根据需要对其进行改进。

    我力图探索的另一个选项是通过查询元存储存储库表来获取表的分区列信息。

    【讨论】:

    • 嘿,你能告诉我们如何用火花(在 sclala 中)做到这一点..???谢谢:-)
    【解决方案2】:

    通过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))
    

    【讨论】:

      【解决方案3】:
      #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()
      

      【讨论】:

      • 您能否解释一下您的答案为何有效/如何运作? stackoverflow.com/help/how-to-answer
      • 更多关于此的信息将帮助那些正在寻找答案的人。例如为什么使用 pyhive 以及在这段代码中你在做什么
      • 致乔希,补充
      【解决方案4】:
       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);
              }
      

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 2022-11-13
        • 2016-04-03
        • 2019-07-29
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2014-09-06
        • 1970-01-01
        相关资源
        最近更新 更多