【问题标题】:Hive: how to show all partitions of a table?Hive:如何显示表的所有分区?
【发布时间】:2013-03-15 00:31:27
【问题描述】:

我有一个包含 1000 多个分区的表。

Show partitions”命令只列出少量分区。

如何显示所有分区?

更新:

  1. 我发现“show partitions”命令只列出了 500 个分区。

  2. select ... where ...”只处理500个分区!

【问题讨论】:

    标签: hadoop hive


    【解决方案1】:

    另一个选择是通过 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]} for part in all_partitions])
    [{'dt': '20210504'}, {'dt': '20210505'}]
    

    注意:max_parts 是一个不能大于 32767(java 短最大值)的参数。

    如果您安装了 Airflow 和额外的 apache.hive,您可以很容易地创建 hmsclient

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

    这似乎是与 Hive Metastore 进行通信的一种比直接访问 DB(以及与数据库引擎无关的 BTW)更有效的通信方式。

    【讨论】:

      【解决方案2】:

      hive> 显示分区表名;

      【讨论】:

        【解决方案3】:

        好的,我通过在上面扩展 wmky 的答案来写这个答案,并且假设您已经为 Metastore 而不是 derby 配置了 mysql。

        select PART_NAME FROM PARTITIONS WHERE TBL_ID=(SELECT TBL_ID FROM TBLS WHERE TBL_NAME='<table_name>');
        

        上述查询为您提供了分区列的所有可能值。

        示例:

        hive> desc clicks_fact;
        OK
        time                    timestamp                                   
        ..                              
        day                     date                                        
        file_date               varchar(8)                                  
        
        # Partition Information      
        # col_name              data_type               comment             
        
        day                     date                                        
        file_date               varchar(8)                                  
        Time taken: 1.075 seconds, Fetched: 28 row(s)
        

        我要获取分区列的值。

        mysql> select PART_NAME FROM PARTITIONS WHERE TBL_ID=(SELECT TBL_ID FROM TBLS WHERE TBL_NAME='clicks_fact');
        +-----------------------------------+
        | PART_NAME                         |
        +-----------------------------------+
        | day=2016-08-16/file_date=20160816 |
        | day=2016-08-17/file_date=20160816 |
        ....
        ....
        | day=2017-09-09/file_date=20170909 |
        | day=2017-09-08/file_date=20170909 |
        | day=2017-09-09/file_date=20170910 |
        | day=2017-09-10/file_date=20170910 |
        +-----------------------------------+
        
        1216 rows in set (0.00 sec)
        

        返回所有分区列。

        注意:JOIN table DBS ON DB_ID 当涉及 DB 时(即,当多个 DB 具有相同的 table_name 时)

        【讨论】:

          【解决方案4】:

          您可以在“PARTITIONS”表中查看 Hive MetaStore 表、分区信息。 您可以使用“TBLS”连接“分区”来查询特殊的表分区。

          【讨论】:

          • 进入 hive mysql 是个坏主意
          【解决方案5】:

          CLI 在显示输出时有一些限制。我建议将输出导出到本地文件:

          $hive -e 'show partitions table;' > partitions
          

          【讨论】:

          • 与 CLI 的结果相同。它只显示 500 个分区。我不知道神奇的数字 500 是从哪里来的。
          • 那就不知道了。这很奇怪。如果您解决它或找到它的来源,请随时通知我们。 GL!也许天真,但你确定有超过 500 个分区吗?
          • 问题由“set cassandra.connection.sliceSize=10000;”解决。也许这是 Hive 的 datastax 的扩展。
          • @colintobing 从 metastore_db 查询时可行
          • 为什么这是投票最多的答案,正如 cmets 显示的那样,它仍然只显示 500 个分区?
          猜你喜欢
          • 2019-11-18
          • 1970-01-01
          • 1970-01-01
          • 2017-03-09
          • 2020-07-09
          • 1970-01-01
          • 2019-01-06
          • 1970-01-01
          • 2018-12-28
          相关资源
          最近更新 更多