hive 表类型

Hive表有受控表(内部表)、外部表、分区表、桶表四种。

 
内部表,就是一般的表,前面讲到的表都是内布标,当表定义被删除的时候,表中的数据随之一并被删除。
 
外部表,数据存在与否和表的定义互不约束,仅仅只是表对hdfs上相应文件的一个引用,当删除表定义的时候,表中的数据依然存在。
 
创建外部表,external是外部表的关键字,也是和内部表有区别的地方
create external table tblName(colName colType...);
加载数据
alter table tblName set location 'hdfs_absolute_uri';
 
外部表还可以在创建表的时候指定数据的位置,引用当前位置的数据。
create external table tblName(colName colType...) location 'hdfs_absolute_uri';
 
内部表和外部表的转换:
内——>外
alter table tblName set tblproperties('EXTERNAL'='TRUE');
外——>内
alter table tblName set tblproperties('EXTERNAL'='FALSE');
 
分区表
如何创建一张分区表?只需要在之前的创建表后面使用partition by加上分区字段就可以了,eg.
  create table tblName (
   id int comment 'ID',
   name string comment 'name' 
  ) partitioned by (dt date comment 'create time')
  row format delimited
  fields terminated by ' ';
 
如何加载数据?
load data local inpath linux_fs_path into table tblName partition(dt='2015-12-12');
 
分区的一些操作:
查询分区中的数据:select * from tblName where dt='2015-12-13';(分区相当于where的一个条件)
手动创建一个分区:alter table tblName add partition(dt='2015-12-13');
查看分区表有哪些分区:show partitions tblName;
删除一个分区(数据一起删掉了):alter table tblName drop partition(dt='2015-12-12');
 
多个分区如何创建?
和单分区表的创建类似:
  create table tblName (
   id int comment 'ID',
   name string comment 'name' 
  ) partitioned by (year int comment 'admission year', school string comment 'school name')
  row format delimited
  fields terminated by ' ';
 
同时也可以从hdfs上引用数据:
alter table tblName partition(year='2015', school='crxy') set location hdfs_uri;
注意:
必须得现有分区,必须要使用hdfs绝对路径。
 
桶表
桶表是对数据进行哈希取值,然后放到不同文件中存储。查看每个桶文件中的内容,可以看出是通过对 buckets 取模确定的。
如何创建桶表?
create table tblName_bucket(id int) clustered by (id) into 3 buckets;
说明:
clustered by :按照什么分桶
into x buckets:分成x个桶
如何加载数据?
不能使用load data这种方式,需要从别的表来引用
insert into table tblName_bucket select * from tbl_other;
注意:在插入数据之前需要先设置开启桶操作,不然插入数据不会设置为桶!
set hive.enforce.bucketing=true;
桶表的主要作用:
数据抽样
提高某些查询效率
注意:
需要特别注意的是:clustered by 和 sorted by 不会影响数据的导入,这意味着,用户必须自己负责数据如何导入,包括数据的分桶和排序。
'set hive.enforce.bucketing = true'可以自动控制上一轮 reduce 的数量从而适配 bucket 的个数,
当然,用户也可以自主设置 mapred.reduce.tasks 去适配bucket 个数,
推荐使用'set hive.enforce.bucketing = true'。
原文地址:https://www.cnblogs.com/beiyi888/p/9645087.html