hive中分区表,桶的使用

hive中分区表的使用:

 

1. 创建一个分区表,以 ds 为分区列:

create table invites (id int, name string) partitioned by (ds string) row format delimited fields terminated by '\t' stored as textfile;

2. 将数据添加到时间为 2012-10-12 这个分区中:

load data local inpath '/home/hadoop/Desktop/data.txt' overwrite into table invites partition (ds='2012-10-12');

3. 将数据添加到时间为 2012-10-20 这个分区中:

load data local inpath '/home/hadoop/Desktop/data.txt' overwrite into table invites partition (ds='2012-10-20');

4. 从一个分区中查询数据:

select * from invites where ds ='2012-10-12';

5.  往一个分区表的某一个分区中添加数据:

insert overwrite table invites partition (ds='2012-10-12') select id,max(name) from test group by id;

可以查看分区的具体情况,使用命令:

hadoop fs -ls /home/hadoop.hive/warehouse/invites

如果想在 eslipse 下面看效果,也是需要开启 hadoop 的, start-all.sh



hive 中桶的使用:

1. 创建带桶的 table

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;


2. 强制多个 reduce 进行输出:

set hive.enforce.bucketing=true;


3. 往表中插入数据:

insert overwrite table bucketed_user select * from test;


4. 查看表的结构,会发现当前表下有四个文件:

dfs -ls /home/hadoop/hive/warehouse/bucketed_user;


5. 读取数据,看没一个文件的数据:

dfs -cat /home/hadoop/hive/warehouse/bucketed_user/000000_0;

桶使用 hash 来实现,所以每个文件拥有的数据的个数都有可能不相等。


6. 对桶中的数据进行采样:

select * from bucketed_user tablesample(bucket 1 out of 4 on name);

桶的个数从 1 开始计数,前面的查询从 4 个桶中的第一个桶获取数据。其实就是四分之一。


7. 查询一半返回的桶数:

select * from bucketed_user tablesample(bucket 1 out of 2 on name);

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值