国产 无码 综合区,色欲AV无码国产永久播放,无码天堂亚洲国产AV,国产日韩欧美女同一区二区

hive sql 語(yǔ)句查詢(xún)規(guī)則

這篇具有很好參考價(jià)值的文章主要介紹了hive sql 語(yǔ)句查詢(xún)規(guī)則。希望對(duì)大家有所幫助。如果存在錯(cuò)誤或未考慮完全的地方,請(qǐng)大家不吝賜教,您也可以點(diǎn)擊"舉報(bào)違法"按鈕提交疑問(wèn)。

一、單表查詢(xún)

SELECT [ALL | DISTINCT] select_expr, select_expr, ... 
FROM table_reference
[WHERE where_condition] 
[GROUP BY col_list [HAVING condition]] 
[CLUSTER BY col_list 
  | [DISTRIBUTE BY col_list] [SORT BY| ORDER BY col_list] 
] 
[LIMIT number]

注意:
1、order by 會(huì)對(duì)輸入做全局排序,因此只有一個(gè)reducer,會(huì)導(dǎo)致當(dāng)輸入規(guī)模較大時(shí),需要較長(zhǎng)的計(jì)算時(shí)間。
2、sort by不是全局排序,其在數(shù)據(jù)進(jìn)入reducer前完成排序。因此,如果用sort by進(jìn)行排序,并且設(shè)置mapred.reduce.tasks>1,則sort by只保證每個(gè)reducer的輸出有序,不保證全局有序。
3、distribute by(字段)根據(jù)指定的字段將數(shù)據(jù)分到不同的reducer,且分發(fā)算法是hash散列。
4、Cluster by(字段) 除了具有Distribute by的功能外,還會(huì)對(duì)該字段進(jìn)行排序。
因此,如果分桶和sort字段是同一個(gè)時(shí),此時(shí),cluster by = distribute by + sort by

1.1 WHERE語(yǔ)句

select * from score where s_score < 60;

注意:
小于某個(gè)值是不包含null的,如上查詢(xún)結(jié)果是把 s_score 為 null 的行剔除的

1.2 GROUP BY 分組

select s_id ,avg(s_score) from score group by s_id;

分組后對(duì)數(shù)據(jù)進(jìn)行篩選,使用having
 select s_id ,avg(s_score) avgscore from score group by s_id having avgscore > 85;
注意:
如果使用 group by 分組,則 select 后面只能寫(xiě)分組的字段或者聚合函數(shù)
where和having區(qū)別:
1 having是在 group by 分完組之后再對(duì)數(shù)據(jù)進(jìn)行篩選,所以having 要篩選的字段只能是分組字段或者聚合函數(shù)
2 where 是從數(shù)據(jù)表中的字段直接進(jìn)行的篩選的,所以不能跟在gruop by后面,也不能使用聚合函數(shù)

1.3 join 連接

INNER JOIN 內(nèi)連接:只有進(jìn)行連接的兩個(gè)表中都存在與連接條件相匹配的數(shù)據(jù)才會(huì)被保留下來(lái)
select * from techer t [inner] join course c on t.t_id = c.t_id; -- inner 可省略

LEFT OUTER JOIN 左外連接:左邊所有數(shù)據(jù)會(huì)被返回,右邊符合條件的被返回
select * from techer t left join course c on t.t_id = c.t_id; -- outer可省略

RIGHT OUTER JOIN 右外連接:右邊所有數(shù)據(jù)會(huì)被返回,左邊符合條件的被返回、
select * from techer t right join course c on t.t_id = c.t_id;

FULL OUTER JOIN 滿外(全外)連接: 將會(huì)返回所有表中符合條件的所有記錄。如果任一表的指定字段沒(méi)有符合條件的值的話,那么就使用NULL值替代。
SELECT * FROM techer t FULL JOIN course c ON t.t_id = c.t_id ;
注:1. hive2版本已經(jīng)支持不等值連接,就是 join on條件后面可以使用大于小于符號(hào)了;并且也支持 join on 條件后跟or (早前版本 on 后只支持 = 和 and,不支持 > < 和 or)
2.如hive執(zhí)行引擎使用MapReduce,一個(gè)join就會(huì)啟動(dòng)一個(gè)job,一條sql語(yǔ)句中如有多個(gè)join,則會(huì)啟動(dòng)多個(gè)job
注意:表之間用逗號(hào)(,)連接和 inner join 是一樣的
select * from table_a,table_b where table_a.id=table_b.id;
它們的執(zhí)行效率沒(méi)有區(qū)別,只是書(shū)寫(xiě)方式不同,用逗號(hào)是sql 89標(biāo)準(zhǔn),join 是sql 92標(biāo)準(zhǔn)。用逗號(hào)連接后面過(guò)濾條件用 where ,用 join 連接后面過(guò)濾條件是 on。

1.4 order by 排序

全局排序,只會(huì)有一個(gè)reduce
ASC(ascend): 升序(默認(rèn)) DESC(descend): 降序
SELECT * FROM student s LEFT JOIN score sco ON s.s_id = sco.s_id ORDER BY sco.s_score DESC;
注意:order by 是全局排序,所以最后只有一個(gè)reduce,也就是在一個(gè)節(jié)點(diǎn)執(zhí)行,如果數(shù)據(jù)量太大,就會(huì)耗費(fèi)較長(zhǎng)時(shí)間

1.5 sort by 局部排序

查詢(xún)成績(jī)按照成績(jī)降序排列
select * from score sort by s_score;

1.6 distribute by 分區(qū)排序

distribute by:類(lèi)似MR中partition,進(jìn)行分區(qū),結(jié)合sort by使用

通過(guò)distribute by  進(jìn)行數(shù)據(jù)的分區(qū)
select * from score distribute by s_id sort by s_score;
注意:Hive要求 distribute by 語(yǔ)句要寫(xiě)在 sort by 語(yǔ)句之前

1.7 cluster by

當(dāng)distribute by和sort by字段相同時(shí),可以使用cluster by方式.
cluster by除了具有distribute by的功能外還兼具sort by的功能。但是排序只能是正序排序,不能指定排序規(guī)則為ASC或者DESC。

以下兩種寫(xiě)法等價(jià)
select * from score cluster by s_id;
select * from score distribute by s_id sort by s_id;

二、Hive函數(shù)

2.1 聚合函數(shù)-count(),max(),min(),sum(),avg()

注意:
聚合操作時(shí)要注意null值
count(*) 包含null值,統(tǒng)計(jì)所有行數(shù)
count(id) 不包含null值
min 求最小值是不包含null,除非所有值都是null
avg 求平均值也是不包含null
  • 非空集合總體變量函數(shù): var_pop
語(yǔ)法: var_pop(col)
返回值: double
說(shuō)明: 統(tǒng)計(jì)結(jié)果集中col非空集合的總體變量(忽略null)
  • 非空集合樣本變量函數(shù): var_samp
語(yǔ)法: var_samp (col)
返回值: double
說(shuō)明: 統(tǒng)計(jì)結(jié)果集中col非空集合的樣本變量(忽略null)
  • 總體標(biāo)準(zhǔn)偏離函數(shù): stddev_pop
語(yǔ)法: stddev_pop(col)
返回值: double
說(shuō)明: 該函數(shù)計(jì)算總體標(biāo)準(zhǔn)偏離,并返回總體變量的平方根,其返回值與VAR_POP函數(shù)的平方根相同
  • 中位數(shù)函數(shù): percentile
語(yǔ)法: percentile(BIGINT col, p)
返回值: double
說(shuō)明: 求準(zhǔn)確的第pth個(gè)百分位數(shù),p必須介于0和1之間,但是col字段目前只支持整數(shù),不支持浮點(diǎn)數(shù)類(lèi)型

2.2 關(guān)系運(yùn)算

支持:等值(=)、不等值(!= 或 <>)、小于(<)、小于等于(<=)、大于(>)、大于等于(>=)

空值判斷(is null)、非空判斷(is not null)
  • LIKE比較: LIKE
語(yǔ)法: A LIKE B
操作類(lèi)型: strings
描述: 如果字符串A或者字符串B為NULL,則返回NULL;如果字符串A符合表達(dá)式B 的正則語(yǔ)法,則為T(mén)RUE;否則為FALSE。B中字符”_”表示任意單個(gè)字符,而字符”%”表示任意數(shù)量的字符。
  • JAVA的LIKE操作: RLIKE
語(yǔ)法: A RLIKE B
操作類(lèi)型: strings
描述: 如果字符串A或者字符串B為NULL,則返回NULL;如果字符串A符合JAVA正則表達(dá)式B的正則語(yǔ)法,則為T(mén)RUE;否則為FALSE。
  • REGEXP操作: REGEXP
語(yǔ)法: A REGEXP B
操作類(lèi)型: strings
描述: 功能與RLIKE相同
示例:select 1 from tableName where 'footbar' REGEXP '^f.*r$';
結(jié)果:1

2.3 數(shù)學(xué)運(yùn)算

支持所有數(shù)值類(lèi)型:加(+)、減(-)、乘(*)、除(/)、取余(%)、位與(&)、位或(|)、位異或(^)、位取反(~)

2.4 邏輯運(yùn)算

支持:邏輯與(and)、邏輯或(or)、邏輯非(not)

2.5 數(shù)值運(yùn)算

  • 取整函數(shù): round
語(yǔ)法: round(double a)
返回值: BIGINT
說(shuō)明: 返回double類(lèi)型的整數(shù)值部分 (遵循四舍五入)
示例:select round(3.1415926) from tableName;
結(jié)果:3
  • 指定精度取整函數(shù): round
語(yǔ)法: round(double a, int d)
返回值: DOUBLE
說(shuō)明: 返回指定精度d的double類(lèi)型
hive> select round(3.1415926,4) from tableName;
3.1416
  • 向下取整函數(shù): floor
語(yǔ)法: floor(double a)
返回值: BIGINT
說(shuō)明: 返回等于或者小于該double變量的最大的整數(shù)
hive> select floor(3.641) from tableName;
3
  • 向上取整函數(shù): ceil
語(yǔ)法: ceil(double a)
返回值: BIGINT
說(shuō)明: 返回等于或者大于該double變量的最小的整數(shù)
hive> select ceil(3.1415926) from tableName;
4
  • 取隨機(jī)數(shù)函數(shù): rand
語(yǔ)法: rand(),rand(int seed)
返回值: double
說(shuō)明: 返回一個(gè)0到1范圍內(nèi)的隨機(jī)數(shù)。如果指定種子seed,則會(huì)等到一個(gè)穩(wěn)定的隨機(jī)數(shù)序列
hive> select rand() from tableName; -- 每次執(zhí)行此語(yǔ)句得到的結(jié)果都不同
0.5577432776034763

hive> select rand(100) ;  -- 只要指定種子,每次執(zhí)行此語(yǔ)句得到的結(jié)果一樣的
0.7220096548596434
  • 自然指數(shù)函數(shù): exp
語(yǔ)法: exp(double a)
返回值: double
說(shuō)明: 返回自然對(duì)數(shù)e的a次方
hive> select exp(2) ;
7.38905609893065
  • 以10為底對(duì)數(shù)函數(shù): log10
語(yǔ)法: log10(double a)
返回值: double
說(shuō)明: 返回以10為底的a的對(duì)數(shù)
hive> select log10(100) ;
2.0
此外還有:以2為底對(duì)數(shù)函數(shù): log2()、對(duì)數(shù)函數(shù): log()
  • 冪運(yùn)算函數(shù): pow
語(yǔ)法: pow(double a, double p)
返回值: double
說(shuō)明: 返回a的p次冪
hive> select pow(2,4) ;
16.0
  • 開(kāi)平方函數(shù): sqrt
語(yǔ)法: sqrt(double a)
返回值: double
說(shuō)明: 返回a的平方根
hive> select sqrt(16) ;
4.0
  • 二進(jìn)制函數(shù): bin
語(yǔ)法: bin(BIGINT a)
返回值: string
說(shuō)明: 返回a的二進(jìn)制代碼表示
hive> select bin(7) ;
111
十六進(jìn)制函數(shù): hex()、將十六進(jìn)制轉(zhuǎn)化為字符串函數(shù): unhex()
進(jìn)制轉(zhuǎn)換函數(shù): conv(bigint num, int from_base, int to_base) 說(shuō)明: 將數(shù)值num從from_base進(jìn)制轉(zhuǎn)化到to_base進(jìn)制
此外還有很多數(shù)學(xué)函數(shù):絕對(duì)值函數(shù): abs()、正取余函數(shù): pmod()、正弦函數(shù): sin()、反正弦函數(shù): asin()、余弦函數(shù): cos()、反余弦函數(shù): acos()、positive函數(shù): positive()、negative函數(shù): negative()

2.6 條件函數(shù)

  • If函數(shù): if
語(yǔ)法: if(boolean testCondition, T valueTrue, T valueFalseOrNull)
返回值: T
說(shuō)明: 當(dāng)條件testCondition為T(mén)RUE時(shí),返回valueTrue;否則返回valueFalseOrNull
hive> select if(1=2,100,200) ;
200
hive> select if(1=1,100,200) ;
100
  • 非空查找函數(shù): coalesce
語(yǔ)法: coalesce(T v1, T v2, …)
返回值: T
說(shuō)明: 返回參數(shù)中的第一個(gè)非空值;如果所有值都為NULL,那么返回NULL
hive> select coalesce(null,'100','50') ;
100
  • 條件判斷函數(shù):case when (兩種寫(xiě)法,其一)
語(yǔ)法: case when a then b [when c then d]* [else e] end
返回值: T
說(shuō)明:如果a為T(mén)RUE,則返回b;如果c為T(mén)RUE,則返回d;否則返回e
hive> select case when 1=2 then 'tom' when 2=2 then 'mary' else 'tim' end from tableName;
mary
  • 條件判斷函數(shù):case when (兩種寫(xiě)法,其二)
語(yǔ)法: case a when b then c [when d then e]* [else f] end
返回值: T
說(shuō)明:如果a等于b,那么返回c;如果a等于d,那么返回e;否則返回f
hive> Select case 100 when 50 then 'tom' when 100 then 'mary' else 'tim' end from tableName;
mary

2.7 日期函數(shù)

注:以下SQL語(yǔ)句中的 from tableName 可去掉,不影響查詢(xún)結(jié)果

獲取當(dāng)前UNIX時(shí)間戳函數(shù): unix_timestamp

語(yǔ)法: unix_timestamp()
返回值: bigint
說(shuō)明: 獲得當(dāng)前時(shí)區(qū)的UNIX時(shí)間戳
hive> select unix_timestamp() from tableName;
1616906976

UNIX時(shí)間戳轉(zhuǎn)日期函數(shù): from_unixtime

語(yǔ)法: from_unixtime(bigint unixtime[, string format])
返回值: string
說(shuō)明: 轉(zhuǎn)化UNIX時(shí)間戳(從1970-01-01 00:00:00 UTC到指定時(shí)間的秒數(shù))到當(dāng)前時(shí)區(qū)的時(shí)間格式
hive> select from_unixtime(1616906976,'yyyyMMdd') from tableName;
20210328

日期轉(zhuǎn)UNIX時(shí)間戳函數(shù): unix_timestamp

語(yǔ)法: unix_timestamp(string date)
返回值: bigint
說(shuō)明: 轉(zhuǎn)換格式為"yyyy-MM-dd HH:mm:ss"的日期到UNIX時(shí)間戳。如果轉(zhuǎn)化失敗,則返回0。
hive>  select unix_timestamp('2021-03-08 14:21:15') from tableName;
1615184475

指定格式日期轉(zhuǎn)UNIX時(shí)間戳函數(shù): unix_timestamp

語(yǔ)法: unix_timestamp(string date, string pattern)
返回值: bigint
說(shuō)明: 轉(zhuǎn)換pattern格式的日期到UNIX時(shí)間戳。如果轉(zhuǎn)化失敗,則返回0。
hive>  select unix_timestamp('2021-03-08 14:21:15','yyyyMMdd HH:mm:ss') from tableName;
1615184475

日期時(shí)間轉(zhuǎn)日期函數(shù): to_date

語(yǔ)法: to_date(string timestamp)
返回值: string
說(shuō)明: 返回日期時(shí)間字段中的日期部分。
hive> select to_date('2021-03-28 14:03:01') from tableName;
2021-03-28

日期轉(zhuǎn)年函數(shù): year

語(yǔ)法: year(string date)
返回值: int
說(shuō)明: 返回日期中的年。
hive> select year('2021-03-28 10:03:01') from tableName;
2021
hive> select year('2021-03-28') from tableName;
2021

日期轉(zhuǎn)月函數(shù): month

語(yǔ)法: month (string date)
返回值: int
說(shuō)明: 返回日期中的月份。
hive> select month('2020-12-28 12:03:01') from tableName;
12
hive> select month('2021-03-08') from tableName;
8

日期轉(zhuǎn)天函數(shù): day

語(yǔ)法: day (string date)
返回值: int
說(shuō)明: 返回日期中的天。
hive> select day('2020-12-08 10:03:01') from tableName;
8
hive> select day('2020-12-24') from tableName;
24

日期轉(zhuǎn)小時(shí)函數(shù): hour

語(yǔ)法: hour (string date)
返回值: int
說(shuō)明: 返回日期中的小時(shí)。
hive> select hour('2020-12-08 10:03:01') from tableName;
10

日期轉(zhuǎn)分鐘函數(shù): minute

語(yǔ)法: minute (string date)
返回值: int
說(shuō)明: 返回日期中的分鐘。
hive> select minute('2020-12-08 10:03:01') from tableName;
3

日期轉(zhuǎn)秒函數(shù): second

語(yǔ)法: second (string date)
返回值: int
說(shuō)明: 返回日期中的秒。
hive> select second('2020-12-08 10:03:01') from tableName;
1

日期轉(zhuǎn)周函數(shù): weekofyear

語(yǔ)法: weekofyear (string date)
返回值: int
說(shuō)明: 返回日期在當(dāng)前的周數(shù)。
hive> select weekofyear('2020-12-08 10:03:01') from tableName;
49

日期比較函數(shù): datediff

語(yǔ)法: datediff(string enddate, string startdate)
返回值: int
說(shuō)明: 返回結(jié)束日期減去開(kāi)始日期的天數(shù)。
hive> select datediff('2020-12-08','2012-05-09') from tableName;
213

日期增加函數(shù): date_add

語(yǔ)法: date_add(string startdate, int days)
返回值: string
說(shuō)明: 返回開(kāi)始日期startdate增加days天后的日期。
hive> select date_add('2020-12-08',10) from tableName;
2020-12-18

日期減少函數(shù): date_sub

語(yǔ)法: date_sub (string startdate, int days)
返回值: string
說(shuō)明: 返回開(kāi)始日期startdate減少days天后的日期。
hive> select date_sub('2020-12-08',10) from tableName;
2020-11-28

日期常用函數(shù)

//昨天
select format_datetime(date_add('day',-1,current_date),'yyyyMMdd')
// 月份
 select substr(cast(current_date as varchar) , 1 ,7 )
//獲取上月月初
select date_trunc('month', (date_add('day', - day_of_month(current_date), current_date)));
上個(gè)月月末
select date_add('day', - day_of_month(current_date), current_date);
//月初
select date_trunc('month',current_date) 
//月末
SELECT date_add('day', -1, date_add('month', 1, date_trunc('month', current_date)))

2.8 字符串函數(shù)

字符串長(zhǎng)度函數(shù):length

語(yǔ)法: length(string A)
返回值: int
說(shuō)明:返回字符串A的長(zhǎng)度
hive> select length('abcedfg') from tableName;
7

字符串反轉(zhuǎn)函數(shù):reverse

語(yǔ)法: reverse(string A)
返回值: string
說(shuō)明:返回字符串A的反轉(zhuǎn)結(jié)果
hive> select reverse('abcedfg') from tableName;
gfdecba

字符串連接函數(shù):concat

語(yǔ)法: concat(string A, string B…)
返回值: string
說(shuō)明:返回輸入字符串連接后的結(jié)果,支持任意個(gè)輸入字符串
hive> select concat('abc','def’,'gh')from tableName;
abcdefgh

帶分隔符字符串連接函數(shù):concat_ws

語(yǔ)法: concat_ws(string SEP, string A, string B…)
返回值: string
說(shuō)明:返回輸入字符串連接后的結(jié)果,SEP表示各個(gè)字符串間的分隔符
hive> select concat_ws(',','abc','def','gh')from tableName;
abc,def,gh

字符串截取函數(shù):substr,substring

語(yǔ)法: substr(string A, int start),substring(string A, int start)
返回值: string
說(shuō)明:返回字符串A從start位置到結(jié)尾的字符串
hive> select substr('abcde',3) from tableName;
cde
hive> select substring('abcde',3) from tableName;
cde
hive> select substr('abcde',-1) from tableName; (和ORACLE相同)
e

字符串截取函數(shù):substr,substring

語(yǔ)法: substr(string A, int start, int len),substring(string A, int start, int len)
返回值: string
說(shuō)明:返回字符串A從start位置開(kāi)始,長(zhǎng)度為len的字符串
hive> select substr('abcde',3,2) from tableName;
cd
hive> select substring('abcde',3,2) from tableName;
cd
hive>select substring('abcde',-2,2) from tableName;
de

字符串轉(zhuǎn)大寫(xiě)函數(shù):upper,ucase

語(yǔ)法: upper(string A) ucase(string A)
返回值: string
說(shuō)明:返回字符串A的大寫(xiě)格式
hive> select upper('abSEd') from tableName;
ABSED
hive> select ucase('abSEd') from tableName;
ABSED

字符串轉(zhuǎn)小寫(xiě)函數(shù):lower,lcase

語(yǔ)法: lower(string A) lcase(string A)
返回值: string
說(shuō)明:返回字符串A的小寫(xiě)格式
hive> select lower('abSEd') from tableName;
absed
hive> select lcase('abSEd') from tableName;
absed

去空格函數(shù):trim

語(yǔ)法: trim(string A)
返回值: string
說(shuō)明:去除字符串兩邊的空格
hive> select trim(' abc ') from tableName;
abc

左邊去空格函數(shù):ltrim

語(yǔ)法: ltrim(string A)
返回值: string
說(shuō)明:去除字符串左邊的空格
hive> select ltrim(' abc ') from tableName;
abc

右邊去空格函數(shù):rtrim

語(yǔ)法: rtrim(string A)
返回值: string
說(shuō)明:去除字符串右邊的空格
hive> select rtrim(' abc ') from tableName;
abc

正則表達(dá)式替換函數(shù):regexp_replace

語(yǔ)法: regexp_replace(string A, string B, string C)
返回值: string
說(shuō)明:將字符串A中的符合java正則表達(dá)式B的部分替換為C。注意,在有些情況下要使用轉(zhuǎn)義字符,類(lèi)似oracle中的regexp_replace函數(shù)。
hive> select regexp_replace('foobar', 'oo|ar', '') from tableName;
fb

正則表達(dá)式解析函數(shù):regexp_extract

語(yǔ)法: regexp_extract(string subject, string pattern, int index)
返回值: string
說(shuō)明:將字符串subject按照pattern正則表達(dá)式的規(guī)則拆分,返回index指定的字符。
hive> select regexp_extract('foothebar', 'foo(.*?)(bar)', 1) from tableName;
the
hive> select regexp_extract('foothebar', 'foo(.*?)(bar)', 2) from tableName;
bar
hive> select regexp_extract('foothebar', 'foo(.*?)(bar)', 0) from tableName;
foothebar
strong>注意,在有些情況下要使用轉(zhuǎn)義字符,下面的等號(hào)要用雙豎線轉(zhuǎn)義,這是java正則表達(dá)式的規(guī)則。
select data_field,
regexp_extract(data_field,'.*?bgStart\\=([^&]+)',1) as aaa,
regexp_extract(data_field,'.*?contentLoaded_headStart\\=([^&]+)',1) as bbb,
regexp_extract(data_field,'.*?AppLoad2Req\\=([^&]+)',1) as ccc 
from pt_nginx_loginlog_st 
where pt = '2021-03-28' limit 2;

URL解析函數(shù):parse_url

語(yǔ)法: parse_url(string urlString, string partToExtract [, string keyToExtract])
返回值: string
說(shuō)明:返回URL中指定的部分。partToExtract的有效值為:HOST, PATH, QUERY, REF, PROTOCOL, AUTHORITY, FILE, and USERINFO.
hive> select parse_url
('https://www.tableName.com/path1/p.php?k1=v1&k2=v2#Ref1', 'HOST') 
from tableName;
www.tableName.com 
hive> select parse_url
('https://www.tableName.com/path1/p.php?k1=v1&k2=v2#Ref1', 'QUERY', 'k1')
 from tableName;
v1

json解析函數(shù):get_json_object

語(yǔ)法: get_json_object(string json_string, string path)
返回值: string
說(shuō)明:解析json的字符串json_string,返回path指定的內(nèi)容。如果輸入的json字符串無(wú)效,那么返回NULL。
hive> select  get_json_object('{"store":{"fruit":\[{"weight":8,"type":"apple"},{"weight":9,"type":"pear"}], "bicycle":{"price":19.95,"color":"red"} },"email":"amy@only_for_json_udf_test.net","owner":"amy"}','$.owner') from tableName;

空格字符串函數(shù):space

語(yǔ)法: space(int n)
返回值: string
說(shuō)明:返回長(zhǎng)度為n的字符串
hive> select space(10) from tableName;
hive> select length(space(10)) from tableName;
10

重復(fù)字符串函數(shù):repeat

語(yǔ)法: repeat(string str, int n)
返回值: string
說(shuō)明:返回重復(fù)n次后的str字符串
hive> select repeat('abc',5) from tableName;
abcabcabcabcabc

首字符ascii函數(shù):ascii

語(yǔ)法: ascii(string str)
返回值: int
說(shuō)明:返回字符串str第一個(gè)字符的ascii碼
hive> select ascii('abcde') from tableName;
97

左補(bǔ)足函數(shù):lpad

語(yǔ)法: lpad(string str, int len, string pad)
返回值: string
說(shuō)明:將str進(jìn)行用pad進(jìn)行左補(bǔ)足到len位
hive> select lpad('abc',10,'td') from tableName;
tdtdtdtabc
注意:與GP,ORACLE不同,pad 不能默認(rèn)

右補(bǔ)足函數(shù):rpad

語(yǔ)法: rpad(string str, int len, string pad)
返回值: string
說(shuō)明:將str進(jìn)行用pad進(jìn)行右補(bǔ)足到len位
hive> select rpad('abc',10,'td') from tableName;
abctdtdtdt

分割字符串函數(shù): split

語(yǔ)法: split(string str, string pat)
返回值: array
說(shuō)明: 按照pat字符串分割str,會(huì)返回分割后的字符串?dāng)?shù)組
hive> select split('abtcdtef','t') from tableName;
["ab","cd","ef"]

集合查找函數(shù): find_in_set

語(yǔ)法: find_in_set(string str, string strList)
返回值: int
說(shuō)明: 返回str在strlist第一次出現(xiàn)的位置,strlist是用逗號(hào)分割的字符串。如果沒(méi)有找該str字符,則返回0
hive> select find_in_set('ab','ef,ab,de') from tableName;
2
hive> select find_in_set('at','ef,ab,de') from tableName;
0

三、json字符串操作

3.1. 解析json對(duì)象-get_json_object

select 
get_json_object('{"name":"zhangsan","age":18}','$.name'),
get_json_object('{"name":"zhangsan","age":18}','$.age');

3.2.解析json對(duì)象-?json_tuple

說(shuō)明:解析json的字符串json_string,可指定多個(gè)json數(shù)據(jù)中的key,返回對(duì)應(yīng)的value。如果輸入的json字符串無(wú)效,那么返回NULL;json_tuple相當(dāng)于get_json_object的優(yōu)勢(shì)就是一次可以解析多個(gè)json字段。但是如果我們有個(gè)json數(shù)組,這兩個(gè)函數(shù)都無(wú)法處理

示例:

select b.name ,b.age
from tableName a lateral view
json_tuple('{"name":"zhangsan","age":18}','name','age') b as name,age;

結(jié)果:

hive sql 語(yǔ)句查詢(xún)規(guī)則,hive,sql

3.3 json數(shù)組-explode函數(shù)

如果有一個(gè)hive表,表中? json_str? 字段的內(nèi)容如下:
[ {"website":"baidu.com","name":百度"},{"website":"google.com","name":谷歌"} ]
我們想把這個(gè)字段解析出來(lái),形成如下的結(jié)構(gòu):
website name
baidu.com 百度
google.com 谷歌

說(shuō)明:explode()函數(shù)接收一個(gè)array或者map類(lèi)型的數(shù)據(jù)作為輸入,然后將array或map里面的元素按照每行的形式輸出,即將hive一列中復(fù)雜的array或者map結(jié)構(gòu)拆分成多行顯示,也被稱(chēng)為列轉(zhuǎn)行函數(shù)

示例

-- 解析array
hive> select explode(array('A','B','C'));

ok 
A
B
C

-- 解析map
hive> select explode(map('A',10,'B',20,'C','30'));

OK
A    10
B    20
C    30

3.4 json數(shù)組regexp_replace函數(shù)

語(yǔ)法: regexp_replace(string A, string B, string C)

說(shuō)明: 將字符串A中的符合 java 正則表達(dá)式的B的部分替換為C。注意:在有些情況下要使用轉(zhuǎn)義字符,類(lèi)似 oracle 中的 regexp_replace 函數(shù)。

示例:

hive> select regexp_replace('roobar', 'oo|ar', '');

ok
fb

上述示例將字符串中的  oo 或  ar  替換為 ''.

先將數(shù)據(jù)轉(zhuǎn)換成對(duì)象行

hive> SELECT explode(split(regexp_replace(regexp_replace('[{"website":"baidu.com","name":"百度"},{"website":"google.com","name":"谷歌"}]', '\\[|\\]',''),'\\}\\,\\{','\\}\\;\\{'),'\\;'));
OK
{"website":"baidu.com","name":"百度"}
{"website":"google.com","name":"谷歌"}

對(duì)上述sql進(jìn)行簡(jiǎn)要說(shuō)明:

SELECT explode(split(
    regexp_replace(
        regexp_replace(
            '[
                {"website":"baidu.com","name":"百度"},
                {"website":"google.com","name":"谷歌"}
            ]', 
            '\\[|\\]' , ''), 將json數(shù)組兩邊的中括號(hào)去掉
            
              '\\}\\,\\{' , '\\}\\;\\{'), 將json數(shù)組元素之間的逗號(hào)換成分號(hào)
                
                 '\\;') 以分號(hào)作為分隔符(split函數(shù)以分號(hào)作為分隔)
          ); 

然后提取json對(duì)象中的屬性

select json_tuple(json, 'website', 'name') 
from (
select explode(split(regexp_replace(regexp_replace('[{"website":"baidu.com","name":"百度"},{"website":"google.com","name":"谷歌"}]', '\\[|\\]',''),'\\}\\,\\{','\\}\\;\\{'),'\\;')) 
as json) t;

?執(zhí)行上述語(yǔ)句,沒(méi)有報(bào)錯(cuò),執(zhí)行結(jié)果如下:

執(zhí)行上述語(yǔ)句,沒(méi)有報(bào)錯(cuò),執(zhí)行結(jié)果如下:

www.baidu.com       百度
google.com          谷歌

3.5 使用 lateral view 解析json數(shù)組

lateral view首先為原始表的每行調(diào)用UDTF,UDTF會(huì)把一行拆分成一行或者多行,lateral view在把結(jié)果組合,產(chǎn)生一個(gè)支持別名表的虛擬表。

name id_list
zhangsan [ 1,2,3 ]
lisi [ 3,4,5 ]

對(duì)興趣id進(jìn)行解析

SELECT name, hobby_id 
FROM hobbies_table 
LATERAL VIEW explode(id_list) tmp_table AS hobby_id;

上述sql執(zhí)行結(jié)果:

name hobby_id
zhangsan 1
zhangsan 2
zhangsan 3
lisi 3
lisi 4
lisi 5

假設(shè)有一張表如下:

hive 表中 goods_id 和json_str 字段的內(nèi)容如下:
goods_id json_str
1,2,3 [? {"source":"7fresh","monthSales":4900,"userCount":1900,"score":"9.9"},{"source":"jd","monthSales":2090,"userCount":78981,"score":"9.8"},{"source":"jdmart","monthSales":6987,"userCount":1600,"score":"9.0"}? ]
select good_id,get_json_object(sale_json,'$.monthSales') as monthSales
from tableName 
LATERAL VIEW explode(split(goods_id,','))goods as good_id 
LATERAL VIEW explode(split(regexp_replace(regexp_replace(json_str , '\\[|\\]',''),'\\}\\,\\{','\\}\\;\\{'),'\\;')) sales as sale_json;

笛卡爾集合

goods_id monthSales
1 4900
1 2090
1 6987
2 4900
2 2090
2 6987
3 4900
3 2090
3 6987

四、內(nèi)置函數(shù)

4.1 NVL

給值為NULL的數(shù)據(jù)賦值,它的格式是NVL( value,default_value)。它的功能是如果value為NULL,則NVL函數(shù)返回default_value的值,否則返回value的值,如果兩個(gè)參數(shù)都為NULL ,則返回NULL

select nvl(column, 0) from xxx;

4.2 行轉(zhuǎn)列

函數(shù) 描述
CONCAT(string A/col, string B/col…) 返回輸入字符串連接后的結(jié)果,支持任意個(gè)輸入字符串
CONCAT_WS(separator, str1, str2,...) 第一個(gè)參數(shù)參數(shù)間的分隔符,如果分隔符是 NULL,返回值也將為 NULL。這個(gè)函數(shù)會(huì)跳過(guò)分隔符參數(shù)后的任何 NULL 和空字符串。分隔符將被加到被連接的字符串之間。
COLLECT_SET(col) 將某字段的值進(jìn)行去重匯總,產(chǎn)生array類(lèi)型字段
COLLECT_LIST(col) 函數(shù)只接受基本數(shù)據(jù)類(lèi)型,它的主要作用是將某字段的值進(jìn)行不去重匯總,產(chǎn)生array類(lèi)型字段。

4.3 列轉(zhuǎn)行(一列轉(zhuǎn)多行)

Split(str, separator):?將字符串按照后面的分隔符切割,轉(zhuǎn)換成字符array。

EXPLODE(col):將hive一列中復(fù)雜的array或者map結(jié)構(gòu)拆分成多行。

LATERAL VIEW

用法:

LATERAL VIEW udtf(expression) tableAlias AS columnAlias

解釋?zhuān)簂ateral view用于和split, explode等UDTF一起使用,它能夠?qū)⒁恍袛?shù)據(jù)拆成多行數(shù)據(jù),在此基礎(chǔ)上可以對(duì)拆分后的數(shù)據(jù)進(jìn)行聚合。

lateral view首先為原始表的每行調(diào)用UDTF,UDTF會(huì)把一行拆分成一或者多行,lateral view再把結(jié)果組合,產(chǎn)生一個(gè)支持別名表的虛擬表。

準(zhǔn)備數(shù)據(jù)源測(cè)試

movie category
《功勛》 記錄,劇情
《戰(zhàn)狼2》 戰(zhàn)爭(zhēng),動(dòng)作,災(zāi)難

SQL

SELECT movie,category_name 
FROM movie_info 
lateral VIEW
explode(split(category,",")) movie_info_tmp  AS category_name ;

測(cè)試結(jié)果文章來(lái)源地址http://www.zghlxwxcb.cn/news/detail-860463.html

《功勛》      記錄
《功勛》      劇情
《戰(zhàn)狼2》     戰(zhàn)爭(zhēng)
《戰(zhàn)狼2》     動(dòng)作
《戰(zhàn)狼2》     災(zāi)難

到了這里,關(guān)于hive sql 語(yǔ)句查詢(xún)規(guī)則的文章就介紹完了。如果您還想了解更多內(nèi)容,請(qǐng)?jiān)谟疑辖撬阉鱐OY模板網(wǎng)以前的文章或繼續(xù)瀏覽下面的相關(guān)文章,希望大家以后多多支持TOY模板網(wǎng)!

本文來(lái)自互聯(lián)網(wǎng)用戶(hù)投稿,該文觀點(diǎn)僅代表作者本人,不代表本站立場(chǎng)。本站僅提供信息存儲(chǔ)空間服務(wù),不擁有所有權(quán),不承擔(dān)相關(guān)法律責(zé)任。如若轉(zhuǎn)載,請(qǐng)注明出處: 如若內(nèi)容造成侵權(quán)/違法違規(guī)/事實(shí)不符,請(qǐng)點(diǎn)擊違法舉報(bào)進(jìn)行投訴反饋,一經(jīng)查實(shí),立即刪除!

領(lǐng)支付寶紅包贊助服務(wù)器費(fèi)用

相關(guān)文章

  • 【Hive-SQL】Hive Select 選擇語(yǔ)句排除一列或多列

    查看 除了sample_date 以外的所有字段信息 查看 除了sample_date 和 msgtype 以外的所有字段信息 上面的 set hive.support.quoted.identifiers=none; 可以替換操作: 在 \\\'hive-site.xml\\\' 中添加以下配置: hive.support.quoted.identifiers=none

    2024年02月10日
    瀏覽(21)
  • SQL語(yǔ)句練習(xí)——查詢(xún)

    SQL語(yǔ)句練習(xí)——查詢(xún)

    數(shù)據(jù)表如下: 1.用SQL完成以下檢索: 檢索在北京的供應(yīng)商的名稱(chēng)。 檢索發(fā)給供應(yīng)商S6的訂購(gòu)單號(hào)。 檢索出職工E6發(fā)給供應(yīng)商S6的訂購(gòu)單信息。 檢索出向供應(yīng)商S3發(fā)過(guò)訂購(gòu)單的職工的職工號(hào)和倉(cāng)庫(kù)號(hào)。 檢索出目前與S3供應(yīng)商沒(méi)有聯(lián)系的職工信息。 檢索出目前沒(méi)有任何訂購(gòu)單的

    2024年02月04日
    瀏覽(19)
  • SQL語(yǔ)句之子查詢(xún)

    SQL語(yǔ)句之子查詢(xún)

    子查詢(xún)指一個(gè)查詢(xún)語(yǔ)句嵌套在另一個(gè)查詢(xún)語(yǔ)句內(nèi)部的查詢(xún),這個(gè)特性從MySQL 4.1開(kāi)始引入。 SQL 中子查詢(xún)的使用大大增強(qiáng)了 SELECT 查詢(xún)的能力,因?yàn)楹芏鄷r(shí)候查詢(xún)需要從結(jié)果集中獲取數(shù)據(jù),或者需要從同一個(gè)表中先計(jì)算得出一個(gè)數(shù)據(jù)結(jié)果,然后與這個(gè)數(shù)據(jù)結(jié)果(可能是某個(gè)標(biāo)量,

    2023年04月27日
    瀏覽(15)
  • SQL查詢(xún)語(yǔ)句

    # 格式: select * from 表名 order by 要排序的列1 [asc/desc], 要排序的列2 [asc/desc]; # 解釋: # ? 1. 無(wú)論SQL語(yǔ)句簡(jiǎn)單或者是復(fù)雜, order by語(yǔ)句一般都放最后, 注意: 如果有l(wèi)imit(分頁(yè)), 則它(limit)在最后. # ? 2. asc表示升序, desc表示降序, 其中, 默認(rèn)是升序, 所以asc可以省略不寫(xiě). # ? 3. 如果排序的

    2024年02月05日
    瀏覽(26)
  • SQL常用(查詢(xún)?nèi)掌冢┱Z(yǔ)句

    目錄 SQL常用日期查詢(xún)語(yǔ)句及顯示格式設(shè)置 1.查詢(xún)當(dāng)前年、月、周相關(guān)時(shí)間 ?2.查詢(xún)當(dāng)前第幾季度 3.查詢(xún)當(dāng)前在一年內(nèi)是第幾周 SqlServer中的日期與時(shí)間函數(shù) ?SQL查詢(xún)前10條的方法為: 1.1.查詢(xún)當(dāng)前年份 1.2.查詢(xún)當(dāng)前年份第幾天 ?1.3.查詢(xún)當(dāng)前月份第幾天 1.4.查詢(xún)當(dāng)前周的星期幾?

    2024年02月16日
    瀏覽(17)
  • sql創(chuàng)建查詢(xún)視圖語(yǔ)句

    sql創(chuàng)建查詢(xún)視圖語(yǔ)句

    學(xué)生表 c表 s表 sc表 ? --1.查詢(xún)?nèi)w學(xué)生的學(xué)號(hào),姓名與年齡 SELECT sno,sname,age=year(getdate())-year(sbirth) FROM S; --2.查詢(xún)計(jì)算機(jī)系(CS)的學(xué)生學(xué)號(hào),姓名 SELECT sno,sname FROM S WHERE sdept=\\\'計(jì)算機(jī)\\\'; --3.查詢(xún)計(jì)算機(jī)系所有的女生姓名和年齡 SELECT sname,age=(year(getdate())-year(sbirth)) FROM S WHERE sdept

    2024年02月05日
    瀏覽(22)
  • 【SQL】MySQL的查詢(xún)語(yǔ)句

    MySQL是一種廣泛使用的關(guān)系型數(shù)據(jù)庫(kù)管理系統(tǒng),它被廣泛地應(yīng)用于各種應(yīng)用程序和網(wǎng)站。學(xué)會(huì)使用MySQL的查詢(xún)語(yǔ)句可以幫助我們更好地管理和分析數(shù)據(jù),從而更好地利用數(shù)據(jù)庫(kù)中的信息。在本篇博客中,我們將深入學(xué)習(xí)MySQL的查詢(xún)語(yǔ)句,包括 SELECT 語(yǔ)句、 WHERE 子句、 JOIN 語(yǔ)句等

    2024年02月06日
    瀏覽(50)
  • SQL server 查詢(xún)語(yǔ)句大全

    在 SQL Server 中,查詢(xún)語(yǔ)句是最常用的語(yǔ)句類(lèi)型,用于從數(shù)據(jù)庫(kù)中提取有用的信息。SQL Server 中常用的查詢(xún)語(yǔ)句有 SELECT、FROM、WHERE、GROUP BY、HAVING 和 ORDER BY。 1. SELECT SELECT 語(yǔ)句用于從表中選取數(shù)據(jù)進(jìn)行查詢(xún),語(yǔ)法如下: ```sql SELECT column1, column2, column3, ... ? FROM table_name; ``` 在這個(gè)

    2024年02月13日
    瀏覽(29)
  • [MySQL]不允許你不會(huì)SQL語(yǔ)句之查詢(xún)語(yǔ)句

    [MySQL]不允許你不會(huì)SQL語(yǔ)句之查詢(xún)語(yǔ)句

    ?? 博客主頁(yè):博主鏈接 ?? 本文由 M malloc 原創(chuàng),首發(fā)于 CSDN?? ?? 學(xué)習(xí)專(zhuān)欄推薦:LeetCode刷題集! ?? 歡迎點(diǎn)贊 ?? 收藏 ?留言 ?? 如有錯(cuò)誤敬請(qǐng)指正! ?? 未來(lái)很長(zhǎng),值得我們?nèi)Ρ几案篮玫纳? ------------------??分割線??------------------------- —————————

    2024年02月08日
    瀏覽(56)
  • 數(shù)據(jù)庫(kù) SQL高級(jí)查詢(xún)語(yǔ)句:聚合查詢(xún),多表查詢(xún),連接查詢(xún)

    數(shù)據(jù)庫(kù) SQL高級(jí)查詢(xún)語(yǔ)句:聚合查詢(xún),多表查詢(xún),連接查詢(xún)

    創(chuàng)建Students和Courses表 直接查詢(xún) 設(shè)置別名查詢(xún) 設(shè)置條件查詢(xún) 使用COUNT(*) 和 COUNT(StudentID)是一樣的效果,因?yàn)镾tudentID是主鍵,每行記錄的主鍵都不同。另外我們?cè)诰酆喜樵?xún)中還是能使用WHERE子句的,比如我們要 查找年齡大于20歲的學(xué)生數(shù)量 ,可使用以下SQL語(yǔ)句: 函數(shù) 說(shuō)明 SUM

    2024年02月09日
    瀏覽(106)

覺(jué)得文章有用就打賞一下文章作者

支付寶掃一掃打賞

博客贊助

微信掃一掃打賞

請(qǐng)作者喝杯咖啡吧~博客贊助

支付寶掃一掃領(lǐng)取紅包,優(yōu)惠每天領(lǐng)

二維碼1

領(lǐng)取紅包

二維碼2

領(lǐng)紅包