我们如何知道表是托管表还是外部表?

时间:2017-03-19 04:50:56

标签: hive hiveql

在hive中,当我运行show tables;时,我得到了所有表的列表,我如何知道哪些是托管表,哪些是外部表?

2 个答案:

答案 0 :(得分:1)

对于单个表格,您可以使用desc formatteddesc extended

演示

create table mytable (i int);
create external table myexttable (i int);

desc格式化

hive> desc formatted mytable;
...
Location:               hdfs://horton/user/dmarkovitz/mytable
Table Type:             MANAGED_TABLE
Table Parameters:
...
hive> desc formatted myexttable;

...
Location:               hdfs://horton/user/dmarkovitz/myexttable
Table Type:             EXTERNAL_TABLE
Table Parameters:
...

desc extended

hive> desc extended mytable;
...
Detailed Table Information      Table(tableName:mytable ... tableType:MANAGED_TABLE)
hive> desc extended myexttable;
...
Detailed Table Information      Table(tableName:myexttable ... tableType:EXTERNAL_TABLE)

对于许多/所有表,您可以查询Metastore(Hive元数据数据库)。

metastore

(在本演示中,Metastore是在mysql中)

select  d.name          as database_name
       ,t.tbl_name
       ,t.tbl_type

from            metastore.DBS   as d

        join    metastore.TBLS  as t  

        on      t.DB_ID =
                d.DB_ID

where   d.name = 'dmarkovitz'
;
+---------------+------------+----------------+
| database_name |  tbl_name  |    tbl_type    |
+---------------+------------+----------------+
| dmarkovitz    | mytable    | MANAGED_TABLE  |
| dmarkovitz    | myexttable | EXTERNAL_TABLE |
+---------------+------------+----------------+

仅供参考,EXTERNAL / MANAGED属性只是一个标志(元数据级别),可以使用ALTER TABLE命令进行更改

<强>蜂房

alter table mytable set tblproperties ("EXTERNAL"="TRUE");
alter table myexttable set tblproperties ("EXTERNAL"="FALSE");

<强> metastore

select  d.name          as database_name
       ,t.tbl_name
       ,t.tbl_type

from            metastore.DBS   as d

        join    metastore.TBLS  as t  

        on      t.DB_ID =
                d.DB_ID

where   d.name = 'dmarkovitz'
;
+---------------+------------+----------------+
| database_name |  tbl_name  |    tbl_type    |
+---------------+------------+----------------+
| dmarkovitz    | mytable    | EXTERNAL_TABLE |
| dmarkovitz    | myexttable | MANAGED_TABLE  |
+---------------+------------+----------------+

答案 1 :(得分:0)

您可以使用以下命令获取指定表的详细信息:

describe formatted <your_table>;

输出将包含一行描述table type

  

表类型:EXTERNAL_TABLE

  

表类型:MANAGED_TABLE