@@ -456,3 +456,137 @@ ON d.loc = l.loc;
456
456
hive (default)> select empno, dname from emp, dept;
457
457
```
458
458
459
+ ### 4、排序
460
+
461
+ #### 4.1 全局排序(Order By)
462
+
463
+ Order By:全局排序,只有一个Reducer
464
+
465
+ 1)使用 ORDER BY 子句排序
466
+
467
+ - ASC(ascend):升序(默认)
468
+
469
+ - DESC(descend):降序
470
+
471
+ 2)ORDER BY 子句在SELECT语句的结尾
472
+
473
+ 3)案例实操
474
+
475
+ (1)查询员工信息按工资升序排列
476
+
477
+ ``` sql
478
+ hive (default)> select * from emp order by sal;
479
+ ```
480
+
481
+ (2)查询员工信息按工资降序排列
482
+
483
+ ``` sql
484
+ hive (default)> select * from emp order by sal desc ;
485
+ ```
486
+
487
+ #### 4.2 按照别名排序
488
+
489
+ 按照员工薪水的2倍排序
490
+
491
+ ``` sql
492
+ hive (default)> select ename, sal* 2 twosal from emp order by twosal;
493
+ ```
494
+
495
+ #### 4.3 多个列排序
496
+
497
+ 按照部门和工资升序排序
498
+
499
+ ``` sql
500
+ hive (default)> select ename, deptno, sal from emp order by deptno, sal;
501
+ ```
502
+
503
+ #### 4.4 每个Reduce内部排序(Sort By)
504
+
505
+ Sort By:对于大规模的数据集order by的效率非常低。在很多情况下,并不需要全局排序,此时可以使用sort by。
506
+
507
+ Sort by为每个reducer产生一个排序文件。每个Reducer内部进行排序,对全局结果集来说不是排序。
508
+
509
+ 1)设置reduce个数
510
+
511
+ ``` sql
512
+ hive (default)> set mapreduce .job .reduces= 3 ;
513
+ ```
514
+
515
+ 2)查看设置reduce个数
516
+
517
+ ``` sql
518
+ hive (default)> set mapreduce .job .reduces;
519
+ ```
520
+
521
+ 3)根据部门编号降序查看员工信息
522
+
523
+ ``` sql
524
+ hive (default)> select * from emp sort by deptno desc ;
525
+ ```
526
+
527
+ 4)将查询结果导入到文件中(按照部门编号降序排序)
528
+
529
+ ``` sql
530
+ hive (default)> insert overwrite local directory ' /opt/module/hive/datas/sortby-result'
531
+ select * from emp sort by deptno desc ;
532
+ ```
533
+
534
+ #### 4.5 分区(Distribute By)
535
+
536
+ Distribute By:在有些情况下,我们需要控制某个特定行应该到哪个reducer,通常是为了进行后续的聚集操作。distribute by 子句可以做这件事。distribute by类似MR中partition(自定义分区),进行分区,结合sort by使用。
537
+
538
+ 对于distribute by进行测试,一定要分配多reduce进行处理,否则无法看到distribute by的效果。
539
+
540
+ 1)案例实操:
541
+
542
+ (1)先按照部门编号分区,再按照员工编号降序排序。
543
+
544
+ ``` sql
545
+ hive (default)> set mapreduce .job .reduces= 3 ;
546
+ hive (default)> insert overwrite local directory ' /opt/module/hive/datas/distribute-result' select * from emp distribute by deptno sort by empno desc ;
547
+ ```
548
+
549
+ 注意:
550
+
551
+ - distribute by的分区规则是根据分区字段的hash码与reduce的个数进行模除后,余数相同的分到一个区。
552
+
553
+ - Hive要求DISTRIBUTE BY语句要写在SORT BY语句之前。
554
+
555
+ #### 4.6 Cluster By
556
+
557
+ 当distribute by和sort by字段相同时,可以使用cluster by方式。
558
+
559
+ cluster by除了具有distribute by的功能外还兼具sort by的功能。但是排序只能是升序排序,不能指定排序规则为ASC或者DESC。
560
+
561
+ (1)以下两种写法等价
562
+
563
+ ``` sql
564
+ hive (default)> select * from emp cluster by deptno;
565
+ hive (default)> select * from emp distribute by deptno sort by deptno;
566
+ ```
567
+
568
+ 注意:按照部门编号分区,不一定就是固定死的数值,可以是20号和30号部门分到一个分区里面去。
569
+
570
+ ### 5、抽样查询
571
+
572
+ 对于非常大的数据集,有时用户需要使用的是一个具有代表性的查询结果而不是全部结果。Hive可以通过对表进行抽样来满足这个需求。
573
+
574
+ 查询表stu_buck中的数据。
575
+
576
+ ``` sql
577
+ hive (default)> select * from stu_buck tablesample(bucket 1 out of 4 on id);
578
+ ```
579
+
580
+ 注:tablesample是抽样语句,语法:TABLESAMPLE(BUCKET x OUT OF y) 。
581
+
582
+ y必须是table总bucket数的倍数或者因子。hive根据y的大小,决定抽样的比例。例如,table总共分了4份,当y=2时,抽取(4/2=)2个bucket的数据,当y=8时,抽取(4/8=)1/2个bucket的数据。
583
+
584
+ x表示从哪个bucket开始抽取,如果需要取多个分区,以后的分区号为当前分区号加上y。例如,table总bucket数为4,tablesample(bucket 1 out of 2),表示总共抽取(4/2=)2个bucket的数据,抽取第1(x)个和第3(x+y)个bucket的数据。
585
+
586
+ 注意:x的值必须小于等于y的值,否则
587
+
588
+ ``` sql
589
+ FAILED: SemanticException [Error 10061 ]: Numerator should not be bigger than denominator in sample clause for table stu_buck
590
+ ```
591
+
592
+
0 commit comments