本文介绍了在HIVE中的字符串转换的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我在hive表格的其中一列中有一个字符串,如下所示。
{ABC:ABCVAL,XYZ:XYZVAL}
我想将它转换为HIVE
{ ABC:ABCVAL,XYZ:XYZVAL}
我该怎么做。
解决方案
简单版该值不包含冒号(:
)符号。
查找除 {,:
(键)以及之外的字符序列:
select regexp_replace('{ABC:ABCVAL,XYZ:XYZVAL}' '(小于密钥GT; [^ {,:] +):', ' $ {键}:');
{ABC:ABCVAL,XYZ:XYZVAL}
安全版
寻找可选的 {
通过一系列紧跟着:
(冒号)的字符(键),然后是,字符(值),然后再次
,然后是
,
或]
select regexp_replace
(
'{ABC:ABCVAL,XYZ (?< colon>:)(?< value>)(< key> '$ {key}$ {colon} $ {value} $ {aftervalue}'
'){(< aftervalue> [,}])'
,'$ {beforekey} )
;
{ABC:ABCVAL,XYZ:XYZVAL}
I have a string as given below in one of the columns in hive table.
{ABC:"ABCVAL",XYZ:"XYZVAL"}
I want to convert it to in HIVE
{"ABC":"ABCVAL","XYZ":"XYZVAL"}
How can I do this.
解决方案
Simple version
Assuming the value does not contain colon (:
) symbol.
Look for sequence of characters other than {,:
(key) followed by :
select regexp_replace('{ABC:"ABCVAL",XYZ:"XYZVAL"}','(?<key>[^{,:]+):','"${key}":');
{"ABC":"ABCVAL","XYZ":"XYZVAL"}
Safe version
Look for optional {
(beforekey), followed by a sequence of characters (key) which is immediately followed by :
(colon), followed by "
, sequence of characters (value) and then "
again, followed by ,
or ]
(aftervalue)
select regexp_replace
(
'{ABC:"ABCVAL",XYZ:"XYZVAL"}'
,'(?<beforekey>\\{)?(?<key>.*?)(?<colon>:)(?<value>".*?")(?<aftervalue>[,}])'
,'${beforekey}"${key}"${colon}${value}${aftervalue}'
)
;
{"ABC":"ABCVAL","XYZ":"XYZVAL"}
这篇关于在HIVE中的字符串转换的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!