https://cwiki.apache.org/confluence/display/Hive/LanguageManual+Select
查询语句语法:
[WITH CommonTableExpression (, CommonTableExpression)*] (Note: Only available
starting with Hive 0.13.0)
SELECT [ALL | DISTINCT] select_expr, select_expr, ...
FROM table_reference
[WHERE where_condition]
[GROUP BY col_list]
[ORDER BY col_list]
[CLUSTER BY col_list
| [DISTRIBUTE BY col_list] [SORT BY col_list]
]
[LIMIT number]
全表和特定列查询
创建部门表
create table if not exists dept(
deptno int,
dname string,
loc int
)
row format delimited fields terminated by '\t';
创建员工表
create table if not exists emp(
empno int,
ename string,
job string,
mgr int,
hiredate string,
sal double,
comm double,
deptno int)
row format delimited fields terminated by '\t';
导入数据
hive (default)> load data local inpath '/opt/module/datas/dept.txt' into table dept;
hive (default)> load data local inpath '/opt/module/datas/emp.txt' into table emp;
全表查询
hive (default)> select * from emp;
选择特定列查询
hive (default)> select empno, ename from emp;
注意:
- SQL 语言大小写不敏感。
- SQL 可以写在一行或者多行
- 关键字不能被缩写也不能分行
- 各子句一般要分行写。
- 使用缩进提高语句的可读性。
列别名
- 重命名一个列
- 便于计算
- 紧跟列名,也可以在列名和别名之间加入关键字‘AS’
- 案例实操
查询名称和部门
hive (default)> select ename AS name, deptno dn from emp;
算术运算符
运算符 | 描述 |
---|---|
A+B | A和B 相加 |
A-B | A减去B |
A*B | A和B 相乘 |
A/B | A除以B |
A%B | A对B取余 |
A&B | A和B按位取与 |
A|B | A和B按位取或 |
A^B | A和B按位取异或 |
~A | A按位取反 |
案例实操
查询出所有员工的薪水后加1显示。
hive (default)> select sal +1 from emp;
常用函数
- 求总行数(count)
hive (default)> select count(*) cnt from emp;
- 求工资的最大值(max)
hive (default)> select max(sal) max_sal from emp;
- 求工资的最小值(min)
hive (default)> select min(sal) min_sal from emp;
- 求工资的总和(sum)
hive (default)> select sum(sal) sum_sal from emp;
- 求工资的平均值(avg)
hive (default)> select avg(sal) avg_sal from emp;
Limit语句
典型的查询会返回多行数据。LIMIT子句用于限制返回的行数。
hive (default)> select * from emp limit 5;
简书:https://www.jianshu.com/u/0278602aea1d
CSDN:https://blog.csdn.net/u012387141