hive 數據類型轉換、字符串函數、條件判斷

數據類型轉換

同Java語言一樣,Hive也包括 隱式轉換(implicit conversions)和顯式轉換(explicitly conversions)。
Hive在需要的時候將會對numeric類型的數據進行隱式轉換。比如我們對兩個不同數據類型的數字進行比較,假如一個數據類型是INT型,另一個 是SMALLINT類型,那麼SMALLINT類型的數據將會被隱式轉換地轉換爲INT類型,這個到底和Java中的一樣;但是我們不能隱式地將一個 INT類型的數據轉換成SMALLINT或TINYINT類型的數據,這將會返回錯誤,除非你使用了CAST操作。
任何整數類型都可以隱式地轉換成一個範圍更大的類型。TINYINT,SMALLINT,INT,BIGINT,FLOAT和STRING都可以隱式 地轉換成DOUBLE;是的你沒看出,STRING也可以隱式地轉換成DOUBLE!但是你要記住,BOOLEAN類型不能轉換爲其他任何數據類型!

下標列出了Hive內置的數據類型之間是否可以進行隱式的轉換操作:
      bl     tinyint     si     int     bigint     float     double     dm     string     vc     ts     date     ba
boolean     true     false     false     false     false     false     false     false     false     false     false     false     false
tinyint     false     true     true     true     true     true     true     true     true     true     false     false     false
smallint     false     false     true     true     true     true     true     true     true     true     false     false     false
int     false     false     false     true     true     true     true     true     true     true     false     false     false
bigint     false     false     false     false     true     true     true     true     true     true     false     false     false
float     false     false     false     false     false     true     true     true     true     true     false     false     false
double     false     false     false     false     false     false     true     true     true     true     false     false     false
decimal     false     false     false     false     false     false     false     true     true     true     false     false     false
string     false     false     false     false     false     false     true     true     true     true     false     false     false
varchar     false     false     false     false     false     false     true     true     true     true     false     false     false
ts     false     false     false     false     false     false     false     false     true     true     true     false     false
date     false     false     false     false     false     false     false     false     true     true     false     true     false
binary     false     false     false     false     false     false     false     false     false     false     false     false     true
注:由於表格比較大,這裏對一些比較長的字符串進行縮寫,ts是timestamp的縮寫,bl是boolean的縮寫,sl是smallint的縮寫,dm是decimal的縮寫,vc是varchar的縮寫,ba是binary的縮寫。

我們可以用CAST來顯式的將一個類型的數據轉換成另一個數據類型。如何使用?CAST的語法爲cast(value AS TYPE)。舉個例子:假如我們一個員工表employees,其中有name、salary等字段;salary是字符串類型的。有如下的查詢:
1     SELECT name, salary FROM employees
2     WHERE cast(salary AS FLOAT) <</code> 100000.0;

這樣salary將會顯示的轉換成float。如果salary是不能轉換成float,這時候cast將會返回NULL!
對cast有一下幾點需要說明的:
(1)、如果將浮點型的數據轉換成int類型的,內部操作是通過round()或者floor()函數來實現的,而不是通過cast實現!
(2)、對於BINARY類型的數據,只能將BINARY類型的數據轉換成STRING類型。如果你確信BINARY類型數據是一個數字類型(a number),這時候你可以利用嵌套的cast操作,比如a是一個BINARY,且它是一個數字類型,那麼你可以用下面的查詢:
1     SELECT (cast(cast(a as string) as double)) from src;

我們也可以將一個String類型的數據轉換成BINARY類型。
(3)、對於Date類型的數據,只能在Date、Timestamp以及String之間進行轉換。下表將進行詳細的說明:
有效的轉換     結果
cast(date as date)     返回date類型
cast(timestamp as date)     timestamp中的年/月/日的值是依賴與當地的時區,結果返回date類型
cast(string as date)     如果string是YYYY-MM-DD格式的,則相應的年/月/日的date類型的數據將會返回;但如果string不是YYYY-MM-DD格式的,結果則會返回NULL。
cast(date as timestamp)     基於當地的時區,生成一個對應date的年/月/日的時間戳值
cast(date as string)     date所代表的年/月/日時間將會轉換成YYYY-MM-DD的字符串。

字符串函數

字符串長度函數:length

 
Java代碼  收藏代碼

    語法: length(string A)  
    返回值: int  
    說明:返回字符串A的長度  
    舉例:  
    hive> select length(‘abcedfg’) from dual;  
    7  

 

字符串反轉函數:reverse

 
Java代碼  收藏代碼

    語法: reverse(string A)  
    返回值: string  
    說明:返回字符串A的反轉結果  
    舉例:  
    hive> select reverse(‘abcedfg’) from dual;  
    gfdecba  

字符串連接函數:concat
Java代碼  收藏代碼

    語法: concat(string A, string B…)  
    返回值: string  
    說明:返回輸入字符串連接後的結果,支持任意個輸入字符串  
    舉例:  
    hive> select concat(‘abc’,'def’,'gh’) from dual;  
    abcdefgh   

帶分隔符字符串連接函數:concat_ws

 
Java代碼  收藏代碼

    語法: concat_ws(string SEP, string A, string B…)  
    返回值: string  
    說明:返回輸入字符串連接後的結果,SEP表示各個字符串間的分隔符  
    舉例:  
    hive> select concat_ws(‘,’,'abc’,'def’,'gh’) from dual;  
    abc,def,gh  


字符串截取函數:substr,substring

 
Java代碼  收藏代碼

    語法: substr(string A, int start),substring(string A, int start)  
    返回值: string  
    說明:返回字符串A從start位置到結尾的字符串  
    舉例:  
    hive> select substr(‘abcde’,3) from dual;  
    cde  
    hive> select substring(‘abcde’,3) from dual;  
    cde  
    hive>  select substr(‘abcde’,-1) from dual;  (和ORACLE相同)  
    e  


 

字符串截取函數:substr,substring

 
Java代碼  收藏代碼

    語法: substr(string A, int start, int len),substring(string A, int start, int len)  
    返回值: string  
    說明:返回字符串A從start位置開始,長度爲len的字符串  
    舉例:  
    hive> select substr(‘abcde’,3,2) from dual;  
    cd  
    hive> select substring(‘abcde’,3,2) from dual;  
    cd  
    hive>select substring(‘abcde’,-2,2) from dual;  
    de  

 

字符串轉大寫函數:upper,ucase

 
Java代碼  收藏代碼

    語法: upper(string A) ucase(string A)  
    返回值: string  
    說明:返回字符串A的大寫格式  
    舉例:  
    hive> select upper(‘abSEd’) from dual;  
    ABSED  
    hive> select ucase(‘abSEd’) from dual;  
    ABSED  

 

字符串轉小寫函數:lower,lcase

 
Java代碼  收藏代碼

    語法: lower(string A) lcase(string A)  
    返回值: string  
    說明:返回字符串A的小寫格式  
    舉例:  
    hive> select lower(‘abSEd’) from dual;  
    absed  
    hive> select lcase(‘abSEd’) from dual;  
    absed  

 

去空格函數:trim

 
Java代碼  收藏代碼

    語法: trim(string A)  
    返回值: string  
    說明:去除字符串兩邊的空格  
    舉例:  
    hive> select trim(‘ abc ‘) from dual;  
    abc  

 

左邊去空格函數:ltrim

 
Java代碼  收藏代碼

    語法: ltrim(string A)  
    返回值: string  
    說明:去除字符串左邊的空格  
    舉例:  
    hive> select ltrim(‘ abc ‘) from dual;  
    abc  

 

右邊去空格函數:rtrim

 
Java代碼  收藏代碼

    語法: rtrim(string A)  
    返回值: string  
    說明:去除字符串右邊的空格  
    舉例:  
    hive> select rtrim(‘ abc ‘) from dual;  
    abc  

 

 

正則表達式解析函數:regexp_extract

其中的index,是按照正則字符串()的位置

 
Java代碼  收藏代碼

    語法: regexp_extract(string subject, string pattern, int index)  
    返回值: string  
    說明:將字符串subject按照pattern正則表達式的規則拆分,返回index指定的字符。注意,在有些情況下要使用轉義字符  
    舉例:  
    hive> select regexp_extract(‘foothebar’, ‘foo(.*?)(bar)’, 1) from dual;  
    the  
    hive> select regexp_extract(‘foothebar’, ‘foo(.*?)(bar)’, 2) from dual;  
    bar  
    hive> select regexp_extract(‘foothebar’, ‘foo(.*?)(bar)’, 0) from dual;  
    foothebar  

 

 

函數parse_url,解析URL字符串

 
Java代碼  收藏代碼

    parse_url(url, partToExtract[, key]) - extracts a part from a URL  
    解析URL字符串,partToExtract的選項包含[HOST,PATH,QUERY,REF,PROTOCOL,FILE,AUTHORITY,USERINFO]。  
      
    舉例:  
    * parse_url('http://facebook.com/path/p1.php?query=1', 'HOST')返回'facebook.com'   
    * parse_url('http://facebook.com/path/p1.php?query=1', 'PATH')返回'/path/p1.php'   
    * parse_url('http://facebook.com/path/p1.php?query=1', 'QUERY')返回'query=1',  
    可以指定key來返回特定參數,例如  
    * parse_url('http://facebook.com/path/p1.php?query=1', 'QUERY','query')返回'1',  
      
    * parse_url('http://facebook.com/path/p1.php?query=1#Ref', 'REF')返回'Ref'   
    * parse_url('http://facebook.com/path/p1.php?query=1#Ref', 'PROTOCOL')返回'http'  

 

json解析函數:get_json_object

語法: get_json_object(string json_string, string path)
Java代碼  收藏代碼

    返回值: string  
    說明:解析json的字符串json_string,返回path指定的內容。如果輸入的json字符串無效,那麼返回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 dual;  
    amy  

 使用實例:

 
Java代碼  收藏代碼

    select get_json_object('{"store":{"fruit":\["aa","bb","cc"]},"owner":"amy"}','$.store.fruit[0]') from test_msg limit 1;  

 

 

空格字符串函數:space

語法: space(int n)
Java代碼  收藏代碼

    返回值: string  
    說明:返回長度爲n的字符串  
    舉例:  
    hive> select space(10) from dual;  
    hive> select length(space(10)) from dual;  
    10  

 

 

重複字符串函數:repeat

語法: repeat(string str, int n)
Java代碼  收藏代碼

    返回值: string  
    說明:返回重複n次後的str字符串  
    舉例:  
    hive> select repeat(‘abc’,5) from dual;  
    abcabcabcabcabc  

 

 

首字符ascii函數:ascii

語法: ascii(string str)
Java代碼  收藏代碼

    返回值: int  
    說明:返回字符串str第一個字符的ascii碼  
    舉例:  
    hive> select ascii(‘abcde’) from dual;  
    97  

 

 

左補足函數:lpad

語法: lpad(string str, int len, string pad)
Java代碼  收藏代碼

    返回值: string  
    說明:將str進行用pad進行左補足到len位  
    舉例:  
    hive> select lpad(‘abc’,10,’td’) from dual;  
    tdtdtdtabc  

 

 

與GP,Oracle不同,pad 不能默認

右補足函數:rpad

語法: rpad(string str, int len, string pad)
Java代碼  收藏代碼

    返回值: string  
    說明:將str進行用pad進行右補足到len位  
    舉例:  
    hive> select rpad(‘abc’,10,’td’) from dual;  
    abctdtdtdt  

 

 

分割字符串函數: split

語法:  split(string str, string pat)
Java代碼  收藏代碼

    返回值:  array  
    說明: 按照pat字符串分割str,會返回分割後的字符串數組  
    舉例:  
    hive> select split(‘abtcdtef’,'t’) from dual;  
    ["ab","cd","ef"]  

 

 

集合查找函數: find_in_set

語法: find_in_set(string str, string strList)
Java代碼  收藏代碼

    返回值: int  
    說明: 返回str在strlist第一次出現的位置,strlist是用逗號分割的字符串。如果沒有找該str字符,則返回0  
    舉例:  
    hive> select find_in_set(‘ab’,'ef,ab,de’) from dual;  
    2  
    hive> select find_in_set(‘at’,'ef,ab,de’) from dual;  
    0  

 

條件判斷

CONDITIONAL FUNCTIONS IN HIVE
Hive supports three types of conditional functions. These functions are listed below:

IF( Test Condition, True Value, False Value )
The IF condition evaluates the “Test Condition” and if the “Test Condition” is true, then it returns the “True Value”. Otherwise, it returns the False Value.
Example: IF(1=1, 'working', 'not working') returns 'working'

COALESCE( value1,value2,... )

The COALESCE function returns the fist not NULL value from the list of values. If all the values in the list are NULL, then it returns NULL.
Example: COALESCE(NULL,NULL,5,NULL,4) returns 5

CASE Statement

The syntax for the case statement is:

    CASE  [ expression ]
      WHEN condition1 THEN result1
      WHEN condition2 THEN result2
      ...
      WHEN conditionn THEN resultn
      ELSE result
    END

Here expression is optional. It is the value that you are comparing to the list of conditions. (ie: condition1, condition2, ... conditionn).

All the conditions must be of same datatype. Conditions are evaluated in the order listed. Once a condition is found to be true, the case statement will return the result and not evaluate the conditions any further.

All the results must be of same datatype. This is the value returned once a condition is found to be true.

IF no condition is found to be true, then the case statement will return the value in the ELSE clause. If the ELSE clause is omitted and no condition is found to be true, then the case statement will return NULL

Example:

    CASE Fruit
      WHEN 'APPLE' THEN 'The owner is APPLE'
      WHEN 'ORANGE' THEN 'The owner is ORANGE'
      ELSE 'It is another Fruit'
    END

The other form of CASE is

    CASE
      WHEN Fruit = 'APPLE' THEN 'The owner is APPLE'
      WHEN Fruit = 'ORANGE' THEN 'The owner is ORANGE'
      ELSE 'It is another Fruit'
    END

 

 
————————————————
版權聲明:本文爲CSDN博主「默一鳴」的原創文章,遵循 CC 4.0 BY-SA 版權協議,轉載請附上原文出處鏈接及本聲明。
原文鏈接:https://blog.csdn.net/yimingsilence/article/details/70057638

發佈了37 篇原創文章 · 獲贊 15 · 訪問量 7萬+
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章