1. 程式人生 > >Hive常見內建函式及其使用

Hive常見內建函式及其使用

函式分類

HIVE CLI命令

顯示當前會話有多少函式可用 

SHOW FUNCTIONS;

顯示函式的描述資訊 

DESC FUNCTION concat;

顯示函式的擴充套件描述資訊 

DESC FUNCTION EXTENDED concat;

簡單函式

函式的計算粒度為單條記錄。 

關係運算 

數學運算 

邏輯運算 

數值計算 

型別轉換 

日期函式 

條件函式 

字串函式 

統計函式

聚合函式

函式處理的資料粒度為多條記錄。 

sum()—求和 

count()—求資料量 

avg()—求平均直 

distinct—求不同值數 

min—求最小值 

max—求最人值

集合函式

複合型別構建 

複雜型別訪問 

複雜型別長度

特殊函式

視窗函式

應用場景 

用於分割槽排序 

動態Group By 

Top N 

累計計算 

層次查詢

Windowing functions

lead

lag

FIRST_VALUE

LAST_VALUE

  • 1
  • 2
  • 3
  • 4

分析函式

Analytics functions

RANK

ROW_NUMBER

DENSE_RANK

CUME_DIST

PERCENT_RANK

NTILE

  • 1
  • 2
  • 3
  • 4
  • 5
  • 6

混合函式

java_method(class,method [,arg1 [,arg2])

reflect(class,method [,arg1 [,arg2..]])

hash(a1 [,a2...])

  • 1
  • 2
  • 3

UDTF

lateralView: LATERAL VIEW udtf(expression) tableAlias AS columnAlias (',' columnAlias)*  

fromClause: FROM baseTable (lateralView)*  

  • 1
  • 2

ateral view用於和split, explode等UDTF一起使用,它能夠將一行資料拆成多行資料,在此基礎上可以對拆分後的資料進行聚合。lateral view首先為原始表的每行呼叫UDTF,UTDF會把一行拆分成一或者多行,lateral view再把結果組合,產生一個支援別名表的虛擬表。

常用函式Demo:

create table employee(

id string,

money double,

type string

)

row format delimited 

fields terminated by '\t' 

lines terminated by '\n' 

stored as textfile;

load data local inpath '/liguodong/hive/data' into table employee;

select * from employee;

優先順序依次為NOT AND OR

select id,money from employee where (id='1001' or id='1002') and money='100';

  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15

cast型別轉換

select cast(1.5 as int);

  • 1

if判斷

if(con,'','');

hive (default)> select if(2>1,'YES','NO');

YES

  • 1
  • 2
  • 3
  • 4

case when con then '' when con then '' else  '' end (''裡面型別要一樣)

select case when id='1001' then 'v1001' when id='1002' then 'v1002' else 'v1003' end from employee;

  • 1
  • 2
  • 3

get_json_object

get_json_object(json 解析函式,用來處理json,必須是json格式)

select get_json_object('{"name":"jack","age":"20"}','$.name');

  • 1
  • 2

URL解析函式

parse_url(string urlString, string partToExtract [, string keyToExtract])

select parse_url('http://facebook.com/path1/p.php?k1=v1&k2=v2#Ref1', 'HOST') from

employee limit 1;

  • 1
  • 2
  • 3
  • 4

字串連線函式: concat 

語法: concat(string A, string B…) 

返回值: string 

說明:返回輸入字串連線後的結果,支援任意個輸入字串 

舉例:

hive> select concat('abc','def’,'gh') from lxw_dual;

abcdefgh

  • 1
  • 2

帶分隔符字串連線函式: concat_ws 

語法: concat_ws(string SEP, string A, string B…) 

返回值: string 

說明:返回輸入字串連線後的結果, SEP 表示各個字串間的分隔符

concat_ws(string SEP, array<string>)

舉例:

hive> select concat_ws(',','abc','def','gh') from lxw_dual;

abc,def,gh

  • 1
  • 2
  • 3
  • 4
  • 5

列出該欄位所有不重複的值,相當於去重

collect_set(id)  //返回的是陣列

列出該欄位所有的值,列出來不去重 

collect_list(id)   //返回的是陣列

select collect_set(id) from taborder;

  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7

求和

sum(money)

統計列數

count(*)

select sum(num),count(*) from taborder;

  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7

視窗函式

first_value(第一行值)

first_value(money) over (partition by id order by money)

select ch,num,first_value(num) over (partition by ch order by num) from taborder;

  • 1
  • 2
  • 3
  • 4
  • 5

rows between 1 preceding and 1 following (當前行以及當前行的前一行與後一行)

hive (liguodong)> select ch,num,first_value(num) over (partition by ch order by num ROWS BETWEEN 2 PRECEDING AND CURRENT ROW) from taborder;

  • 1
  • 2
  • 3
  • 4

last_value 最後一行值

hive (liguodong)> select ch,num,last_value(num) over (partition by ch) from taborder;

  • 1
  • 2
  • 3

 lead

 去當前行後面的第二行的值

 lead(money,2) over (order by money)

 lag 

 去當前行前面的第二行的值

 lag(money,2) over (order by money)

 ```

 ```

 select ch, num, lead(num,2) over (order by num) from taborder;

  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11

 select ch, num, lag(num,2) over (order by num) from taborder;

  • 1

rank排名

rank() over(partition by id order by money)

  • 1

select ch, num, rank() over(partition by ch order by num) as rank from taborder;

  • 1

select ch, num, dense_rank() over(partition by ch order by num) as dense_rank from taborder;

  • 1

cume_dist

cume_dist (相同值的最大行號/行數)

cume_dist() over (partition by id order by money)

percent_rank (相同值的最小行號-1)/(行數-1)

第一個總是從0開始

percent_rank() over (partition by id order by money)

 select ch,num,cume_dist() over (partition by ch order by num) as cume_dist,

 percent_rank() over (partition by ch order by num) as percent_rank

 from taborder;

  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12

 ntile分片 

 ntile(2) over (order by money desc)  分兩份 

 select ch,num,ntile(2) over (order by num desc) from taborder;

  • 1
  • 2
  • 3
  • 4

混合函式

select id,java_method("java.lang,Math","sqrt",cast(id as double)) as sqrt from hiveTest;

  • 1

UDTF

 select id,adid 

 from employee 

 lateral view explode(split(type,'B')) tt as adid;

 explode 把一列轉成多行

hive (liguodong)>  select id,adid

                >  from hiveDemo

                >  lateral view explode(split(str,',')) tt as adid;

  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8

正則表示式 

使用正則表示式的函式 

regexp_replace(string subject A,string B,string C) 

regexp_extract(string subject,string pattern,int index)

hive> select regexp_replace('foobar', 'oo|ar', '') from lxw_dual;

fb

hive> select regexp_replace('979|7.10.80|8684', '.*\\|(.*)',1) from hiveDemo limit 1;

  • 1
  • 2
  • 3
  • 4

hive> select regexp_replace('979|7.10.80|8684', '(.*?)\\|(.*)',1) from hiveDemo limit 1;

  • 1

相關推薦

Hive常見函式及其使用

函式分類 HIVE CLI命令 顯示當前會話有多少函式可用  SHOW FUNCTIONS; 顯示函式的描述資訊  DESC FUNCTION concat; 顯示函式的擴充套件描述資訊  DESC FUNCTION EXTENDED conca

Python學習總結(四)——常見函式(Built-in Functions)

 國慶期間下定決心打算學習Python,於是下載安裝了開發環境。然後問題就來了,怎麼開始呢?糾結一番,還是從官方幫助文件開始吧。可是全是英文啊,英語渣怎麼破?那就邊翻譯邊看邊實踐著做吧(順便吐槽下百度翻譯,同樣的語句百度翻譯出來的結果和谷歌翻譯出來的結果差的不是一丟丟)。

[Hive_6] Hive函式應用

0. 說明            5. explode   5.1 描述   separates the elements of array a into multiple rows   or the elemen

hive---常用函式總結

數學函式:round(四捨五入):ceil(向上取整):floor(向下取整):上面的結果變為45字元函式:lower(轉小寫):upper(轉大寫):length(字串長度,字元數):concat(字串拼接):substr(求子串):substr(a,b):從字串a中,第b

python常見函式

最常見的內建函式print("Hello World!")數學運算abs(-5)                         # 取絕對值,也就是5round(2.6)                       # 四捨五入取整,也就是3.0pow(2, 3)    

hive函式

在Hive中,函式包括以下型別: 一、內建函式 1、數學函式 (1)round:四捨五入            select round(數值,小數點位數);   (2)ceil:向上取

【十七】hive常用函式之String Functions

String Functions Return Type Name(Signature) Description Return

Hive 運算子 & 函式詳解 -- 適合關鍵詞查詢

1.內建運算子 1.1關係運算符 運算子 型別 說明 A = B 所有原始型別 如果A與B相等,返回TRUE,否則返回FALSE A == B 無 失敗,因為無效的語法。 SQL使用”=”,不使用”==”。 A <> B 所有原始型別

HIVE 常見函式

開發十年,就只剩下這套架構體系了! >>>   

hive 函式

在Hive中,函式包括以下型別: 一、內建函式 1、數學函式 [plain] view plain copy print? (1)round:四捨五入 select round(數值,小數點位數); (2)ceil:向上取整 sel

大資料入門教程系列之Hive函式及自定義函式

本篇文章主要介紹Hive內建函式以及自定義UDF函式和UDFT函式,自定義UDF函式通過一個國際轉換中文的例子說明。 操作步驟: ①、準備資料和環境 ②、演示Hive內建函式 ③、自定義UDF函式編寫、演示   詳細步驟: 一、準備資料和

Hive基本操作,DDL操作(建立表,修改表,顯示命令),DML操作(Load Insert Select),Hive Join,Hive Shell引數(運算子、函式)等

1.  Hive基本操作 1.1  DDL操作1.1.1    建立表 建表語法 CREATE [EXTERNAL] TABLE [IF NOT EXISTS] table_name    

大資料系列之hive(八、hive函式全解)

1.內建運算子1.1關係運算符 運算子 型別 說明 A = B 所有原始型別 如果A

函式 匿名函式 sorted map filter等常見函式

內建函式 什麼是內建函式? 就是python給你提供的. 拿來直接用的函式, 比如print., input等等. 截⽌止 到python版本3.6.2 python⼀共提供了了68個內建函式. 他們就是python直接提供給我們的. 有 ⼀些我們已經⽤用過了了. 有一些還沒有⽤用過. 還有⼀些需要學完了了⾯

hive函式

• 正則表示式解析函式: regexp_extract 語法: regexp_extract(string subject, string pattern, int index) 返回值: string 說明:將字串subject按照pattern正則表示式的規則拆分,返回index

Hive 函式及自定義函式

1.內建函式 使用如下命令檢視當前hive版本支援的所有內建函式 show functions; 部分截圖: 可以使用如下命令檢視某個函式的使用方法及作用,比如檢視 upper函式 desc function upper; 如果想要檢

Hive函式(測試函式小技巧)

內容較多,見《Hive官方文件》   主要是字串操作函式測試各種內建函式的快捷方法:1、建立一個dual表create table dual(id string);2、load一個檔案(一行,一個空格)到dual表3、select substr('angelababy',2,

Spark SQL 支援的Hive函式

數學函式 round bround floor ceil rand exp log pow sqrt bin hex unhex abs pmod sin asin cos acos tan ata

Hive 函式和UDF函式

1)內建函式    hive> show functions;    // 顯示所有函式,比官網詳細    hive> desc function substr;    // 檢視函式的詳細資訊    hive> desc function extended

hive函式和自定義函式的使用

1.hive函式的分類 內建函式和自定義函式 1.1、內建函式 1、查詢有哪些內建函式: show functions;2、查詢某個內建函式怎麼使用desc function extended concat;1.2、自定義函式 分三大類:1、UDF : user defin