查询HIVE元数据

时间:2018-06-28 20:39:35

标签: hadoop hive hiveql hadoop2 hortonworks-data-platform

我需要查询下表并查看我的Apache HIVE集群中的信息:

每行需要包含以下内容:

表模式

表名

表说明

列名

列数据类型

列长

列精度

列标度

NULL或NOT NULL

主键指示器

从大多数RDBMS(元数据表/视图)中可以很容易地查询到它,但是我正在努力寻找有关HIVE中等效的元数据表/视图的很多信息。

请帮助:)

1 个答案:

答案 0 :(得分:2)

此信息可从Hive Metastore获得。下面的示例查询是针对MySQL支持的元存储库(Hive版本1.2)。

SELECT 
DBS.NAME AS TABLE_SCHEMA,
TBLS.TBL_NAME AS TABLE_NAME,
TBL_COMMENTS.TBL_COMMENT AS TABLE_DESCRIPTION,
COLUMNS_V2.COLUMN_NAME AS COLUMN_NAME,
COLUMNS_V2.TYPE_NAME AS COLUMN_DATA_TYPE_DETAILS
FROM DBS
JOIN TBLS ON DBS.DB_ID = TBLS.DB_ID
JOIN SDS ON TBLS.SD_ID = SDS.SD_ID
JOIN COLUMNS_V2 ON COLUMNS_V2.CD_ID = SDS.CD_ID
JOIN 
    (
        SELECT DISTINCT TBL_ID, TBL_COMMENT 
        FROM 
        (
            SELECT TBLS.TBL_ID TBL_ID, TABLE_PARAMS.PARAM_KEY, TABLE_PARAMS.PARAM_VALUE, CASE WHEN TABLE_PARAMS.PARAM_KEY = 'comment' THEN TABLE_PARAMS.PARAM_VALUE ELSE '' END TBL_COMMENT
            FROM TBLS JOIN TABLE_PARAMS
            ON TBLS.TBL_ID = TABLE_PARAMS.TBL_ID
        ) TBL_COMMENTS_INTERNAL
    ) TBL_COMMENTS 
ON TBLS.TBL_ID = TBL_COMMENTS.TBL_ID;

示例输出:

+--------------+----------------------+-----------------------+-------------------+------------------------------+
| TABLE_SCHEMA | TABLE_NAME           | TABLE_DESCRIPTION     | COLUMN_NAME       | COLUMN_DATA_TYPE_DETAILS     |
+--------------+----------------------+-----------------------+-------------------+------------------------------+
| default      | temp003              | This is temp003 table | col1              | string                       |
| default      | temp003              | This is temp003 table | col2              | array<string>                |
| default      | temp003              | This is temp003 table | col3              | array<string>                |
| default      | temp003              | This is temp003 table | col4              | int                          |
| default      | temp003              | This is temp003 table | col5              | decimal(10,2)                |
| default      | temp004              |                       | col11             | string                       |
| default      | temp004              |                       | col21             | array<string>                |
| default      | temp004              |                       | col31             | array<string>                |
| default      | temp004              |                       | col41             | int                          |
| default      | temp004              |                       | col51             | decimal(10,2)                |
+--------------+----------------------+-----------------------+-------------------+------------------------------+

查询中引用的元存储表:

DBS: Details of databases/schemas.
TBLS: Details of tables.
COLUMNS_V2: Details about columns.
SDS: Details about storage.
TABLE_PARAMS: Details about table parameters (key-value pairs)