Impala字符串函數(shù)
Impala中字符串函數(shù)主要應(yīng)用于 varchar习蓬、char担神、string類型验靡,如果把varchar或者char類型的值傳遞給字符串函數(shù)弯洗,返回將是一個(gè)string類型的值
函數(shù)列表
base64encode(string str)
base64decode(string str)
加密和解密,返回值為4字節(jié)的倍數(shù)能真,可以用來存儲(chǔ)特殊字符串
--將hello world加密
[master:21000] > select base64encode('hello world') as encoded;
+------------------+
| encoded |
+------------------+
| aGVsbG8gd29ybGQ= |
+------------------+
--將加密后的密文解密
[master:21000] > select base64decode('aGVsbG8gd29ybGQ=') as decoded;
+-------------+
| decoded |
+-------------+
| hello world |
+-------------+
ascii(string str)
返回參數(shù)字符串的第一個(gè)字符的ascii碼
--得到字符a的ascii碼
[master:21000] > select ascii('a') as ascii;
+-------+
| ascii |
+-------+
| 97 |
+-------+
--驗(yàn)證是否只能返回第一個(gè)字符
[master:21000] > select ascii('abc') as ascii;
+-------+
| ascii |
+-------+
| 97 |
+-------+
chr(int character_code)
返回?cái)?shù)值ascii碼對應(yīng)的字符
--得到數(shù)值97對應(yīng)的字符
[master:21000] > select chr(97) as chr;
+-----+
| chr |
+-----+
| a |
+-----+
btrim(string a)
去除字符串之前和之后的任意個(gè)數(shù)的空格
--去除hello前的空格
[master:21000] > select btrim(' hello ') as btrim;
+-------+
| btrim |
+-------+
| hello |
+-------+
btrim(string a,string chars_to_trim)
去除第一個(gè)字符串之前和之后的任何包含在第二個(gè)字符串中出現(xiàn)任意次數(shù)的字符(真的難理解QAQ)
--去除xyz并驗(yàn)證是否去除空格
[master:21000] > select btrim('xy hello zyzzxx','xyz') as btrim;
+------------+
| btrim |
+------------+
| hello |
+------------+
--驗(yàn)證是否會(huì)去除其他字符中間的應(yīng)去除字符
[master:21000] > select btrim('xyhelxyzlozyzzxx','xyz') as btrim;
+----------+
| btrim |
+----------+
| helxyzlo |
+----------+
char_length(string a)
character_length(string a)
返回字符串的長度赁严,兩個(gè)函數(shù)功能相同
--char_length得到hello world的長度
[master:21000] > select char_length('hello world') as char_length;
+-------------+
| char_length |
+-------------+
| 11 |
+-------------+
--通過函數(shù)character_length得到hello world的長度
[master:21000] > select character_length('hello world') as character_length;
+------------------+
| character_length |
+------------------+
| 11 |
+------------------+
concat(string a,string b...)
拼接多個(gè)字符串
--連接hello和world兩個(gè)字符串
[master:21000] > select concat('hello','world') as concat;
+------------+
| concat |
+------------+
| helloworld |
+------------+
--連接hello、world粉铐、cauchy三個(gè)字符串
[master:21000] > select concat('hello','world','cauchy') as concat;
+------------------+
| concat |
+------------------+
| helloworldcauchy |
+------------------+
concat_ws(string sep,string a,string b...)
拼接多個(gè)字符串疼约,由指定分隔符分割
--通過'-'連接兩個(gè)字符串
[master:21000] > select concat_ws('-','hello','world') as concat_ws;
+-------------+
| concat_ws |
+-------------+
| hello-world |
+-------------+
find_in_set(string str,string strList)
查找某個(gè)字符串在一個(gè)以逗號(hào)為分隔符的列表中第一次出現(xiàn)的位置(以1為起點(diǎn)),如果查詢不到或查詢字符串中出現(xiàn)'蝙泼,'(逗號(hào))程剥,返回則為0
--在以逗號(hào)間隔的abcdefg中字符c第一次出現(xiàn)的位置
[master:21000] > select find_in_set('c','a,b,c,d,e,f,g') as find_in_set;
+-------------+
| find_in_set |
+-------------+
| 3 |
+-------------+
--在查詢','的位置時(shí)的返回值
[master:21000] > select find_in_set(',','a,b,c,d,e,f,g') as find_in_set;
+-------------+
| find_in_set |
+-------------+
| 0 |
+-------------+
--在查詢不存在字符的位置時(shí)的返回值
[master:21000] > select find_in_set('h','a,b,c,d,e,f,g') as find_in_set;
+-------------+
| find_in_set |
+-------------+
| 0 |
+-------------+
initcap(string str)
將字符串首字符大寫并返回
--將'abc'首字母大寫
[master:21000] > select initcap('abc') as initcap;
+---------+
| initcap |
+---------+
| Abc |
+---------+
instr(string str,string substr)
返回較長字符串中第一次出現(xiàn)子字符串的位置(從1開始)
--在字符串'abcdefg'中查找'bcd'第一次出現(xiàn)的位置
[master:21000] > select instr('abcdefg','bcd') as instr;
+-------+
| instr |
+-------+
| 2 |
+-------+
length(string a)
返回參數(shù)字符串的字符長度
--得到字符串'abcdefg'的長度
[master:21000] > select length('abcdefg') as length;
+--------+
| length |
+--------+
| 7 |
+--------+
locate(string substr,string str,[int pos])
返回字符串中第一次出現(xiàn)子字符串的位置(從1開始)汤踏,可指定位置
--返回長字符串中'bc'第一次出現(xiàn)的位置
[master:21000] > select locate('bc','abcdefgabc') as locate;
+--------+
| locate |
+--------+
| 2 |
+--------+
--返回長字符串中'bc'從第三位之后第一次出現(xiàn)的位置
[master:21000] > select locate('bc','abcdefgabc',3) as locate;
+--------+
| locate |
+--------+
| 9 |
+--------+
lower(string a)
lcase(string a)
返回全部為小寫字符的字符串
--使用lower返回全小寫的hello world
[master:21000] > select lower('Hello World') as lower;
+-------------+
| lower |
+-------------+
| hello world |
+-------------+
--使用lcase返回全小寫的hello world
[master:21000] > select lcase('Hello World') as lcase;
+-------------+
| lcase |
+-------------+
| hello world |
upper(string a)
ucase(string a)
返回全部為大寫字符的字符串
--使用upper返回全小寫的hello world
[master:21000] > select upper('hello world') as upper;
+-------------+
| upper |
+-------------+
| HELLO WORLD |
+-------------+
--使用ucase返回全小寫的hello world
[master:21000] > select ucase('hello world') as ucase;
+-------------+
| ucase |
+-------------+
| HELLO WORLD |
+-------------+
lpad(string str,int len,string pad)
返回更改了長度的第一個(gè)字符串织鲸,如果小于長度,則用pad字符串在左邊補(bǔ)齊溪胶,如果大于長度搂擦,則從左邊截取對應(yīng)長度字符串返回
--從左邊截取長度為7的'hello world'
[master:21000] > select lpad('hello world',7,'/') as lpad;
+---------+
| lpad |
+---------+
| hello w |
+---------+
--從左邊截取長度為13的'hello world',長度不足在左側(cè)用'/'補(bǔ)齊
[master:21000] > select lpad('hello world',13,'/') as lpad;
+---------------+
| lpad |
+---------------+
| //hello world |
+---------------+
rpad(string str,int len,string pad)
返回更改了長度的第一個(gè)字符串,如果小于長度哗脖,則用pad字符串在右邊補(bǔ)齊瀑踢,如果大于長度,則從左邊截取對應(yīng)長度字符串返回
--從左邊截取長度為7的'hello world'
[master:21000] > select rpad('hello world',7,'/') as rpad;
+---------+
| rpad |
+---------+
| hello w |
+---------+
--從左邊截取長度為13的'hello world',長度不足在右側(cè)用'/'補(bǔ)齊
[master:21000] > select rpad('hello world',13,'/') as rpad;
+---------------+
| rpad |
+---------------+
| hello world// |
+---------------+
ltrim(string a)
返回參數(shù)字符串懒熙,并從左側(cè)刪除任何前導(dǎo)空格
--刪除字符串' hello '左側(cè)的所有空格
[master:21000] > select ltrim(' hello ') as ltrim;
+---------+
| ltrim |
+---------+
| hello |
+---------+
rtrim(string a)
返回參數(shù)字符串丘损,并從右側(cè)刪除任何后置空格
--刪除字符串' hello '右側(cè)的所有空格
[master:21000] > select rtrim(' hello ') as rtrim;
+---------+
| rtrim |
+---------+
| hello |
+---------+
trim(string a)
去掉字符串中所有前導(dǎo)和后置空格
--去掉' hello world '的前導(dǎo)和后置空格
[master:21000] > select trim(' hello world ') as trim;
+-------------+
| trim |
+-------------+
| hello world |
+-------------+
regexp_extract(string subject,string pattern,int index)
返回通過正則表達(dá)式提取的字符串,
impala使用\字符進(jìn)行轉(zhuǎn)義工扎,所以\d需要\d徘钥,也可以采用[[:digit:]]
--匹配任意字符以數(shù)字結(jié)尾,返回匹配的整個(gè)字符串
[master:21000] > select regexp_extract('abcdef123ghi456jkl','.*?(\\d+)',0);
+------------------------------------------------------+
| regexp_extract('abcdef123ghi456jkl', '.*?(\\d+)', 0) |
+------------------------------------------------------+
| abcdef123ghi456 |
+------------------------------------------------------+
--匹配任意字符以數(shù)字結(jié)尾肢娘,只返回匹配的第一個(gè)值
[master:21000] > select regexp_extract('abcdef123ghi456jkl','.*?(\\d+)',1);
+------------------------------------------------------+
| regexp_extract('abcdef123ghi456jkl', '.*?(\\d+)', 1) |
+------------------------------------------------------+
| 456 |
+------------------------------------------------------+
--匹配任意字符以小寫字母結(jié)尾呈础,返回匹配的整個(gè)字符串
[master:21000] > select regexp_extract('AbcdBCdefGHI','.*?([[:lower:]]+)',0);
+--------------------------------------------------------+
| regexp_extract('abcdbcdefghi', '.*?([[:lower:]]+)', 0) |
+--------------------------------------------------------+
| AbcdBCdef |
+--------------------------------------------------------+
--匹配任意字符以小寫字母結(jié)尾,只返回匹配的第一個(gè)值
[master:21000] > select regexp_extract('AbcdBCdefGHI','.*?([[:lower:]]+)',1);
+--------------------------------------------------------+
| regexp_extract('abcdbcdefghi', '.*?([[:lower:]]+)', 1) |
+--------------------------------------------------------+
| def |
+--------------------------------------------------------+
regexp_like(string source,string pattern,[string options])
返回true或者false橱健,表示字符串是否包含正則表達(dá)式的值
options參數(shù):
- c: 區(qū)分大小寫匹配(默認(rèn))
- i:不區(qū)分大小寫
- m:多行匹配
- n:換行符匹配
--判斷字符'foo'是否包含'f'
[master:21000] > select regexp_like('foo','f');
+-------------------------+
| regexp_like('foo', 'f') |
+-------------------------+
| true |
+-------------------------+
--判斷字符'foo'是否包含'F'
[master:21000] > select regexp_like('foo','F');
+-------------------------+
| regexp_like('foo', 'f') |
+-------------------------+
| false |
+-------------------------+
--判斷字符'foo'是否包含'f',設(shè)置參數(shù)不區(qū)分大小寫
[master:21000] > select regexp_like('foo','F','i');
+------------------------------+
| regexp_like('foo', 'f', 'i') |
+------------------------------+
| true |
+------------------------------+
regexp_replace(string initial,string pattern,string replacement)
替換字符串與正則表達(dá)式匹配項(xiàng)為新字符串并返回
--將字符串中任意的字符'b'替換為'xyz'
[master:21000] > select regexp_replace('aaabbbaaa','b+','xyz');
+------------------------------------------+
| regexp_replace('aaabbbaaa', 'b+', 'xyz') |
+------------------------------------------+
| aaaxyzaaa |
+------------------------------------------+
--將字符串中任意的非數(shù)字字符替換為''(空)
[master:21000] > select regexp_replace('123-456-789','[^[:digit:]]','');
+---------------------------------------------------+
| regexp_replace('123-456-789', '[^[:digit:]]', '') |
+---------------------------------------------------+
| 123456789 |
+---------------------------------------------------+
repeat(string str,int n)
返回指定重復(fù)次數(shù)的字符串
--將'hello'重復(fù)5次
[master:21000] > select repeat('hello',5) as repeat;
+---------------------------+
| repeat |
+---------------------------+
| hellohellohellohellohello |
+---------------------------+
reverse(string a)
返回反轉(zhuǎn)字符串
--反轉(zhuǎn)字符串'hello world'
[master:21000] > select reverse('hello world') as reverse;
+-------------+
| reverse |
+-------------+
| dlrow olleh |
+-------------+
space(int n)
返回指定數(shù)量的空格的連接字符串
--返回5個(gè)連續(xù)空格的字符串
[master:21000] > select space(5) as space;
+-------+
| space |
+-------+
| |
+-------+
split_part(string source,string delimiter,bigint n)
以delimiter字符串作為拆分項(xiàng)而钞,取第n個(gè)字符串返回
--以','為分隔符拆分'x,y,z'并返回第1個(gè)字符串
[master:21000] > select split_part('x,y,z',',',1);
+-----------------------------+
| split_part('x,y,z', ',', 1) |
+-----------------------------+
| x |
+-----------------------------+
--以','為分隔符拆分'x,y,z'并返回第2個(gè)字符串
[master:21000] > select split_part('x,y,z',',',2);
+-----------------------------+
| split_part('x,y,z', ',', 2) |
+-----------------------------+
| y |
+-----------------------------+
--以','為分隔符拆分'x,y,z'并返回第3個(gè)字符串
[master:21000] > select split_part('x,y,z',',',3);
+-----------------------------+
| split_part('x,y,z', ',', 3) |
+-----------------------------+
| z |
+-----------------------------+
strleft(string a,int num_chars)
截取字符串,返回左邊的n個(gè)字符
--從左邊截取字符串'hello world'拘荡,返回長度為4的字符串
[master:21000] > select strleft('hello world',4) as strleft;
+---------+
| strleft |
+---------+
| hell |
+---------+
strright(string a,int num_chars)
截取字符串臼节,返回右邊的n個(gè)字符
--從右邊截取字符串'hello world',返回長度為4的字符串
[master:21000] > select strright('hello world',4) as strright;
+----------+
| strright |
+----------+
| orld |
+----------+
substr(string a,int start,[int len])
substring(string a,int start,[int len])
返回從指定點(diǎn)開始的字符串部分,可選地指定最大長度
--截取字符串'hello world',從第6位開始
[master:21000] > select substr('hello world',6) as substr;
+--------+
| substr |
+--------+
| world |
+--------+
--截取字符串'hello world'网缝,從第6位開始巨税,長度為3
[master:21000] > select substr('hello world',6,3) as substr;
+--------+
| substr |
+--------+
| wo |
+--------+
--截取字符串'hello world',從第6位開始
[master:21000] > select substring('hello world',6) as substring;
+-----------+
| substring |
+-----------+
| world |
+-----------+
--截取字符串'hello world'粉臊,從第6位開始草添,長度為3
[master:21000] > select substring('hello world',6,3) as substring;
+-----------+
| substring |
+-----------+
| wo |
+-----------+
translate(string input,string from,string to)
將字符串中的一些字符替換為其他字符
不能替換字符串,from字符串與to字符串一一對應(yīng)扼仲,再替換 input字符串中所有對應(yīng)字符
--將'world'替換為'cauchy',只能匹配到想相同長度,即'cauch',且拆分為w->c,o->a,r->u,l->c,d->h
[master:21000] > select translate('hello world','world','cauchy') as translate;
+-------------+
| translate |
+-------------+
| hecca cauch |
+-------------+
--替換字符串中所有屬于'world'的字符為'abcde'
[master:21000] > select translate('hello world','world','abcde') as translate;
+-------------+
| translate |
+-------------+
| heddb abcde |
+-------------+