【问题标题】:How to execute .sql file(BigQuery) from Cloud Function?如何从 Cloud Function 执行 .sql 文件(BigQuery)?
【发布时间】:2019-08-01 18:21:38
【问题描述】:

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

我找不到任何文档。

提前致谢。

【问题讨论】:

    标签: google-bigquery google-cloud-functions


    【解决方案1】:

    使用 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')
    
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2021-05-02
      • 2020-06-04
      • 1970-01-01
      • 1970-01-01
      • 2021-05-02
      • 1970-01-01
      • 2020-01-10
      • 2019-04-30
      相关资源
      最近更新 更多