简体   繁体   English

如何从 bigquery 中提取 csv 并将其发送到具有 python 的外部服务器?

[英]How to extract a csv from bigquery and send it to an external server with python?

I would like to automate a csv file extraction process from Google BigQuery to a Google Cloud Storage Bucket, and from the latter to an external server with two Python scripts, could you help me please?我想自动化 csv 文件提取过程,从 Google BigQuery 到 Google Cloud Storage Bucket,然后从后者到具有两个 Python 脚本的外部服务器,你能帮帮我吗? I would appreciate it.我会很感激。

For extracting from BigQuery in Python, you can use the Python Client for Google BigQuery .要从 Python 中的 BigQuery 中提取,您可以使用Python Client for Google BigQuery

The below snippet based on this repository should get you going:以下基于此存储库的代码段应该可以帮助您:

# 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)
)

In order to post the export to another server, you can use the Cloud Storage Client Library for Python to post the CSV file to your server or service of choice.为了将导出发布到另一台服务器,您可以使用 Python 的云存储客户端库将 CSV 文件发布到您的服务器或选择的服务。

As per my knowledge, BigQuery can't export/download query result to GCS or Local File.据我所知,BigQuery 无法将查询结果导出/下载到 GCS 或本地文件。 You can keep it in a temporary / stagging table and then use code like below to export to gcs:您可以将其保存在临时/临时表中,然后使用如下代码导出到 gcs:

https://cloud.google.com/bigquery/docs/exporting-data#exporting_table_data https://cloud.google.com/bigquery/docs/exporting-data#exporting_table_data

So you can put this in a container and deploy it as cloudrun service and call this from cloud scheduler.因此,您可以将其放入容器中并将其部署为 cloudrun 服务并从云调度程序中调用它。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM