hive的常用HQL语句

Posted wakerwang

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了hive的常用HQL语句相关的知识,希望对你有一定的参考价值。

1、过滤条件
where 、limit、 distinct、 between and 、 null、 is not null
select * from emp where sal > 3000;
select * from emp limit 1;
select distinct deptno from emp;
select * from emp where sal between 2000 and 3000;
select ename from emp where comm is null;
select ename from emp where comm is not null;

2、聚合函数
count、 sum、 avg、 max、 min 、group by、 having

select count(1) from emp;
select count(*) from emp; -》运行效率较低

select avg(sal) avg_sal from emp;
select deptno,avg(sal) from emp group by deptno;
select deptno,avg(sal) avg_sal from emp group by deptno having avg_sal > 2000;

3、join
等值join
左join left
右join right
全join full

 

select e.empno,e.ename,d.deptno,e.sal from emp e join dept d on e.deptno=d.deptno;

select e.empno,e.ename,d.deptno,e.sal from emp e left join dept d on e.deptno=d.deptno;

select e.empno,e.ename,d.deptno,e.sal from emp e right join dept d on e.deptno=d.deptno;

select e.empno,e.ename,d.deptno,e.sal from emp e full join dept d on e.deptno=d.deptno;

4、hive中的几种排序方式
1、order by
select * from emp order by sal;
2、sort by

insert overwrite local directory ‘/opt/datas/emp_sort‘ row format delimited fields terminated by ‘ ‘ select * from emp sort by sal;

3、distribute by

insert overwrite local directory ‘/opt/datas/emp_dist‘ row format delimited fields terminated by ‘ ‘ select * from emp distribute by deptno sort by sal;

4、cluster by
=distribute by+sort by

insert overwrite local directory ‘/opt/datas/emp_cls‘ row format delimited fields terminated by ‘ ‘ select * from emp cluster by sal;






















以上是关于hive的常用HQL语句的主要内容,如果未能解决你的问题,请参考以下文章

Hive 源码解读 CliDriver HQL 语句拆分

Hive 源码解读 CliDriver HQL 语句拆分

Hive 源码解读 CliDriver HQL 语句拆分

Hive 编程之DDLDMLUDFSelect总结

Hive内容总结

Hive 源码解读 Driver 将 HQL 语句转换为 AST