Hive Built-in Functions
Built-in Functions
透過Hive command列出Built-in Functions
- 顯示出當前session所支持的Built-in Functions
123456789101112131415161718192021222324252627282930313233hive> show functions;OK!!=%&*+-/<<=<=><>===>>=^absacosadd_monthsandarrayarray_containsasciiasinassert_trueatanavg-- ...Time taken: 0.471 seconds, Fetched: 210 row(s)
查看特定Built-in function說明。
- function名稱可以是show functions所顯示出來的任一個。1234567891011121314hive> desc function to_unix_timestamp;OKto_unix_timestamp(date[, pattern]) - Returns the UNIX timestampTime taken: 0.084 seconds, Fetched: 1 row(s)hive> desc function !;OK! a - Logical notTime taken: 0.05 seconds, Fetched: 1 row(s)hive> desc function substr;OKsubstr(str, pos[, len]) - returns the substring of str that starts at pos and is of length len orsubstr(bin, pos[, len]) - returns the slice of byte array that starts at pos and is of length lenTime taken: 0.026 seconds, Fetched: 1 row(s)
顯示更詳細的Built-in function說明
- 包含註釋與使用範例。
- Synonyms關鍵字顯示,相同作用的function。12345678910111213hive> desc function extended substr;OKsubstr(str, pos[, len]) - returns the substring of str that starts at pos and is of length len orsubstr(bin, pos[, len]) - returns the slice of byte array that starts at pos and is of length lenSynonyms: substringpos is a 1-based index. If pos<0 the starting position is determined by counting backwards from the end of str.Example:> SELECT substr('Facebook', 5) FROM src LIMIT 1;'book'> SELECT substr('Facebook', -5) FROM src LIMIT 1;'ebook'> SELECT substr('Facebook', 5, 1) FROM src LIMIT 1;'b'Time taken: 0.028 seconds, Fetched: 10 row(s)
使用Built-in function
- 使用des function extented命令,可以得知此function如何使用,下述使用substr作為示例。1234567891011-- 列出紀錄hive> select ename from emp limit 1;OKSMITHTime taken: 0.239 seconds, Fetched: 1 row(s)-- 使用substr處理紀錄hive> select substr(ename, 3) from emp limit 1;OKITHTime taken: 0.119 seconds, Fetched: 1 row(s)
Date Built-in Functions
取得當前時間
|
|
取的兩個時間的天數差異
|
|
Type Conversion Functions
使用Type Conversion Functions
|
|
Type Conversion Failure
- 當轉換失敗時,Type Conversion Function將會回傳NUll123456789101112131415161718hive> select ename, cast(ename as int) from emp;OKSMITH NULLALLEN NULLWARD NULLJONES NULLMARTIN NULLBLAKE NULLCLARK NULLSCOTT NULLKING NULLTURNER NULLADAMS NULLJAMES NULLFORD NULLMILLER NULLHIVE NULLTime taken: 0.088 seconds, Fetched: 15 row(s)
Binary Type Conversion
- 若Binary要轉成Int類型,須先轉換成String類型,才能換成Int類型。
常用built-in functions補充
isnull and isnotnull
- isnull: 若紀錄為null,則回傳true。
- isnotnull: 與isnull相反。若紀錄不為null,則回傳true。123456789101112131415161718hive> select comm, isnull(comm), isnotnull(comm) from emp;OKNULL true false300.0 false true500.0 false trueNULL true false1400.0 false trueNULL true falseNULL true falseNULL true falseNULL true false0.0 false trueNULL true falseNULL true falseNULL true falseNULL true falseNULL true falseTime taken: 0.079 seconds, Fetched: 15 row(s)
assert_true
- 判斷指定欄位內的紀錄中,若有任何一個紀錄不符合,則拋出異常。1234567891011121314151617181920212223hive> select assert_true(sal > 700) from emp;OKNULLNULLNULLNULLNULLNULLNULLNULLNULLNULLNULLNULLNULLNULLNULLTime taken: 0.096 seconds, Fetched: 15 row(s)hive> select assert_true(sal > 800) from emp;OKFailed with exception java.io.IOException:org.apache.hadoop.hive.ql.metadata.HiveException: ASSERT_TRUE(): assertion failed.Time taken: 0.07 seconds