如何从bigquery提取csv并使用python将其发送到外部服务器?

时间:2020-06-03 12:22:56

标签: python google-cloud-platform google-bigquery google-cloud-storage bucket

我想自动将csv文件提取过程从Google BigQuery到Google Cloud Storage Bucket,再从后者到带有两个Python脚本的外部服务器,您能帮我吗?我会很感激的。

2 个答案:

答案 0 :(得分:1)

要使用Python从BigQuery中提取内容,可以使用Python Client for Google BigQuery

下面基于this repository的代码片段应该可以帮助您:

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

为了将导出发布到其他服务器,您可以使用Cloud Storage Client Library for Python将CSV文件发布到您选择的服务器或服务。

答案 1 :(得分:-1)

据我所知,BigQuery无法将查询结果导出/下载到GCS或本地文件。您可以将其保存在临时表中,然后使用以下代码将其导出到gcs:

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

因此,您可以将其放入容器中,并将其部署为cloudrun服务,然后从云调度程序中调用它。