將表格資料去識別化示例

Sensitive Data Protection 可偵測、分類及去識別化結構化資料中的機密資料。將內容去識別化為資料表時,結構和資料欄會為 Sensitive Data Protection 提供其他線索,讓其在某些使用情況下取得更好的結果。舉例來說,您可以掃描單一資料欄的特定資料類型,而非整個資料表結構。

本主題提供範例,說明如何設定結構化文字中的機密資料去識別化。去識別化功能是透過記錄轉換啟用。這些轉換會套用到識別為特定 infoType 的表格文字資料中的值,或是整欄表格資料中的值。

本主題也提供使用密碼編譯雜湊方法轉換表格資料的範例。加密轉換方法需要加密編譯金鑰,因此獨一無二。

下列範例中的 JSON 可插入 "deidentifyConfig" 屬性內的任何去識別化要求中。DeidentifyConfig按一下「APIs Explorer 範例」連結,即可在 APIs Explorer 中試用範例 JSON。

轉換資料欄 (不檢查)

如要轉換內容已知的特定資料欄,可以略過檢查,直接指定轉換方式。下表後方的範例會將「HAPPINESS SCORE」欄分組,每 10 分為一組。

輸入 轉換後的表格
年齡 PATIENT 快樂指數
101 查爾斯狄更斯 95
22 珍奧斯汀 21
55 馬克吐溫 75
年齡 PATIENT 快樂指數
101 查爾斯狄更斯 90:100
22 珍奧斯汀 20:30
55 馬克吐溫 70:80

C#

如要瞭解如何安裝及使用 Sensitive Data Protection 的用戶端程式庫,請參閱「Sensitive Data Protection 用戶端程式庫」。

如要向 Sensitive Data Protection 進行驗證,請設定應用程式預設憑證。詳情請參閱「為本機開發環境設定驗證機制」。


using System;
using Google.Api.Gax.ResourceNames;
using Google.Cloud.Dlp.V2;

public class DeidentifyUsingTableBucketing
{
    public static Table DeidentifyData(
        string projectId,
        Table tableToInspect = null)
    {
        // Instantiate dlp client.
        var dlp = DlpServiceClient.Create();

        // Construct the table if null.
        if (tableToInspect == null)
        {
            var row1 = new Value[]
            {
                new Value { StringValue = "101" },
                new Value { StringValue = "Charles Dickens" },
                new Value { StringValue = "95" }
            };
            var row2 = new Value[]
            {
                new Value { StringValue = "22" },
                new Value { StringValue = "Jane Austin" },
                new Value { StringValue = "21" }
            };
            var row3 = new Value[]
            {
                new Value { StringValue = "55" },
                new Value { StringValue = "Mark Twain" },
                new Value { StringValue = "75" }
            };

            tableToInspect = new Table
            {
                Headers =
                {
                    new FieldId { Name = "AGE" },
                    new FieldId { Name = "PATIENT" },
                    new FieldId { Name = "HAPPINESS SCORE" }
                },
                Rows =
                {
                    new Table.Types.Row { Values = { row1 } },
                    new Table.Types.Row { Values = { row2 } },
                    new Table.Types.Row { Values = { row3 } }
                }
            };
        }

        // Construct the table content item.
        var contentItem = new ContentItem { Table = tableToInspect };

        // Specify how the content should be de-identified.
        var fixedSizeBucketingConfig = new FixedSizeBucketingConfig
        {
            BucketSize = 10,
            LowerBound = new Value { IntegerValue = 0 },
            UpperBound = new Value { IntegerValue = 100 },
        };

        // Specify the fields to be encrypted.
        var fields = new FieldId[] { new FieldId { Name = "HAPPINESS SCORE" } };

        // Associate the encryption with the specified field.
        var fieldTransformation = new FieldTransformation
        {
            PrimitiveTransformation = new PrimitiveTransformation
            {
                FixedSizeBucketingConfig = fixedSizeBucketingConfig
            },
            Fields = { fields }
        };

        // Construct the deidentify config.
        var deidentifyConfig = new DeidentifyConfig
        {
            RecordTransformations = new RecordTransformations
            {
                FieldTransformations = { fieldTransformation }
            }
        };

        // Construct the request.
        var request = new DeidentifyContentRequest
        {
            ParentAsLocationName = new LocationName(projectId, "global"),
            DeidentifyConfig = deidentifyConfig,
            Item = contentItem,
        };

        // Call the API.
        var response = dlp.DeidentifyContent(request);

        // Inspect the response.
        Console.WriteLine(response.Item.Table);

        return response.Item.Table;
    }
}

Go

如要瞭解如何安裝及使用 Sensitive Data Protection 的用戶端程式庫,請參閱「