转载:https://www.cnblogs.com/end/archive/2013/01/09/2852413.html

hive中table可以拆分成partition,table和partition可以通过‘CLUSTERED BY ’进一步分bucket,bucket中的数据可以通过‘SORT BY’排序。
bucket主要作用:
1. 数据sampling
2. 提升某些查询操作效率,例如mapside join
需要特别注意的是:clustered by和sorted by不会影响数据的导入,这意味着,用户必须自己负责数据如何如何导入,包括数据的分桶和排序。
'set hive.enforce.bucketing = true' 可以自动控制上一轮reduce的数量从而适配bucket的个数,当然,用户也可以自主设置mapred.reduce.tasks去适配bucket 个数,推荐使用'set hive.enforce.bucketing = true'

示例:
建临时表student_tmp,并导入数据:
hive> desc student_tmp;         
OK
id      int
age     int
name    string
stat_date       string
Time taken: 0.106 seconds
hive> select * from student_tmp;
OK
1       20      zxm     20120801
2       21      ljz     20120801
3       19      cds     20120801
4       18      mac     20120801
5       22      android 20120801
6       23      symbian 20120801
7       25      wp      20120801
Time taken: 0.123 seconds

建student表:
hive>create table student(id INT, age INT, name STRING)
       >partitioned by(stat_date STRING) 
       >clustered by(id) sorted by(age) into 2 bucket
       >row format delimited fields terminated by ',';

设置环境变量:
       >set hive.enforce.bucketing = true; 

插入数据:

>from student_tmp 
       >insert overwrite table student partition(stat_date="20120802") 
       >select id,age,name where stat_date="20120801" sort by age;

查看文件目录:
$ hadoop fs -ls /user/hive/warehouse/studentstat_date=20120802/
Found 2 items
-rw-r--r--   1 work supergroup         31 2012-07-31 19:52 /user/hive/warehouse/student/stat_date=20120802/000000_0
-rw-r--r--   1 work supergroup         39 2012-07-31 19:52 /user/hive/warehouse/student/stat_date=20120802/000001_0

查看sampling数据:

hive> select * from student tablesample(bucket 1 out of 2 on id);                                                                               
Total MapReduce jobs = 1
Launching Job 1 out of 1
.......
OK
4       18      mac     20120802
2       21      ljz     20120802
6       23      symbian 20120802
Time taken: 20.608 seconds

tablesample是抽样语句,语法:TABLESAMPLE(BUCKET x OUT OF y)

y必须是table总bucket数的倍数或者因子。hive根据y的大小,决定抽样的比例。例如,table总共分了64份,当y=32时,抽取 (64/32=)2个bucket的数据,当y=128时,抽取(64/128=)1/2个bucket的数据。x表示从哪个bucket开始抽取。例 如,table总bucket数为32,tablesample(bucket 3 out of 16),表示总共抽取(32/16=)2个bucket的数据,分别为第3个bucket和第(3+16=)19个bucket的数据。

reference:
Working with bucketed tables
Data manipulation statements

最新文章

  1. 设置html title标题左侧的小图标
  2. Android中ListView的几种常见的优化方法
  3. 第15章 设备无关位图_15.1 DIB文件格式
  4. 【原】Gradle调用shell脚本和python脚本并传参
  5. Ztree使用笔记
  6. POJ-1947 Rebuilding Roads (树形DP+分组背包)
  7. SQL Server 数据库文件管理
  8. jsp是有自己的编译引擎的
  9. Linux C 程序 指针和字符串函数(11)
  10. uva 12253 - Simple Encryption(dfs)
  11. JAVA中的正则表达式--待续
  12. Print! Print! Print!
  13. js中静态函数与变量
  14. Linux系统管理员必备的监控工具(88款)
  15. hashMap和treeMap
  16. 大赛获奖选手专访 | 冷燕冰:最佳设计奖TIMING里的时机和时序
  17. VS下使用Google Protobuf完成SOCKET通信
  18. 移动网页广告引入mraid.js使用指南
  19. TP文件上传
  20. linux 安装Git详细过程

热门文章

  1. [AtCoder AGC27A]Candy Distribution Again
  2. ZOJ 1280 Interesting Lines | 求两直线交点
  3. nginx简易部署
  4. 编写Shell脚本的最佳实践,规范一
  5. Java I/O 笔记
  6. Codeforces Round #395 Div.2 题解
  7. OpenGL入门学习(四)
  8. commons-lang3中DateUtils类方法介绍
  9. ubuntu16.04安装flash plugin
  10. bisect维护已排序的序列