【发布时间】:2021-10-19 07:56:35
【问题描述】:
我正在尝试使用两个成功部署的云功能和一个成功运行的云调度程序自动将 JSON 数据上传到 BigQuery。运行云调度程序后,数据会上传到我的云存储,但不会上传到 BigQuery。
以下是我的代码和 JSON 数据:
# function 1 triggered by http
def function(request):
url = "https://api...."
headers = {"Content-Type" : "application/json",
"Authorization" : "..."}
response = requests.get(url, headers=headers)
json_data = response.json()
pretty_json = json.dumps(json_data, indent=4, sort_keys=True)
storage_client = storage.Client()
bucket = storage_client.bucket("bucket_name")
blob = bucket.blob("blob_name")
blob.upload_from_string(pretty_json)
# function 2 triggered by cloud storage -> event type finalize/create
def function_2(data, context):
client = bigquery.Client()
table_id = "booming-post-322920:dataset_name.table_name"
job_config = bigquery.LoadJobConfig()
job_config.schema=[
bigquery.SchemaField("order_items", "INTEGER"),
bigquery.SchemaField("created_at", "TIMESTAMP"),
.....,
bigquery.SchemaField("updated_at", "TIMESTAMP")
]
job_config.source_format = bigquery.SourceFormat.NEWLINE_DELIMITED_JSON
uri = 'gs://bucket_name/blob_name'
load_job = client.load_table_from_uri(
uri,
table_id,
location="US",
job_config=job_config
)
load_job.result()
这是我的 JSON 数据 pretty_json 的样子:
{
"records": [
{
"active": null,
"approved": null,
"buyer": [
1
],
"cancel_reason": null,
"cancelled": null,
"chef": [
1
],
"completed": null,
"created_at": "2021-07-15T17:44:31.064Z",
...
请指教。
【问题讨论】:
-
您的意思是说 JSON 没有加载到 BigQuery 中吗?您的问题描述仅提及有关存储的问题。假设 BigQuery 没有加载数据,你是如何触发 function_2 的?您是否同时运行这两个功能?想知道是否可能存在 json 仍在上传到存储桶且 function_2 已被触发的竞争条件。
-
没有我对函数 2 的触发是云存储更新时。我怀疑 JSON 格式和 bigquery 可能存在问题。
-
@CaioT 是 JSON 未加载到 BigQuery 中
-
你从哪里得到变量值 'blob_name' 来构建 URI uri = 'gs://bucket_name/blob_name' ?
-
@CaiOT 从这里stackoverflow.com/questions/25373467/…
标签: python google-cloud-platform google-cloud-storage