How Can I Attach Policy Tags to columns using Python API

350 Views Asked by At

As a part of data governance, we have created Taxonomies, Policy Tags Using "Python API". And I am trying to Assign Policy Tags to Columns [Name, Age] for a table Project.Dataset.TMP_TBL. Looked across the GCP Documentation but couldn't find any code snippets of Python to do this. Please Help me out with and Example code Snippet to do so.

1

There are 1 best solutions below

0
On

You can use update_table() to assign policy tags to columns. When updating a policy tag for a specific column see code below:

from google.cloud import bigquery
from google.cloud.bigquery.schema import SchemaField,PolicyTagList
client = bigquery.Client()

table_id = 'project_id.dataset_id.table_id'

policy_tags = PolicyTagList(names=["projects/<my_project_id>/locations/us/taxonomies/<taxonomy_id>/policyTags/<policy_tag_id>"])
table = client.get_table(table_id)

table.schema = [
        SchemaField(
            name="name",
            field_type="STRING",
            policy_tags=policy_tags
            ),
        SchemaField(
            name="age",
            field_type="INT64"
            )
        ]

table = client.update_table(table=table,fields=["schema"])
print(table.schema)

Output:

enter image description here

NOTE: For example you have a table with 2 columns (name, age). If you want to update the policy_tag for name only you can do so as shown with the code above. But you also need to redefine age so it won't error out.