文章目錄
- SQL練習:
- Hive資料型別
- 整型:TINYINT、SMALLINT、INT、BIGINT
- 浮點:FLOAT、DOUBLE
- 布爾型別:BOOL (False/True)
- 字串:STRING
- 時間型別:
- 復雜資料型別:
- Hive HQL
- DDL
- DML
SQL練習:
1、count(*)、count(1) 、count(‘欄位名’) 區別
2、HQL 執行優先級:
from、where、 group by 、having、order by、join、select 、limit
3、where 條件里不支持不等式子查詢,實際上是支持 in、not in、exists、not exists
-- 列出與“SCOTT”從事相同作業的所有員工,
select t1.EMPNO
,t1.ENAME
,t1.JOB
from emp t1
where t1.ENAME != "SCOTT" and t1.job in(
select job
from emp
where ENAME = "SCOTT");
7900,JAMES,CLERK,7698,1981-12-03,950,null,30
7902,FORD,ANALYST,7566,1981-12-03,3000,null,20
select t1.EMPNO
,t1.ENAME
,t1.JOB
from emp t1
where t1.ENAME != "SCOTT" and exists(
select job
from emp t2
where ENAME = "SCOTT"
and t1.job = t2.job
);
4、hive中大小寫不敏感
5、在hive中,資料中如果有null字串,加載到表中的時候會變成 null (不是字串)
如果需要判斷 null,使用 某個欄位名 is null 這樣的方式來判斷
或者使用 nvl() 函式,不能 直接 某個欄位名 == null
6、使用explain查看SQL執行計劃
explain select t1.EMPNO
,t1.ENAME
,t1.JOB
from emp t1
where t1.ENAME != "SCOTT" and t1.job in(
select job
from emp
where ENAME = "SCOTT");
# 查看更加詳細的執行計劃,加上extended
explain extended select t1.EMPNO
,t1.ENAME
,t1.JOB
from emp t1
where t1.ENAME != "SCOTT" and t1.job in(
select job
from emp
where ENAME = "SCOTT");
Hive資料型別
整型:TINYINT、SMALLINT、INT、BIGINT
浮點:FLOAT、DOUBLE
布爾型別:BOOL (False/True)
字串:STRING
時間型別:
- 時間戳 timestamp
- 日期 date
create table testDate(
ts timestamp
,dt date
) row format delimited fields terminated by ',';
// 2021-01-14 14:24:57.200,2021-01-11
- 時間戳與時間字串轉換
// from_unixtime 傳入一個時間戳以及pattern(yyyy-MM-dd) 可以將 時間戳轉換成對應格式的字串
select from_unixtime(1630915221,'yyyy年MM月dd日 HH時mm分ss秒')
// unix_timestamp 傳入一個時間字串以及pattern,可以將字串按照pattern轉換成時間戳
select unix_timestamp('2021年09月06日 16時00分21秒','yyyy年MM月dd日 HH時mm分ss秒');
select unix_timestamp('2021-01-14 14:24:57.200')
復雜資料型別:
- array
create table testArray(
name string,
weight array<string>
)row format delimited
fields terminated by '\t'
COLLECTION ITEMS terminated by ',';
select name,weight[0] from testArray;
楊老板 140,160,180
張志凱 160,200,180
-
map
key:value,key2:v2,k3:v3
create table scoreMap(
name string,
score map<string,int>
)ROW FORMAT DELIMITED
FIELDS TERMINATED BY '\t'
COLLECTION ITEMS TERMINATED BY ','
MAP KEYS TERMINATED BY ':';
select name,score['語文'] from scoreMap;
小明 語文:91,數學:110,英語:40
小紅 語文:100,數學:130,英語:140
- struct
create table scoreStruct(
name string,
score struct<course:string,score:int,course_id:int,tearcher:String>
)ROW FORMAT DELIMITED
FIELDS TERMINATED BY '\t'
COLLECTION ITEMS TERMINATED BY ',';
select name,score.course,score.score from scoreStruct;
小明 語文,91,000001,余老師
小紅 數學,100,000002,體育老師
https://blog.csdn.net/woshixuye/article/details/53317009
Hive HQL
DDL
DML
select id,name from tb t where ... and .... group by xxx having xxxx order by xxx asc/desc limit n;
-
where :過濾資料、!!!磁區裁剪!!!
-
join:left join、right join、join 注意MapJoin

-
group by : 通常結合聚合函式一起使用
-
order by:全域排序

-
sort by:區域排序

-
distribute by:磁區

-
cluster by

https://zhuanlan.zhihu.com/p/93747613 order by、distribute by、sort by、cluster by詳解
轉載請註明出處,本文鏈接:https://www.uj5u.com/qita/404096.html
標籤:其他
上一篇:三峽大學-復雜資料預處理實訓
