我知道我应该有一个代码,但我还没有任何有用的东西。
我的 GCS gs://path/listings_all.json 上有 ~300GB JSON 文件,最终我试图将它导入 BigQuery,但它有一些错误数据结构(我通过 MongoDB 的 mongoexport 获取它)
invalid field name "$date". Fields must contain only letters, numbers, and underscores, start with a letter or underscore, and be at most 128 characters long
因此,现在我的方法是以某种方式从 GCS 处理它逐行读取源文件,并使用 python API 将每个处理过的行上传到 BigQuery。
下面是我从原始大文件中提取的 100 行样本放在一起进行测试的简单阅读器:
import json
from pprint import pprint
with open('schema_in_10.json') as f:
for line in f:
j_content = json.loads(line)
# print(j_content['id'], j_content['city'], j_content['country'], j_content['state'], j_content['country_code'], j_content['smart_location'], j_content['address'], j_content['market'], j_content['neighborhood'])
# // geo { lat, lng}'])
print('------')
pprint(j_content['is_location_exact'])
pprint(j_content['zipcode'])
pprint(j_content['name'])
能否请您帮助我了解如何使用 Python3 从 Google Cloud Storage 逐行读取或流式传输巨大的 JSON?
请您参考如下方法:
smart_open现在支持流式传输 GCS 文件。
from smart_open import open
# stream from GCS
with open('gs://my_bucket/my_file.txt') as fin:
for line in fin:
print(line)
# stream content *into* GCS (write mode):
with open('gs://my_bucket/my_file.txt', 'wb') as fout:
fout.write(b'hello world')
