Hive 表分区

基本知识:

    Hadoop:文件相关操作比如:

     hadoop fs -rmr 'hdfs://hdfs://192.168.8.101:8020/user/hive/warehouse';

     hadoop fs -put '/user/hive/warehouse/data.txt' 'hdfs://hdfs://192.168.8.101:8020/user/hive/warehouse/data.txt'

     创建分区表:

   

   外部表:

create external table if not exists employee(
id int,
name string,
dept string,
yoj int
)
partitioned by (
year string
)
row format delimited fields terminated by '\t'

   

  内部表:

  

create table if not exists employee(
id int,
name string,
dept string,
yoj int
)
partitioned by (
year string
)
row format delimited fields terminated by '\t'

   增加分区并加载数据

    1、 对于外部表

       <1 alter table employee add partition (year=‘2013’) location ‘hdfs://192.168.8.101:8020/user/hive/warehouse/hivedata/data’;      执行添加分区时   hivedata/    文件夹下的数据不会被移动。并且没有分区目录year=2013

      alter table employee drop partition (year=‘2013’);     执行删除分区目录时hivedata/    下的数据不会被删除

  <2  load data inpath 'hdfs://192.168.8.101:8020/user/hive/warehouse/hivedata/data' overwrite into table employee   partition(year='2013');    

    执行加载数据添加分区时 hivedata/     文件夹下的数据会被移动,并创建分区目录logdate=2015-02-26,数据移动到此目录下

        alter table employee drop partition (year='2013');     执行删除分区目录时,已经创建year=2013

分区目录不会被删除,其文件夹下的数据也不会被删除;

    

    2、内部表

         alter table employee add partition (year=‘2013’) location ‘hdfs://192.168.8.101:8020/user/hive/warehouse/hivedata/data’;      执行添加分区时   hivedata/    文件夹下的数据不会被移动。并且没有分区目录year=2013

       alter table employee drop partition (year=‘2013’);     执行删除分区时hivedata/    下的数据会被删除并且连同hivedata/文件夹也会被删除

      

       load data inpath 'hdfs://192.168.8.101:8020/user/hive/warehouse/hivedata/data' overwrite into table empl_inn  partition(year=2013);    
    执行加载数据添加分区时 
hivedata/     文件夹下的数据会被移动,并创建分区目录year=2013,数据移动到此目录下

        alter table employee drop partition (year=‘2013’);     执行删除分区目录时,已经创建year=2013  分区目录会被删除,其文件夹下的数据随之会被删除;

猜你喜欢

转载自1028826685.iteye.com/blog/2330761