是to_delete参数需要在我的代码中从BigQuery提取到GCS?



我已经写了一些代码从BigQuery提取到GCS桶,使用谷歌云文档,我不确定to_delete参数是否需要在我的代码。

我还没有尝试任何东西,因为我不确定我将用什么来替换参数。

这是我的代码:

def extract_table(client, to_delete):
bucket_name = "extract_mytable_{}".format(_millis())
storage_client = storage.Client()
bucket = retry_storage_errors(storage_client.create_bucket)(bucket_name)
to_delete.append(bucket)
# [START bigquery_extract_table]
# from google.cloud import bigquery
# client = bigquery.Client()
# bucket_name = 'my-bucket'
project = "bigquery-public-data"
dataset_id = "samples"
table_id = "mytable"
destination_uri = "gs://{}/{}".format(bucket_name, "mytable.csv")
dataset_ref = bigquery.DatasetReference(project, dataset_id)
table_ref = dataset_ref.table(table_id)
extract_job = client.extract_table(
table_ref,
destination_uri,
# Location must match that of the source table.
location="US",
)  # API request
extract_job.result()  # Waits for job to complete.

如果您只想将BigQuery表导出到GCS,我认为不需要像to_delete这样的参数。

您也可以使用内置的Airflow操作符来执行与问题中显示的相同的代码,但使用BigQueryToGCSOperator操作符:

from airflow.providers.google.cloud.transfers.bigquery_to_gcs import BigQueryToGCSOperator

bigquery_to_gcs = BigQueryToGCSOperator(
task_id='bq_to_gsc_task',
compression='NONE',
export_format='CSV',
field_delimiter=',',
print_header=True,
source_project_dataset_table=f'{your_dataset}.{your_table}',
destination_cloud_storage_uris=[
f'gs://{your_bucket_name}/{your_output_filename}.csv',
],
)

BigQueryToGCSOperator允许更简单地执行相同的代码。

最新更新