Skip to content

Commit 345c94b

Browse files
authored
Create 5、Hive查询.md
1 parent 58354b5 commit 345c94b

File tree

1 file changed

+291
-0
lines changed

1 file changed

+291
-0
lines changed

Hive/5、Hive查询.md

Lines changed: 291 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,291 @@
1+
## Hive查询
2+
3+
### 1、基本查询
4+
5+
#### 1.1 全表和特定列查询
6+
7+
0)数据准备
8+
9+
(0)原始数据
10+
11+
dept:
12+
13+
```xml
14+
10 ACCOUNTING 1700
15+
20 RESEARCH 1800
16+
30 SALES 1900
17+
40 OPERATIONS 1700
18+
```
19+
20+
emp:
21+
22+
```xml
23+
369 SMITH CLERK 7902 1980-12-17 800.00 20
24+
7499 ALLEN SALESMAN 7698 1981-2-20 1600.00 300.00 30
25+
7521 WARD SALESMAN 7698 1981-2-22 1250.00 500.00 30
26+
7566 JONES MANAGER 7839 1981-4-2 2975.00 20
27+
7654 MARTIN SALESMAN 7698 1981-9-28 1250.00 1400.00 30
28+
7698 BLAKE MANAGER 7839 1981-5-1 2850.00 30
29+
7782 CLARK MANAGER 7839 1981-6-9 2450.00 10
30+
7788 SCOTT ANALYST 7566 1987-4-19 3000.00 20
31+
7839 KING PRESIDENT 1981-11-17 5000.00 10
32+
7844 TURNER SALESMAN 7698 1981-9-8 1500.00 0.00 30
33+
7876 ADAMS CLERK 7788 1987-5-23 1100.00 20
34+
7900 JAMES CLERK 7698 1981-12-3 950.00 30
35+
7902 FORD ANALYST 7566 1981-12-3 3000.00 20
36+
7934 MILLER CLERK 7782 1982-1-23 1300.00 10
37+
```
38+
39+
(1)创建部门表
40+
41+
```sql
42+
create table if not exists dept(
43+
deptno int,
44+
dname string,
45+
loc int
46+
)
47+
row format delimited fields terminated by '\t';
48+
```
49+
50+
(2)创建员工表
51+
52+
```sql
53+
create table if not exists emp(
54+
empno int,
55+
ename string,
56+
job string,
57+
mgr int,
58+
hiredate string,
59+
sal double,
60+
comm double,
61+
deptno int)
62+
row format delimited fields terminated by '\t';
63+
```
64+
65+
(3)导入数据
66+
67+
```sql
68+
load data local inpath '/opt/module/datas/dept.txt' into table dept;
69+
load data local inpath '/opt/module/datas/emp.txt' into table emp;
70+
```
71+
72+
1)全表查询
73+
74+
```sql
75+
hive (default)> select * from emp;
76+
hive (default)> select empno,ename,job,mgr,hiredate,sal,comm,deptno from emp;
77+
```
78+
79+
2)选择特定列查询
80+
81+
```sql
82+
hive (default)> select empno, ename from emp;
83+
```
84+
85+
注意:
86+
87+
(1)SQL 语言大小写不敏感。
88+
89+
(2)SQL 可以写在一行或者多行
90+
91+
(3)关键字不能被缩写也不能分行
92+
93+
(4)各子句一般要分行写。
94+
95+
(5)使用缩进提高语句的可读性。
96+
97+
#### 1.2 列别名
98+
99+
1)重命名一个列
100+
101+
2)便于计算
102+
103+
3)紧跟列名,也可以在列名和别名之间加入关键字‘AS’
104+
105+
4)案例实操
106+
107+
查询名称和部门
108+
109+
```sql
110+
hive (default)> select ename AS name, deptno dn from emp;
111+
```
112+
113+
#### 1.3 算术运算符
114+
115+
| 运算符 | 描述 |
116+
| ------ | -------------- |
117+
| A+B | A和B 相加 |
118+
| A-B | A减去B |
119+
| A*B | A和B 相乘 |
120+
| A/B | A除以B |
121+
| A%B | A对B取余 |
122+
| A&B | A和B按位取与 |
123+
| A\|B | A和B按位取或 |
124+
| A^B | A和B按位取异或 |
125+
| ~A | A按位取反 |
126+
127+
案例实操:查询出所有员工的薪水后加1显示。
128+
129+
```sql
130+
hive (default)> select sal +1 from emp;
131+
```
132+
133+
#### 1.4 常用函数
134+
135+
1)求总行数(count)
136+
137+
```sql
138+
hive (default)> select count(*) cnt from emp;
139+
```
140+
141+
2)求工资的最大值(max)
142+
143+
```sql
144+
hive (default)> select max(sal) max_sal from emp;
145+
```
146+
147+
3)求工资的最小值(min)
148+
149+
```sql
150+
hive (default)> select min(sal) min_sal from emp;
151+
```
152+
153+
4)求工资的总和(sum)
154+
155+
```sql
156+
hive (default)> select sum(sal) sum_sal from emp;
157+
```
158+
159+
5)求工资的平均值(avg)
160+
161+
```sql
162+
hive (default)> select avg(sal) avg_sal from emp;
163+
```
164+
165+
#### 1.5 Limit语句
166+
167+
典型的查询会返回多行数据。LIMIT子句用于限制返回的行数。
168+
169+
```sql
170+
hive (default)> select * from emp limit 5;
171+
hive (default)> select * from emp limit 2,3;
172+
```
173+
174+
#### 1.6 Where语句
175+
176+
1)使用WHERE子句,将不满足条件的行过滤掉
177+
178+
2)WHERE子句紧随FROM子句
179+
180+
3)案例实操
181+
182+
查询出薪水大于1000的所有员工
183+
184+
```sql
185+
hive (default)> select * from emp where sal >1000;
186+
```
187+
188+
注意:where子句中不能使用字段别名。
189+
190+
#### 1.7 比较运算符(Between/In/ Is Null)
191+
192+
1)下面表中描述了谓词操作符,这些操作符同样可以用于JOIN…ON和HAVING语句中。
193+
194+
| 操作符 | 支持的数据类型 | 描述 |
195+
| ----------------------- | -------------- | ------------------------------------------------------------ |
196+
| A=B | 基本数据类型 | 如果A等于B则返回TRUE,反之返回FALSE |
197+
| A<=>B | 基本数据类型 | 如果A和B都为NULL,则返回TRUE,如果一边为NULL,返回False |
198+
| A<>B, A!=B | 基本数据类型 | A或者B为NULL则返回NULL;如果A不等于B,则返回TRUE,反之返回FALSE |
199+
| A<B | 基本数据类型 | A或者B为NULL,则返回NULL;如果A小于B,则返回TRUE,反之返回FALSE |
200+
| A<=B | 基本数据类型 | A或者B为NULL,则返回NULL;如果A小于等于B,则返回TRUE,反之返回FALSE |
201+
| A>B | 基本数据类型 | A或者B为NULL,则返回NULL;如果A大于B,则返回TRUE,反之返回FALSE |
202+
| A>=B | 基本数据类型 | A或者B为NULL,则返回NULL;如果A大于等于B,则返回TRUE,反之返回FALSE |
203+
| A [NOT] BETWEEN B AND C | 基本数据类型 | 如果A,B或者C任一为NULL,则结果为NULL。如果A的值大于等于B而且小于或等于C,则结果为TRUE,反之为FALSE。如果使用NOT关键字则可达到相反的效果。 |
204+
| A IS NULL | 所有数据类型 | 如果A等于NULL,则返回TRUE,反之返回FALSE |
205+
| A IS NOT NULL | 所有数据类型 | 如果A不等于NULL,则返回TRUE,反之返回FALSE |
206+
| IN(数值1, 数值2) | 所有数据类型 | 使用 IN运算显示列表中的值 |
207+
| A [NOT] LIKE B | STRING 类型 | B是一个SQL下的简单正则表达式,也叫通配符模式,如果A与其匹配的话,则返回TRUE;反之返回FALSE。B的表达式说明如下:‘x%’表示A必须以字母‘x’开头,‘%x’表示A必须以字母’x’结尾,而‘%x%’表示A包含有字母’x’,可以位于开头,结尾或者字符串中间。如果使用NOT关键字则可达到相反的效果。 |
208+
| A RLIKE B, A REGEXP B | STRING 类型 | B是基于java的正则表达式,如果A与其匹配,则返回TRUE;反之返回FALSE。匹配使用的是JDK中的正则表达式接口实现的,因为正则也依据其中的规则。例如,正则表达式必须和整个字符串A相匹配,而不是只需与其字符串匹配。 |
209+
210+
2)案例实操
211+
212+
(1)查询出薪水等于5000的所有员工
213+
214+
```sql
215+
hive (default)> select * from emp where sal =5000;
216+
```
217+
218+
(2)查询工资在500到1000的员工信息
219+
220+
```sql
221+
hive (default)> select * from emp where sal between 500 and 1000;
222+
```
223+
224+
(3)查询comm为空的所有员工信息
225+
226+
```sql
227+
hive (default)> select * from emp where comm is null;
228+
```
229+
230+
(4)查询工资是1500或5000的员工信息
231+
232+
```sql
233+
hive (default)> select * from emp where sal IN (1500, 5000);
234+
```
235+
236+
#### 1.8 Like和RLike
237+
238+
1)使用LIKE运算选择类似的值
239+
240+
2)选择条件可以包含字符或数字:
241+
242+
% 代表零个或多个字符(任意个字符)。
243+
244+
_ 代表一个字符。
245+
246+
3)RLIKE子句
247+
248+
RLIKE子句是Hive中这个功能的一个扩展,其可以通过Java的正则表达式这个更强大的语言来指定匹配条件。
249+
250+
4)案例实操
251+
252+
(1)查找名字以A开头的员工信息
253+
254+
hive (default)> select * from emp where ename LIKE 'A%';
255+
256+
(2)查找名字中第二个字母为A的员工信息
257+
258+
hive (default)> select * from emp where ename LIKE '_A%';
259+
260+
(3)查找名字中带有A的员工信息
261+
262+
hive (default)> select * from emp where ename RLIKE '[A]';
263+
264+
#### 1.9 逻辑运算符(And/Or/Not)
265+
266+
| 操作符 | 含义 |
267+
| ------ | ------ |
268+
| AND | 逻辑并 |
269+
| OR | 逻辑或 |
270+
| NOT | 逻辑否 |
271+
272+
1)案例实操
273+
274+
(1)查询薪水大于1000,部门是30
275+
276+
```sql
277+
hive (default)> select * from emp where sal>1000 and deptno=30;
278+
```
279+
280+
(2)查询薪水大于1000,或者部门是30
281+
282+
```sql
283+
hive (default)> select * from emp where sal>1000 or deptno=30;
284+
```
285+
286+
(3)查询除了20部门和30部门以外的员工信息
287+
288+
```sql
289+
hive (default)> select * from emp where deptno not IN(30, 20);
290+
```
291+

0 commit comments

Comments
 (0)