如何从Cloud Function执行.sql文件(BigQuery)?

时间:2019-08-01 18:21:38

标签: google-bigquery google-cloud-functions

有人尝试过通过Cloud Function执行存储在gs:// bucket中的BigQuery脚本吗?

我找不到任何文档。

谢谢。

1 个答案:

答案 0 :(得分:0)

使用Python:

requirements.txt文件

# Function dependencies, for example:
# package>=version
google-cloud-bigquery
google-cloud-storage

main.py文件

from google.cloud import bigquery
from google.cloud import storage
import base64

def execute-sql-scripts(request):
    client = bigquery.Client(project='bigquery-project-id')
    # Read query from GCS bucket.
    gcs_client = storage.Client('storage-project-id')
    query = read_sql(gcs_client,'bucket-name')
    job = client.query(query)
    result = job.result()


def read_sql(gcs_client, bucket_id):
    bucket = gcs_client.get_bucket(bucket_id)
    blob = bucket.get_blob("update-script.sql")
    contents = blob.download_as_string()
    return contents.decode('utf-8')