hiveSQL 常用函數總結


  • 查詢系統自帶的函數
show functions
  • 顯示系統自帶的函數的用法
desc function count
  • 詳細顯示自帶函數的用法
desc function extended count

日期函數

1. unix時間戳類型轉日期:
      - 10位數時間戳
      select from_unixtime(1564581347,'yyyy-MM-dd HH:mm:ss') ;
      - 13位數時間戳
      select from_unixtime(cast(1564581347793/1000 as bigint),'yyyy-MM-dd HH:mm:ss') ;

2、獲取當前UNIX時間戳函數: unix_timestamp ***
語法: unix_timestamp()
返回值: bigint
說明: 獲得當前時區的UNIX時間戳

hive> select unix_timestamp() from tableName;
- 返回 1323309615


3、指定格式日期轉UNIX時間戳函數: unix_timestamp ***
語法: unix_timestamp(string date, string pattern)
返回值: bigint
說明: 轉換pattern格式的日期到UNIX時間戳。如果轉化失敗,則返回0。

hive> select unix_timestamp('20111207 13:01:03','yyyyMMdd HH:mm:ss') from tableName;

- 返回 1323234063

4、日期時間轉日期函數: to_date ***
語法: to_date(string timestamp)
返回值: string
說明: 返回日期時間字段中的日期部分。

hive> select to_date('2011-12-08 10:03:01') from tableName;

-返回 2011-12-08


2. case when 的用法

select 
case 
    when mobile='17310698783'and status in (2,4)
        then 'leon'
    when mobile='1866375062' and status in (2,4)
        then 'alix'
    else '其他'
end,
nickname
from _cloud_vipcourse.order 
WHERE  mobile  in ('17310698783','1866375062')

3.字符串相關函數

1. 字符串連接函數:concat ***
語法: concat(string A, string B…)
返回值: string
說明:返回輸入字符串連接后的結果,支持任意個輸入字符串

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

- 返回 abcdefgh


2. 帶分隔符字符串連接函數:concat_ws ***
語法: concat_ws(string SEP, string A, string B…)
返回值: string
說明:返回輸入字符串連接后的結果,SEP表示各個字符串間的分隔符

hive> select concat_ws('-','union','12345678')from tableName;

- 返回 union-12345678


3. 字符串截取函數:substr,substring ****
語法: substr(string A, int start),substring(string A, int start)
返回值: string
說明:返回字符串A從start位置到結尾的字符串

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

4. 字符串截取函數:substr,substring ****
語法: 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 tableName;

- 返回 cd

hive> select substring('abcde',3,2) from tableName;

- 返回 cd

hive>select substring('abcde',-2,2) from tableName;

- 返回 de


5. 去空格函數:trim ***
語法: trim(string A)
返回值: string
說明:去除字符串兩邊的空格

hive> select trim(' abc ') from tableName;

- 返回 abc

6.去空格函數:trim ***
語法: trim(string A)
返回值: string
說明:去除字符串兩邊的空格

hive> select trim('  abc  ') from tableName;

- 返回 abc

7. json解析函數:get_json_object ****
語法: get_json_object(string json_string, string path)
返回值: 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 tableName;

-返回 amy

8.分割字符串函數: split ****
語法: split(string str, string pat)
返回值: array
說明: 按照pat字符串分割str,會返回分割后的字符串數組

hive> select split('abtcdtef','t') from tableName;

-返回 ["ab","cd","ef"]


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM