soda-core icon indicating copy to clipboard operation
soda-core copied to clipboard

Associating labels with BigQuery queries

Open tombaeyens opened this issue 2 years ago • 1 comments

It seems that through the BigQuery command line, it's possible to add labels to jobs / queries: https://cloud.google.com/bigquery/docs/adding-labels#adding_a_label_to_a_job

What I didn't find yet is how to set those labels through the PEP 249 DBAPI using Google's BigQuery Python library as the driver: https://cloud.google.com/python/docs/reference/bigquery/latest/dbapi

Ideally it would be possible in the creation of the DBAPI connecttion somehow

            self.client = bigquery.Client(
                project=self.project_id,
                credentials=self.credentials,
                default_query_job_config=bigquery.QueryJobConfig(
                    default_dataset=f"{self.project_id}.{self.dataset}",
                ),
                location=self.location,
                client_info=self.client_info,
                client_options=self.client_options,
            )
            self.connection = dbapi.Connection(self.client)

But I didn't find that yet. Or alternatively it could possible to add the labels when executing the queries,

but that would be harder to implement i think.

Any suggestions?

tombaeyens avatar Sep 02 '22 13:09 tombaeyens

Hi @tombaeyens,

this is a great idea to add. Labels for queries are actually quite useful !

Could you not just pass the labels attribute to the QueryJobConfig? See https://cloud.google.com/python/docs/reference/bigquery/latest/google.cloud.bigquery.job.QueryJobConfig#google_cloud_bigquery_job_QueryJobConfig_labels

e.g:

 self.client = bigquery.Client(
                project=self.project_id,
                credentials=self.credentials,
                default_query_job_config=bigquery.QueryJobConfig(
                    default_dataset=f"{self.project_id}.{self.dataset}",
                    labels={"environemnt": "testing"}
                ),
                location=self.location,
                client_info=self.client_info,
                client_options=self.client_options,
            )

linderttobias avatar Sep 15 '22 08:09 linderttobias