现在的位置: 首页 > 综合 > 正文

Hive表的分区与分桶

2018年06月05日 ⁄ 综合 ⁄ 共 1935字 ⁄ 字号 评论关闭

1.Hive分区表

Hive使用select语句进行查询的时候一般会扫描整个表内容,会消耗很多时间做没必要的工作。Hive可以在创建表的时候指定分区空间,这样在做查询的时候就可以很好的提高查询的效率。

创建分区表的语法:

create table tablename(
       name string
)partitioned by(key,type...);

示例

drop table if exists employees;
create table  if not exists employees(
       name string,
       salary float,
       subordinate array<string>,
       deductions map<string,float>,
       address struct<street:string,city:string,num:int>
) partitioned by (date_time string,type string)
row format delimited fields terminated by '\t'
collection items terminated by ','
map keys terminated by ':'
lines terminated by '\n'
stored as textfile
location '/hive/inner';

附:上述语句表示在建表时划分了date_time和type两个分区也叫双分区,一个分区的话就叫单分区,上述语句执行完以后我们查看表的结果会发现多了分区的两个字段。

desc employees;

结果如下:

注:在文件系统中的表现为date_time为一个文件夹,type为date_time的子文件夹。

向分区表中插入数据(要指定分区)

hive> load data local inpath '/usr/local/src/employee_data' into table employees partition(date_time='2015-01_24',type='userInfo');
Copying data from file:/usr/local/src/employee_data
Copying file: file:/usr/local/src/employee_data
Loading data to table default.employees partition (date_time=2015-01_24, type=userInfo)
OK
Time taken: 0.22 seconds
hive> 

数据插入后在文件系统中显示为:

注:从上图中我们就可以发现type分区是作为子文件夹的形式存在的。

添加分区:

alter table employees add if not exists partition(date_time='2088-08-18',type='liaozhongmin');

注:我们可以先添加分区,再向对应的分区中添加数据。

查看分区:

show partitions employees;

附:employees在这里表示表名。

删除不想要的分区

alter table employees drop if exists partition(date_time='2015-01_24',type='userInfo');

再次查看分区:

2.Hive桶表

对于每一个表或者是分区,Hive可以进一步组织成桶,也就是说桶是更为细粒度的数据范围划分。Hive是针对某一列进行分桶。Hive采用对列值哈希,然后除以桶的个数求余的方式决定该条记录存放在哪个桶中。分桶的好处是可以获得更高的查询处理效率。使取样更高效。

示例:

create table bucketed_user(
  id int,
  name string
)
clustered by(id) sorted by(name) into 4 buckets
row format delimited fields terminated by '\t'
stored as textfile;

我们使用用户id来确定如何划分桶(Hive使用对值进行哈希并将结果除于桶的个数取余数的方式进行分桶)

另外一个要注意的问题是使用桶表的时候我们要开启桶表:

set hive.enforce.bucketing = true;

现在我们将表employees中name和salary查询出来再插入到这张表中:

insert overwrite table bucketed_user select salary,name from employees;

我们通过查询语句可以查看插进来的数据:

数据在文件中的表现形式如下,分成了四个桶:

当从桶表中进行查询时,hive会根据分桶的字段进行计算分析出数据存放的桶中,然后直接到对应的桶中去取数据,这样做就很好的提高了效率。


抱歉!评论已关闭.