【发布时间】:2022-11-17 22:54:57
【问题描述】:
我正在尝试使用简单的 Python 运算符而不是 BigQuery 运算符将一些数据插入表中,但我不确定如何实现它。我正在尝试以 Airflow DAG 的形式实现它。
我编写了一个简单的 DAG,并且设法使用以下方法将数据从 GCS Bucket 插入到 BigQuery,但我想使用 Python 运算符而不是 BigQuery 来执行此操作:
load_csv = gcs_to_bq.GoogleCloudStorageToBigQueryOperator(
task_id='gcs_to_bq_example',
bucket='cloud-samples-data',
source_objects=['bigquery/us-states/us-states.csv'],
destination_project_dataset_table='airflow_test.gcs_to_bq_table',
schema_fields=[
{'name': 'name', 'type': 'STRING', 'mode': 'NULLABLE'},
{'name': 'post_abbr', 'type': 'STRING', 'mode': 'NULLABLE'},
],
write_disposition='WRITE_TRUNCATE',
dag=dag)
我想使用简单的 Python 运算符而不是 BigQuery 来实现上述目标。
BQ 到 GCS: BigQuery 到 GCS:
# from google.cloud import bigquery
# client = bigquery.Client()
# bucket_name = 'my-bucket'
project = "bigquery-public-data"
dataset_id = "samples"
table_id = "shakespeare"
destination_uri = "gs://{}/{}".format(bucket_name, "shakespeare.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.
print(
"Exported {}:{}.{} to {}".format(project, dataset_id, table_id, destination_uri)
)
【问题讨论】:
-
你为什么要使用
PythonOperator而不是GCSToBigQueryOperator? -
我想使用
PythonOperator完成同样的任务。我只需要帮助来编写我的代码,而是使用PythonOperator代替。这只是为了扩展我的知识。
标签: python sql google-cloud-storage airflow directed-acyclic-graphs