【问题标题】:How to list HDFS location of all the partitions in a hive table?如何列出配置单元表中所有分区的 HDFS 位置?
【发布时间】:2021-05-17 21:55:03
【问题描述】:

使用命令:

describe formatted my_table partition my_partition

我们能够在my_table 中列出包括分区my_partition 的hdfs 位置的元数据。但是我们如何才能得到 2 列的输出:

Partition | Location

哪个会列出my_table 中的所有分区及其hdfs 位置?

【问题讨论】:

  • 你不能。显示 metadata 的命令(如 showdescribe)不应用作 data 的源,然后可以使用 SQL 对其进行操作。
  • 但是您可以使用 Java API 访问 Hive Metastore,参见。 stackoverflow.com/questions/33880050/… >> 每个Partition 都有自己的StorageDescriptor;注意没有Table方法可以访问自己的Partition描述符,你必须回到HiveMetastoreClient

标签: hive


【解决方案1】:

查询元存储。

演示

蜂巢

create table mytable (i int) partitioned by (dt date,type varchar(10))
;

alter table mytable add 
    partition (dt=date '2017-06-10',type='A')
    partition (dt=date '2017-06-11',type='A')
    partition (dt=date '2017-06-12',type='A')
    partition (dt=date '2017-06-10',type='B')
    partition (dt=date '2017-06-11',type='B')
    partition (dt=date '2017-06-12',type='B')
;
        

元存储(MySQL)

select  p.part_name
       ,s.location

from            metastore.DBS           as d
        
        join    metastore.TBLS          as t
        
        on      t.db_id         =
                d.db_id
                
        join    metastore.PARTITIONS    as p
        
        on      p.tbl_id        =
                t.tbl_id
                
        join    metastore.SDS           as s
        
        on      s.sd_id         =
                p.sd_id
                
where   d.name     = 'default'
    and t.tbl_name = 'mytable'
;

+----------------------+----------------------------------------------------------------------------------+
|      part_name       |                                     location                                     |
+----------------------+----------------------------------------------------------------------------------+
| dt=2017-06-10/type=A | hdfs://quickstart.cloudera:8020/user/hive/warehouse/mytable/dt=2017-06-10/type=A |
| dt=2017-06-11/type=A | hdfs://quickstart.cloudera:8020/user/hive/warehouse/mytable/dt=2017-06-11/type=A |
| dt=2017-06-12/type=A | hdfs://quickstart.cloudera:8020/user/hive/warehouse/mytable/dt=2017-06-12/type=A |
| dt=2017-06-10/type=B | hdfs://quickstart.cloudera:8020/user/hive/warehouse/mytable/dt=2017-06-10/type=B |
| dt=2017-06-11/type=B | hdfs://quickstart.cloudera:8020/user/hive/warehouse/mytable/dt=2017-06-11/type=B |
| dt=2017-06-12/type=B | hdfs://quickstart.cloudera:8020/user/hive/warehouse/mytable/dt=2017-06-12/type=B |
+----------------------+----------------------------------------------------------------------------------+

【讨论】:

  • 这是一个非常顽皮的把戏。顺便问一下,最近有没有关于 Metastore 数据模型的文档——无论是官方的还是非官方的?因为有时您不仅想从中获取数据;有时您必须更新该死的东西,因为您的 Prod 版本升级只是破坏了您的 Prod 系统……我去过那里。
【解决方案2】:

如果不需要以漂亮的表格格式获取信息 - 并且您无权访问 HMS 数据库,则可能需要运行 explain extended

explain extended select * from default.mytable;

然后您可以找到基本信息,partition valueslocation


root@ubuntu:/home/sathya# hive -e "explain extended select * from default.mytable;" | grep location
OK
location hdfs://localhost:9000/user/hive/warehouse/mytable/dt=2017-06-10/type=A
location hdfs://localhost:9000/user/hive/warehouse/mytable
location hdfs://localhost:9000/user/hive/warehouse/mytable/dt=2017-06-10/type=B
location hdfs://localhost:9000/user/hive/warehouse/mytable
location hdfs://localhost:9000/user/hive/warehouse/mytable/dt=2017-06-11/type=A
location hdfs://localhost:9000/user/hive/warehouse/mytable
location hdfs://localhost:9000/user/hive/warehouse/mytable/dt=2017-06-11/type=B
location hdfs://localhost:9000/user/hive/warehouse/mytable
location hdfs://localhost:9000/user/hive/warehouse/mytable/dt=2017-06-12/type=A
location hdfs://localhost:9000/user/hive/warehouse/mytable
location hdfs://localhost:9000/user/hive/warehouse/mytable/dt=2017-06-12/type=B
location hdfs://localhost:9000/user/hive/warehouse/mytable

【讨论】:

    【解决方案3】:

    在我看来,最好的解决方案是通过 Thrift 协议从 Hive Metastore 获取此信息。
    如果你用python写代码,你可以使用hmsclient库:

    Hive cli:

    hive> create table test_table_with_partitions(f1 string, f2 int) partitioned by (dt string);
    OK
    Time taken: 0.127 seconds
    
    hive> alter table test_table_with_partitions add partition(dt=20210504) partition(dt=20210505);
    OK
    Time taken: 0.152 seconds
    

    Python 命令行:

    >>> from hmsclient import hmsclient
    >>> client = hmsclient.HMSClient(host='hive.metastore.location', port=9083)
    >>> with client as c:
    ...    all_partitions = c.get_partitions(db_name='default',
    ...                                      tbl_name='test_table_with_partitions', 
    ...                                      max_parts=24 * 365 * 3)
    ...
    >>> print([{'dt': part.values[0], 'location': part.sd.location} for part in all_partitions])
    [{'dt': '20210504', 
      'location': 'hdfs://hdfs.master.host:8020/user/hive/warehouse/test_table_with_partitions/dt=20210504'}, 
    {'dt': '20210505', 
     'location': 'hdfs://hdfs.master.host:8020/user/hive/warehouse/test_table_with_partitions/dt=20210505'}]
    

    如果你安装了 Airflow 和 apache.hive 额外的,你创建 hmsclient 使用来自 Airflow Connections 的数据非常容易:

    hive_hook = HiveMetastoreHook()
    with hive_hook.metastore as hive_client:
        ... your code goes here ...
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2017-03-27
      • 1970-01-01
      • 2018-01-10
      • 2023-01-30
      • 1970-01-01
      相关资源
      最近更新 更多