Source code for airflow.providers.google.cloud.transfers.bigquery_to_gcs
## Licensed to the Apache Software Foundation (ASF) under one# or more contributor license agreements. See the NOTICE file# distributed with this work for additional information# regarding copyright ownership. The ASF licenses this file# to you under the Apache License, Version 2.0 (the# "License"); you may not use this file except in compliance# with the License. You may obtain a copy of the License at## http://www.apache.org/licenses/LICENSE-2.0## Unless required by applicable law or agreed to in writing,# software distributed under the License is distributed on an# "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY# KIND, either express or implied. See the License for the# specific language governing permissions and limitations# under the License."""This module contains Google BigQuery to Google Cloud Storage operator."""from__future__importannotationsfromtypingimportTYPE_CHECKING,Any,Sequencefromgoogle.api_core.exceptionsimportConflictfromgoogle.cloud.bigqueryimportDEFAULT_RETRY,UnknownJobfromairflow.configurationimportconffromairflow.exceptionsimportAirflowExceptionfromairflow.modelsimportBaseOperatorfromairflow.providers.google.cloud.hooks.bigqueryimportBigQueryHook,BigQueryJobfromairflow.providers.google.cloud.links.bigqueryimportBigQueryTableLinkfromairflow.providers.google.cloud.triggers.bigqueryimportBigQueryInsertJobTriggerfromairflow.utils.helpersimportmerge_dictsifTYPE_CHECKING:fromgoogle.api_core.retryimportRetryfromairflow.utils.contextimportContext
[docs]classBigQueryToGCSOperator(BaseOperator):""" Transfers a BigQuery table to a Google Cloud Storage bucket. .. seealso:: For more information on how to use this operator, take a look at the guide: :ref:`howto/operator:BigQueryToGCSOperator` .. seealso:: For more details about these parameters: https://cloud.google.com/bigquery/docs/reference/v2/jobs :param source_project_dataset_table: The dotted ``(<project>.|<project>:)<dataset>.<table>`` BigQuery table to use as the source data. If ``<project>`` is not included, project will be the project defined in the connection json. (templated) :param destination_cloud_storage_uris: The destination Google Cloud Storage URI (e.g. gs://some-bucket/some-file.txt). (templated) Follows convention defined here: https://cloud.google.com/bigquery/exporting-data-from-bigquery#exportingmultiple :param project_id: Google Cloud Project where the job is running :param compression: Type of compression to use. :param export_format: File format to export. :param field_delimiter: The delimiter to use when extracting to a CSV. :param print_header: Whether to print a header for a CSV file extract. :param gcp_conn_id: (Optional) The connection ID used to connect to Google Cloud. :param labels: a dictionary containing labels for the job/query, passed to BigQuery :param location: The location used for the operation. :param impersonation_chain: Optional service account to impersonate using short-term credentials, or chained list of accounts required to get the access_token of the last account in the list, which will be impersonated in the request. If set as a string, the account must grant the originating account the Service Account Token Creator IAM role. If set as a sequence, the identities from the list must grant Service Account Token Creator IAM role to the directly preceding identity, with first account from the list granting this role to the originating account (templated). :param result_retry: How to retry the `result` call that retrieves rows :param result_timeout: The number of seconds to wait for `result` method before using `result_retry` :param job_id: The ID of the job. It will be suffixed with hash of job configuration unless ``force_rerun`` is True. The ID must contain only letters (a-z, A-Z), numbers (0-9), underscores (_), or dashes (-). The maximum length is 1,024 characters. If not provided then uuid will be generated. :param force_rerun: If True then operator will use hash of uuid as job id suffix :param reattach_states: Set of BigQuery job's states in case of which we should reattach to the job. Should be other than final states. :param deferrable: Run operator in the deferrable mode """
def__init__(self,*,source_project_dataset_table:str,destination_cloud_storage_uris:list[str],project_id:str|None=None,compression:str="NONE",export_format:str="CSV",field_delimiter:str=",",print_header:bool=True,gcp_conn_id:str="google_cloud_default",labels:dict|None=None,location:str|None=None,impersonation_chain:str|Sequence[str]|None=None,result_retry:Retry=DEFAULT_RETRY,result_timeout:float|None=None,job_id:str|None=None,force_rerun:bool=False,reattach_states:set[str]|None=None,deferrable:bool=conf.getboolean("operators","default_deferrable",fallback=False),**kwargs,)->None:super().__init__(**kwargs)self.project_id=project_idself.source_project_dataset_table=source_project_dataset_tableself.destination_cloud_storage_uris=destination_cloud_storage_urisself.compression=compressionself.export_format=export_formatself.field_delimiter=field_delimiterself.print_header=print_headerself.gcp_conn_id=gcp_conn_idself.labels=labelsself.location=locationself.impersonation_chain=impersonation_chainself.result_retry=result_retryself.result_timeout=result_timeoutself.job_id=job_idself.force_rerun=force_rerunself.reattach_states:set[str]=reattach_statesorset()self.hook:BigQueryHook|None=Noneself.deferrable=deferrableself._job_id:str=""@staticmethoddef_handle_job_error(job:BigQueryJob|UnknownJob)->None:ifjob.error_result:raiseAirflowException(f"BigQuery job {job.job_id} failed: {job.error_result}")def_prepare_configuration(self):source_project,source_dataset,source_table=self.hook.split_tablename(table_input=self.source_project_dataset_table,default_project_id=self.hook.project_id,var_name="source_project_dataset_table",)configuration:dict[str,Any]={"extract":{"sourceTable":{"projectId":source_project,"datasetId":source_dataset,"tableId":source_table,},"compression":self.compression,"destinationUris":self.destination_cloud_storage_uris,"destinationFormat":self.export_format,}}ifself.labels:configuration["labels"]=self.labelsifself.export_format=="CSV":# Only set fieldDelimiter and printHeader fields if using CSV.# Google does not like it if you set these fields for other export# formats.configuration["extract"]["fieldDelimiter"]=self.field_delimiterconfiguration["extract"]["printHeader"]=self.print_headerreturnconfigurationdef_submit_job(self,hook:BigQueryHook,job_id:str,configuration:dict,)->BigQueryJob:# Submit a new job without waiting for it to complete.returnhook.insert_job(configuration=configuration,project_id=self.project_idorhook.project_id,location=self.location,job_id=job_id,timeout=self.result_timeout,retry=self.result_retry,nowait=self.deferrable,)
[docs]defexecute(self,context:Context):self.log.info("Executing extract of %s into: %s",self.source_project_dataset_table,self.destination_cloud_storage_uris,)hook=BigQueryHook(gcp_conn_id=self.gcp_conn_id,location=self.location,impersonation_chain=self.impersonation_chain,)self.hook=hookconfiguration=self._prepare_configuration()job_id=hook.generate_job_id(job_id=self.job_id,dag_id=self.dag_id,task_id=self.task_id,logical_date=context["logical_date"],configuration=configuration,force_rerun=self.force_rerun,)try:self.log.info("Executing: %s",configuration)job:BigQueryJob|UnknownJob=self._submit_job(hook=hook,job_id=job_id,configuration=configuration)exceptConflict:# If the job already exists retrieve itjob=hook.get_job(project_id=self.project_id,location=self.location,job_id=job_id,)ifjob.stateinself.reattach_states:# We are reattaching to a jobjob.result(timeout=self.result_timeout,retry=self.result_retry)self._handle_job_error(job)else:# Same job configuration so we need force_rerunraiseAirflowException(f"Job with id: {job_id} already exists and is in {job.state} state. If you "f"want to force rerun it consider setting `force_rerun=True`."f"Or, if you want to reattach in this scenario add {job.state} to `reattach_states`")self._job_id=job.job_idconf=job.to_api_repr()["configuration"]["extract"]["sourceTable"]dataset_id,project_id,table_id=conf["datasetId"],conf["projectId"],conf["tableId"]BigQueryTableLink.persist(context=context,task_instance=self,dataset_id=dataset_id,project_id=project_id,table_id=table_id,)ifself.deferrable:self.defer(timeout=self.execution_timeout,trigger=BigQueryInsertJobTrigger(conn_id=self.gcp_conn_id,job_id=self._job_id,project_id=self.project_idorself.hook.project_id,),method_name="execute_complete",)else:job.result(timeout=self.result_timeout,retry=self.result_retry)
[docs]defexecute_complete(self,context:Context,event:dict[str,Any]):""" Callback for when the trigger fires - returns immediately. Relies on trigger to throw an exception, otherwise it assumes execution was successful. """ifevent["status"]=="error":raiseAirflowException(event["message"])self.log.info("%s completed with response %s ",self.task_id,event["message"],)
[docs]defget_openlineage_facets_on_complete(self,task_instance):"""Implementing on_complete as we will include final BQ job id."""frompathlibimportPathfromopenlineage.client.facetimport(ExternalQueryRunFacet,SymlinksDatasetFacet,SymlinksDatasetFacetIdentifiers,)fromopenlineage.client.runimportDatasetfromairflow.providers.google.cloud.hooks.gcsimport_parse_gcs_urlfromairflow.providers.google.cloud.utils.openlineageimport(get_facets_from_bq_table,get_identity_column_lineage_facet,)fromairflow.providers.openlineage.extractorsimportOperatorLineagetable_object=self.hook.get_client(self.hook.project_id).get_table(self.source_project_dataset_table)input_dataset=Dataset(namespace="bigquery",name=str(table_object.reference),facets=get_facets_from_bq_table(table_object),)output_dataset_facets={"schema":input_dataset.facets["schema"],"columnLineage":get_identity_column_lineage_facet(field_names=[field.nameforfieldintable_object.schema],input_datasets=[input_dataset]),}output_datasets=[]foruriinsorted(self.destination_cloud_storage_uris):bucket,blob=_parse_gcs_url(uri)additional_facets={}if"*"inblob:# If wildcard ("*") is used in gcs path, we want the name of dataset to be directory name,# but we create a symlink to the full object path with wildcard.additional_facets={"symlink":SymlinksDatasetFacet(identifiers=[SymlinksDatasetFacetIdentifiers(namespace=f"gs://{bucket}",name=blob,type="file")]),}blob=Path(blob).parent.as_posix()ifblob==".":# blob path does not have leading slash, but we need root dataset name to be "/"blob="/"dataset=Dataset(namespace=f"gs://{bucket}",name=blob,facets=merge_dicts(output_dataset_facets,additional_facets),)output_datasets.append(dataset)run_facets={}ifself._job_id:run_facets={"externalQuery":ExternalQueryRunFacet(externalQueryId=self._job_id,source="bigquery"),}returnOperatorLineage(inputs=[input_dataset],outputs=output_datasets,run_facets=run_facets)