Hive:我们如何屏蔽位于 Hive 列中的 json 对象中的特定键值对(包含 PII 数据)?

Hive: How do we mask a particular key value pair ( containing PII data) in a json object, residing in a column, in Hive?

PII 数据存在于 json 对象的键值对中,存储在我的 column.Is 中的 Hive table 中,我只能通过任何方式屏蔽所需键值对的值,同时保持其他键值对不被屏蔽?

{ "name":"John", "age":30, "car":null, "mob_no": "99999999"} 

我需要屏蔽 PII 数据字段 "mob_no" 及其关联值。 json 的其他键值对应该是可见的

您可以使用 regexp_replace 将 json 中的某些值替换为一些字符串(参见下面示例中的 json_with_number_replaced 列)。

最好应用 sha256 而不是简单的替换,因为此函数具有很强的加密性(不可逆的单向函数)、确定性和容错性。 sha256 哈希函数的这些属性允许您加入混淆值(如果使用相同的混淆方法)并进行聚合。混淆后仍然可以统计出不同的mobile_numbers。请参阅以下示例中的 mobile_number_obfuscatedjson_with_number_obfuscated 列计算:

select json original_json,
       s.original_mobile_number,
       s.mobile_number_obfuscated,
       regexp_replace(s.json, '(mob_no":[\s]?")([+]?[\d]+)','xx') as json_with_number_replaced,
       regexp_replace(s.json, '(mob_no":[\s]?")([+]?[\d]+)',concat('',mobile_number_obfuscated)) as json_with_number_obfuscated
from
(
select regexp_extract(s.json, 'mob_no":[\s]?"([+]?[\d]+)',1) as original_mobile_number,
       java_method('org.apache.commons.codec.digest.DigestUtils', 'sha256Hex', regexp_extract(s.json, 'mob_no":[\s]?"([+]?[\d]+)',1)) mobile_number_obfuscated,
       s.json
from
(--original data
select '{ "name":"John", "age":30, "car":null, "mob_no": "+99999999"}' as json
)s
)s;

OK
original_json                                                   original_mobile_number  mobile_number_obfuscated                                                json_with_number_replaced                               json_with_number_obfuscated
{ "name":"John", "age":30, "car":null, "mob_no": "+99999999"}   +99999999               98ae38dddc1994179e21d104feb7b09e5627953d9fe9b9851239ac445b6de3cd        { "name":"John", "age":30, "car":null, "mob_no": "xx"}  { "name":"John", "age":30, "car":null, "mob_no": "98ae38dddc1994179e21d104feb7b09e5627953d9fe9b9851239ac445b6de3cd"}
Time taken: 1.988 seconds, Fetched: 1 row(s)

在此示例中,我演示了如何使用 regexp_replace 替换 JSON 中某些键的值,以及如何使用 sha256 混淆您的 PII 数据。