在Hive中,如何使用"regexp_replace()"对标签值进行通配符搜索,将其替换为通用值?



我在由不同值组成的多个族中出现了字符串标记。我需要使用regexp_replace()进行通配符搜索,读取所有这样的字符串出现&用一个共同的值"来代替它们;NULL";。

下面是一个示例XML:

<ParentArray>
<ParentFieldArray>
<Value>
<string>123</string>
<string>234</string>
</Value>
</ParentFieldArray>
<ParentFieldArray>
<Value>
<string>345</string>
<string>456</string>
</Value>
</ParentFieldArray>
</ParentArray>

期望的是读取所有String标记值,并将其替换为NULL。

使用

regexp_replace(str,'(<string>)(\d+)(</string>)','$1NULL$3')

演示:

select "<ParentArray>
<ParentFieldArray>
<Value>
<string>123</string>
<string>234</string>
</Value>
</ParentFieldArray>
<ParentFieldArray>
<Value>
<string>345</string>
<string>456</string>
</Value>
</ParentFieldArray>
</ParentArray>
" as str)
select regexp_replace(str,'(<string>)(\d+)(</string>)','$1NULL$3') from mydata

结果:

<ParentArray>
<ParentFieldArray>
<Value>
<string>NULL</string>
<string>NULL</string>
</Value>
</ParentFieldArray>
<ParentFieldArray>
<Value>
<string>NULL</string>
<string>NULL</string>
</Value>
</ParentFieldArray>
</ParentArray>

如果您想不仅替换值中的数字,包括空值,请使用:

select regexp_replace(str,'(<string>)(.*)(</string>)','$1NULL$3') from mydata

最新更新