截取
substr、substring
select substr('hello-world',7);
select substr('hello-world',7,3);
select substr('hello-world',-2);
切割
split
select split('123,abc,大中小',',')[0];
:'
123
'
替换
regexp_replace
select regexp_replace('abc','a','888');
select regexp_replace('abc123def','[0-9]','M');
select regexp_replace('[hello.world]','\\[,\\]','');
:'
在我们的hive表中存在某些特殊字符和hive表的列分隔符号或者行分隔符冲突的时候,会导致数据串列串行,所以可以使用以下方式进行对应符号的替换,进行解决。
使用正则替换:
'
regexp_replace(需要处理的字段名称,'\\n|\\r|\\t','') as 重新命名
regexp_extract
log:{"status":0,"startTime":"2021-03-31 14:40:19","endTime":"2021-03-31 14:40:32","totalTime":"12939s",}
select
regexp_extract(log,'"status":(.*?)(,)',1) as status,
regexp_extract(log,'"startTime":(.*?)(,)',1) as startTime,
regexp_extract(log,'"endTime":(.*?)(,)',1) as endTime,
regexp_extract(log,'"totalTime":(.*?)(,)',1) as totalTime
from table;
:'
0
"2021-03-31 14:40:19"
"2021-03-31 14:40:32"
"12939s"
'
包含
有张表cluster,表中数据为:ABC-PRD-DEF
需求:
判断是生产环境还是开发环境,PRD-生产环境 DEV-开发环境
方法1:使用函数
直接使用字符串包含的函数看是否包含PRD、DEV,但是此方法不准确,因为如果某个子字符串中恰巧包含PRD这3个字符,就会判断错误
select instr(name,'PRD') from cluster;
select locate('PRD',name) from cluster;
:'
instr(str,substr)=locate(substr,str)
1.instr和locate都可以判断substr在str首次出现的位置.
2.二者的参数顺序相反
3.找不到都是返回0
'
方法2:借助数组
select array_contains(split(name,'-'),'PRD') from cluster;