hive common string functions

1. String length function: length
Syntax: length(string A)  
Return value: int  
Description: Return the length of string A  
Example:  
hive> select length('abcedfg') 
7  

2. String reverse function: reverse
Syntax: reverse(string A)  
Return value: string  
Description: Return the reversed result of string A  
Example:  
hive> select reverse('abcedfg') ;  
gfdecba 

3. String concatenation function: concat
Syntax: concat(string A, string B…)  
Return value: string  
Description: Return the result of the concatenation of input strings, support any input string  
Example:  
hive> select concat('abc' ,'def','gh');  
abcdefgh 

4. String concatenation function with separator: concat_ws
Syntax: concat_ws(string SEP, string A, string B…)  
Return value: string  
Description: Return the result of the concatenation of the input strings, SEP represents the separator between each string  
Example :  
hive> select concat_ws('-','abc','def','gh') ;  
abc-def-gh

5. String interception function: substr, substring
Syntax: substr(string A, int start), substring(string A, int start)  
Return value: string  
Description: Return the string from the start position to the end of string A  
Example:  
hive > select substr('abcdefghijklmnopqrst',3) ;  
cdefghijklmnopqrst  
hive> select substr('abcdefghijklmnopqrst',-3) ; (same as ORACLE)  
rst 
hive> select substring('abcdefghijklmnopqrst',3) ;  
cdefghijklmnopqrst  
hive> select substring(' abcdefghijklmnopqrst',-3) ; (same as ORACLE)  
rst  

6. String interception function: substr, substring
Syntax: substr(string A, int start, int len), substring(string A, int start, int len)  
Return value: string  
Description: The returned string A starts from the start position, An example of a string of length len  
:  
hive> select substr('abcdefghijklmnopqrst',3,9);  
cdefghijk 
hive> select substring('abcdefghijklmnopqrst',3,9);    
cdefghijk  

7. String to uppercase function: upper, ucase 
Syntax: upper(string A) ucase(string A)  
Return value: string  
Description: Return the uppercase format of string A  
Example:  
hive> select upper('abSEd');  
ABSED  
hive > select ucase('abSEd');  
ABSED 

8. String to lowercase function: lower,lcase 
Syntax: lower(string A) lcase(string A)  
Return value: string  
Description: Return the lowercase format of string A  
Example:  
hive> select lower('abSEd') from dual;  
absed  
hive> select lcase('abSEd') from dual;  
absed  

9. Remove spaces function: trim 
syntax: trim(string A)  
Return value: string  
Description: Remove spaces on both sides of the string  
Example:  
hive> select trim(' abc ') from dual;  
abc   

10. Function to remove spaces on the left: ltrim
syntax: ltrim(string A)  
Return value: string  
Description: Remove spaces on the left side of the string  
Example:  
hive> select ltrim(' abc ') from dual;  
abc  

11. Function to remove spaces on the right: rtrim
syntax: rtrim(string A)  
Return value: string  
Description: Remove spaces on the right side of the string  
Example:  
hive> select rtrim(' abc ') from dual;  
abc  

12. Regular expression parsing function:
the index in regexp_extract is the position according to the regular string ( )
Syntax: regexp_extract(string subject, string pattern, int index)  
Return value: string  
Description: The string subject follows the pattern regular expression The rule split, returns the character specified by index. Note that in some cases escape characters are used  
for example:  
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  
 
13. function parse_url, parsing URL string
parse_url(url, partToExtract[, key]) - extracts a part from a URL  
parsed URL string, the options of partToExtract include [HOST,PATH,QUERY,REF,PROTOCOL,FILE,AUTHORITY,USERINFO].  
  
Example:  
String description: Parse the json string json_string and return the content specified by path. Returns NULL if the input json string is invalid.  Example:  




  


 





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  
 使用实例:
select get_json_object('{"store":{"fruit":\["aa","bb","cc"]},"owner":"amy"}','$.store.fruit[0]') from test_msg limit 1;  

15. Space string function: space
syntax: space(int n)
Return value: string  
Description: Return a string of length n  
Example:  
hive> select space(10) from dual;  
hive> select length(space(10)) from dual;  
10  

16. Repeat string function: repeat
Syntax: repeat(string str, int n)
Return value: string  
Description: Return the str string repeated n times  
Example:  
hive> select repeat('abc',5) from dual;  
abcabcabcabcabc  

17. First character ascii function: ascii
syntax: ascii(string str)
Return value: int  
Description: Return the ascii code of the first character of string str  
Example:  
hive> select ascii('abcde') from dual;  
97  

18. Left complement function: lpad
Syntax: lpad(string str, int len, string pad)
Return value: string  
Description: Left complement str to len position with pad  
Example:  
hive> select lpad('abc',10, 'td') from dual;  
tdtdtdtabc  

19. Different from GP and ORACLE, pad cannot be
right-complemented by default. Function: rpad
syntax: rpad(string str, int len, string pad)
Return value: string  
Description: Use pad to right-complement str to len.  
Example:  
hive> select rpad('abc',10,'td') from dual;  
abctdtdtdt  

20. Split string function: split
Syntax: split(string str, string pat)
Return value: array  
Description: Split str according to the pat string, and the split string array will be returned  
Example:  
hive> select split('abtcdtef', 't') from dual;  
["ab","cd","ef"]  

21. Set search function: find_in_set
Syntax: find_in_set(string str, string strList)
Return value: int  
Description: Return the position where str appears for the first time in strlist, which is a string separated by commas. If the str character is not found, it will return 0.  
Example:  
hive> select find_in_set('ab','ef,ab,de') from dual;  
2  
hive> select find_in_set('at','ef,ab,de') from dual;  
0  

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=325560158&siteId=291194637