Hive 查询
一、 基本查询
1.1 全表和特定列查询
1.2 列别名
1.3 算术运算符
1.4 常用函数
1.5 Limit 语句
二、Where 语句
2.1 比较运算符
2.2 Like 和 Rlike
2.3 逻辑运算符(and/or/not)
三、分组
3.1 Group By 语句
3.2 Having 语句
四、Join语句
4.1 等值 Join
4.2 表的别名
4.3 内连接
4.4 左外连接
4.5 右外连接
4.6 满外连接
4.7 多表查询
4.8 笛卡尔积
4.9 连接谓词中不支持or
五、 排序
5.1 全局排序
5.2 按照别名排序
5.3 多个列排序
5.4 每个 MapReduce 内部排序(Sort By)
5.5 分区排序(Distribute By)
5.6 Cluster By
六、分桶及抽样查询
6.1 分桶表数据存储
分区针对的是数据的存储路径;分桶针对的是数据文件。
分区提供一个隔离数据和优化查询的便利方式。不过,并非所有的数据集都可形成合理的分区,特别是之前所提到过的要确定合适的划分大小这个疑虑。
分桶是将数据集分解成更容易管理的若*分的另一个技术。
-
先创建分桶表,通过直接导入数据文件的方式
① 数据准备1001 ss1 1002 ss2 1003 ss3 1004 ss4 1005 ss5 1006 ss6 1007 ss7 1008 ss8 1009 ss9 1010 ss10 1011 ss11 1012 ss12 1013 ss13 1014 ss14 1015 ss15 1016 ss16
② 创建分桶表
create table stu_buck(id int, name string) clustered by(id) into 4 buckets row format delimited fields terminated by '\t';
③ 查看表结构
hive (default)> desc formatted stu_buck; Num Buckets: 4
④ 导入数据到分桶表中
hive (default)> load data local inpath '/opt/module/datas/student.txt' into table stu_buck;
⑤ 查看创建的分桶表中是否分成4个桶
发现并没有分成4个桶。是什么原因呢? -
创建分桶表时,数据通过子查询的方式导入
① 先建一个普通的stu表create table stu(id int, name string) row format delimited fields terminated by '\t';
② 向普通的stu表中导入数据
load data local inpath '/opt/module/datas/student.txt' into table stu;
③ 清空stu_buck表中数据
truncate table stu_buck; select * from stu_buck;
④ 导入数据到分桶表,通过子查询的方式
insert into table stu_buck select id, name from stu;
⑤ 发现还是只有一个分桶,如图所示
⑥ 需要设置一个属性
hive (default)> set hive.enforce.bucketing=true; hive (default)> set mapreduce.job.reduces=-1; hive (default)> insert into table stu_buck select id, name from stu;
⑦ 查询分桶的数据
hive (default)> select * from stu_buck; OK stu_buck.id stu_buck.name 1004 ss4 1008 ss8 1012 ss12 1016 ss16 1001 ss1 1005 ss5 1009 ss9 1013 ss13 1002 ss2 1006 ss6 1010 ss10 1014 ss14 1003 ss3 1007 ss7 1011 ss11 1015 ss15
6.2 分桶抽样查询
对于非常大的数据集,有时用户需要使用的是一个具有代表性的查询结果而不是全部结果。Hive可以通过对表进行抽样来满足这个需求。
查询表stu_buck中的数据。
hive (default)> select * from stu_buck tablesample(bucket 1 out of 4 on id);
注:tablesample是抽样语句,语法:TABLESAMPLE(BUCKET x OUT OF y) 。
y必须是table总bucket数的倍数或者因子。hive根据y的大小,决定抽样的比例。例如,table总共分了4份,当y=2时,抽取(4/2=)2个bucket的数据,当y=8时,抽取(4/8=)1/2个bucket的数据。
x表示从哪个bucket开始抽取,如果需要取多个分区,以后的分区号为当前分区号加上y。例如,table总bucket数为4,tablesample(bucket 1 out of 2),表示总共抽取(4/2=)2个bucket的数据,抽取第1(x)个和第3(x+y)个bucket的数据。
注意:x的值必须小于等于y的值,否则
FAILED: SemanticException [Error 10061]: Numerator should not be bigger than denominator in sample clause for table stu_buck
七、其他常用查询函数
7.1 空字段赋值
-
函数说明
NVL:给值为NULL的数据赋值,它的格式是NVL( string1, replace_with)。它的功能是如果string1为NULL,则NVL函数返回replace_with的值,否则返回string1的值,如果两个参数都为NULL ,则返回NULL。 -
数据准备:采用员工表
-
查询:如果员工的comm为NULL,则用-1代替
hive (default)> select nvl(comm,-1) from emp; OK _c0 20.0 300.0 500.0 -1.0 1400.0 -1.0 -1.0 -1.0 -1.0 0.0 -1.0 -1.0 -1.0 -1.0
-
查询:如果员工的comm为NULL,则用领导id代替
hive (default)> select nvl(comm,mgr) from emp;
OK
_c0
20.0
300.0
500.0
7839.0
1400.0
7839.0
7839.0
7566.0
NULL
0.0
7788.0
7698.0
7566.0
7.2 CASE WHEN
-
数据准备
name dept_id sex 悟空 A 男 沙僧 B 男 八戒 A 男 白骨精 B 女 蜘蛛精 B 女 狐狸精 A 女 -
需求
求出不同部门各男女各多少人 -
创建本地 emp_sex.txt , 导入数据
[[email protected] datas]$ vim emp_sex.txt 悟空 A 男 沙僧 B 男 八戒 A 男 白骨精 B 女 蜘蛛精 B 女 狐狸精 A 女
-
创建hive表并导入数据
create table emp_sex( name string, dept_id string, sex string) row format delimited fields terminated by "\t"; load data local inpath '/opt/module/datas/emp_sex.txt' into table emp_sex;
-
按需求查询数据
select dept_id, sum(case sex when '男' then 1 else 0 end) male_count, sum(case sex when '女' then 1 else 0 end) female_count from emp_sex group by dept_id;
7.3 行转列
7.4 列转行
7.5 窗口函数
7.6 Rank
推荐阅读