在 Hive 中,如何使用 "regexp_replace()" 对标记值执行通配符搜索以将其替换为公共值?

In Hive, how to perform wild card search of a tag value using "regexp_replace()" to replace it with a common value?

我在多个 Value 系列中出现了 string 个标签,其中包含不同的值。我需要利用 regexp_replace() 进行通配符搜索,读取所有此类 string 事件并将它们替换为通用值“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>)','NULL')

演示:

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>)','NULL') 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>)','NULL') from mydata