无法使用 python 将 JSON 文件从谷歌云存储加载到 bigquery
Posted
技术标签:
【中文标题】无法使用 python 将 JSON 文件从谷歌云存储加载到 bigquery【英文标题】:Unable to load a JSON file from google cloud storage to bigquery using python 【发布时间】:2017-07-05 06:43:09 【问题描述】:我正在尝试将文件从我的 Google 云存储导出到 Google bigquery,但遇到了许多问题。
#standardSQL
import json
import argparse
import time
import uuid
from google.cloud import bigquery
from google.cloud import storage
dataset = 'dataworks-356fa'
source = 'gs://dataworks-356fa-backups/pullnupload.json'
def load_data_from_gcs(dataset, test10, source ):
bigquery_client = bigquery.Client(dataset)
dataset = bigquery_client.dataset('FirebaseArchive')
table = dataset.table(test10)
job_name = str(uuid.uuid4())
job= bigquery_client.load_table_from_storage(
job_name, table, "gs://dataworks-356fa-backups/pullnupload.json")
job.source_format = 'NEWLINE_DELIMITED_JSON'
job.begin()
# wait_for_job(job)
print("state of job is: " + job.state)
# print("errors: " + job.errors)
load_data_from_gcs(dataset, 'test10', source)
当wait_for_job(job)
行没有被注释掉时,我会收到这个错误
Traceback (most recent call last):
File "cloudtobq.py", line 42, in <module>
load_data_from_gcs(dataset, 'test10', source)
File "cloudtobq.py", line 38, in load_data_from_gcs
wait_for_job(job)
NameError: global name 'wait_for_job' is not defined
当print("errors: " + job.errors)
未被注释掉时,我会收到此错误。
Traceback (most recent call last):
File "cloudtobq.py", line 42, in <module>
load_data_from_gcs(dataset, 'test10', source)
File "cloudtobq.py", line 40, in load_data_from_gcs
print("errors: " + job.errors)
TypeError: cannot concatenate 'str' and 'NoneType' objects
当两者都被注释掉时,这就是我收到的内容,然后返回到原始终端屏幕。
Wess-MacBook-Pro:desktop wesstephens$ python cloudtobq.py
state of job is: RUNNING
Wess-MacBook-Pro:desktop wesstephens$
【问题讨论】:
【参考方案1】:你需要包含函数from the documentation sample code的定义:
def wait_for_job(job):
while True:
job.reload()
if job.state == 'DONE':
if job.error_result:
raise RuntimeError(job.errors)
return
time.sleep(1)
您不需要打印job.errors
,因为如果作业不成功,wait_for_job
会引发异常。
【讨论】:
以上是关于无法使用 python 将 JSON 文件从谷歌云存储加载到 bigquery的主要内容,如果未能解决你的问题,请参考以下文章