Get hands-on experience with 20+ free Google Cloud products and $300 in free credit for new customers.

How to apply De-identification template on BigQuery table

I've multiple datasets and tables which has huge data, it's historical Data , and I need to apply my de-identification template on those tables, and those tables mostly has partition tables which has partition_require True, 

I need to apply de-identify template only on matching values, I don't need to pass the partition details.

do we have any other from DLP Jobs there directly update the tables? Or better suggestions ?

Thanks in Advance.


0 1 369
1 REPLY 1

De-identifying data in Google Cloud BigQuery, particularly for partitioned tables with historical data, presents several challenges. Here are some key considerations:

 

  • Partitioned Tables: Working with partitioned tables, especially those with partition_require = True, adds complexity. Direct in-place modification of such tables is not supported by Google Cloud DLP, necessitating the creation of new tables to store de-identified data.

  • Data Volume: Handling large historical datasets requires efficient and scalable solutions. The potential cost implications of duplicating data and the computational resources needed for de-identification must be carefully considered.

  • Template Match: De-identification efforts should focus on transforming only the values specified by the template, preserving the integrity of other data. Ensuring the template is accurately configured to target only relevant data is crucial.

DLP Jobs and New Tables

This approach involves using the Data Loss Prevention (DLP) API to create a job that references your de-identification template and source table. The job writes the de-identified output to a new BigQuery table, optionally partitioned similarly to the source table but without the partition_require = True constraint. This method leverages DLP’s built-in capabilities, avoids the challenges of in-place modification, and allows control over the output table structure. However, it requires creating duplicate tables, which may incur additional storage costs.

BigQuery Scripting 

Writing a script that iterates over partitions of the source tables can be an efficient method. This script dynamically constructs and executes SQL queries to apply the de-identification template to each partition and inserts the results into a new table. This approach allows fine-grained control over the de-identification process but is more complex to set up and maintain.

Here’s a Python example demonstrating how to create a DLP job for de-identification:

from google.cloud import dlp_v2
from google.cloud.dlp_v2 import types

def deidentify_table(project_id, dataset_id, table_id, template_name, output_dataset_id, output_table_id):
    dlp_client = dlp_v2.DlpServiceClient()
    parent = f"projects/{project_id}/locations/global"

    bigquery_table = {
        "project_id": project_id,
        "dataset_id": dataset_id,
        "table_id": table_id,
    }

    bigquery_output_table = {
        "project_id": project_id,
        "dataset_id": output_dataset_id,
        "table_id": output_table_id,
    }

    deidentify_config = {
        "deidentify_template": template_name,
    }

    job = {
        "inspect_job": {
            "storage_config": {
                "big_query_options": {
                    "table_reference": bigquery_table,
                }
            },
            "inspect_config": {},
            "actions": [
                {
                    "save_findings": {
                        "output_config": {
                            "table": bigquery_output_table,
                        }
                    }
                }
            ],
        },
        "deidentify_config": deidentify_config,
    }

    response = dlp_client.create_dlp_job(parent=parent, inspect_job=job)
    print(f"Created DLP job: {response.name}")

project_id = 'your-project-id'
dataset_id = 'your-dataset-id'
table_id = 'your-table-id'
template_name = 'projects/your-project-id/deidentifyTemplates/your-template-id'
output_dataset_id = 'your-output-dataset-id'
output_table_id = 'your-output-table-id'

deidentify_table(project_id, dataset_id, table_id, template_name, output_dataset_id, output_table_id)

To apply the de-identification process across multiple tables, a script can iterate over each table and dataset, applying the de-identification logic accordingly:

tables_to_deidentify = [
    {"dataset_id": "dataset1", "table_id": "table1"},
    {"dataset_id": "dataset2", "table_id": "table2"},
    # Add more datasets and tables as needed
]

for table in tables_to_deidentify:
    deidentify_table(
        project_id,
        table["dataset_id"],
        table["table_id"],
        template_name,
        output_dataset_id,
        f"{table['table_id']}_deidentified"
    )

Use Cloud Scheduler to run the script periodically if necessary, and monitor DLP jobs using Cloud Monitoring, setting up alerts for any job failures.