簡體   English   中英

Google GCP Cloud Functions 到 BigQuery 錯誤

[英]Google GCP Cloud Functions to BigQuery Error

我創建了一個用於將數據發送到 BigQuery 的 Cloud Functions Cloud Functions 正在從 pub/sub 接收數據。

場景一:我寫了一個python代碼直接發送JSON數據到Bigquery,沒問題

場景2:我把JSON數據保存到.json文件,用bq load命令手動上傳到Bigquery,沒問題

場景 3:(出現錯誤)Cloud Functions 可以從 Pub/Sub 接收數據,但無法將其發送到 BigQuery。

這是雲函數的代碼:

from google.cloud import bigquery
import base64, json, sys, os

def pubsub_to_bq(event, context):
   if 'data' in event:
      print("Event Data is found : " + str(event['data']))
      name = base64.b64decode(event['data']).decode('utf-8')
   else:
      name = 'World'
   print('Hello {}!'.format(name))


   pubsub_message = base64.b64decode(event['data']).decode('utf-8')
   print(pubsub_message)
   to_bigquery(os.environ['dataset'], os.environ['table'], json.loads(str(pubsub_message)))

def to_bigquery(dataset, table, document):
   bigquery_client = bigquery.Client()
   table = bigquery_client.dataset(dataset).table(table)
   
   job_config.source_format = bq.SourceFormat.NEWLINE_DELIMITED_JSON
   job_config = bq.LoadJobConfig()
   job_config.autodetect = True
   
   errors = bigquery_client.insert_rows_json(table,json_rows=[document],job_config=job_config)
   if errors != [] :
      print(errors, file=sys.stderr)

我已經嘗試了兩種類型的 JSON 數據格式,但沒有運氣。 [{"field1":"data1","field2":"data2"}] 或 {"field1":"data1","field2":"data2"}

我可以從 Cloud Functions 事件日志中獲得的所有錯誤消息是:textPayload:“函數執行耗時 100 毫秒,完成狀態:'crash'”

任何專家可以幫助我嗎? 謝謝。

如果你看一下庫代碼,你有這個insert_rows_json

    def insert_rows_json(
        self,
        table,
        json_rows,
        row_ids=None,
        skip_invalid_rows=None,
        ignore_unknown_values=None,
        template_suffix=None,
        retry=DEFAULT_RETRY,
        timeout=None,
    ):

沒有job_config參數! 崩潰應該來自這個錯誤

insert_rows_json方法執行流式插入而不是加載作業。

對於來自 JSON 的加載作業,您可以使用load_table_from_json方法,您也可以在庫的源代碼中找到該方法。 代碼庫與此類似(對於 JobConfig 選項)

    def load_table_from_json(
        self,
        json_rows,
        destination,
        num_retries=_DEFAULT_NUM_RETRIES,
        job_id=None,
        job_id_prefix=None,
        location=None,
        project=None,
        job_config=None,
    ):

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM