先认识几个函数:
1.split函数
用途:可以将字符串按指定字符进行分割,返回值为分割后的字符串数组
使用方法:split(需分割的字段,"指定字符串")
eg:原数据格式如下表
sub_type |
[{“A”:“1”,“B”:“2”,“C”:3},{“A”:“4”,“B”:“5”,“C”:6}] |
通过split函数可返回如下表,即按逗号分隔开后每部分存在引号中
select split(sub_type,"},") as sub_type from appeal
需注意,返回结果的第一项末尾没有右侧的 }, 部分
sub_type |
["[{“A”:“1”,“B”:“2”,“C”:3"," {“A”:“4”,“B”:“5”,“C”:6}]"] |
2.lateral view explode函数
用途:将一行数据拆分为多行数据
使用方法:lateral view explode(字段)
eg:原数据格式如下表1,之后其他函数的原数据格式同样都为表1
num |
sub_type |
1 |
[{“A”:“1”,“B”:“2”,“C”:3},{“A”:“4”,“B”:“5”,“C”:6}] |
2 |
[{“A”:“1”,“B”:“2”,“C”:3},{“A”:“4”,“B”:“5”,“C”:6}] |
3 |
[{“A”:“1”,“B”:“2”,“C”:3},{“A”:“4”,“B”:“5”,“C”:6}] |
通过lateral view explode可返回如下表,即第一列的元素会与第二列同行中的每个元素相对应
select num, single_sub_type from
(select num, sub_type from t) t1 lateral view explode(split(sub_type,"},") a as single_sub_type
num |
single_sub_type |
1 |
[{“A”:“1”,“B”:“2”,“C”:3 |
1 |
{“A”:“4”,“B”:“5”,“C”:6}] |
2 |
[{“A”:“1”,“B”:“2”,“C”:3 |
2 |
{“A”:“4”,“B”:“5”,“C”:6}] |
3 |
[{“A”:“1”,“B”:“2”,“C”:3 |
3 |
{“A”:“4”,“B”:“5”,“C”:6}] |
3.regexp_replace函数
用途:替换指定字符串,相当于