HIVE外部表 分区表

HIVE外部表 分区表
    外部表
        创建hive表,经过检查发现TBLS表中,hive表的类型为MANAGED_TABLE.

        在真实开发中,很可能在hdfs中已经有了数据,希望通过hive直接使用这些数据作为表内容。
        此时可以直接创建出hdfs文件夹,其中放置数据,再在hive中创建表管来管理,这种方式创建出来的表叫做外部表。

        #创建目录,上传已有文件
        hadoop fs -mkdir /data
        hadoop fs -put student.txt /datax/a.txt    
        hadoop fs -put student.txt /datax/b.txt    
        #在hive中创建外部表管理已有数据
        create external table ext_student(id int ,name string) row format delimited fields terminated by ' ' location '/datax';
        经过检查发现可以使用其中的数据。成功的建立了一张外部表。

        #vim ppp.txt
            1    x
            2    y
            3    z
        #hadoop fs - put  peo.avi
        再在该目录下手动创建文件,能不能查询出来呢?
        发现是可以的。

        不管是内部表还是外部表,新增的文件都可以自动被应用。

        在删除表时,内部表一旦删除对应元数据和hdfs中的文件夹和文件都会被删除。外部表只删除元数据,对应的hdfs中的文件夹和文件不会被删除。
    分区表
        hive也支持分区表
        对数据进行分区可以提高查询时的效率
        普通表和分区表区别:有大量数据增加的需要建分区表
        create table book (id bigint, name string) partitioned by (category string) row format delimited fields terminated by ' ';
        在创建分区表时,partitioned字段可以不在字段列表中。生成的文件自动就会具有该字段。
        
        分区表加载数据
        load data local inpath './book_china.txt' overwrite into table book partition (category='china');
        load data local inpath './book_us.txt' overwrite into table book partition (pubdate='2015-01-11');
        
        select * from book;
        select * from book where pubdate='2010-08-22';
        经检查发现分区也是一个目录。
        此时手动创建目录是无法被hive使用的,因为元数据库中没有记录该分区。
        如果需要将自己创建的分区也能被识别,需要执行:
            ALTER TABLE book add  PARTITION (category = 'zazhi') location '/user/hive/warehouse/datax.db/book/category=zazhi';

原文地址:https://www.cnblogs.com/zpb2016/p/5791623.html