我有一个很大的 CSV 文件,大约 1 GB,我想在数据存储中创建实体,每行一个实体。
该 CSV 文件当前位于 Google Cloud Storage 中。有没有一种干净的方法可以做到这一点?我可以在网上找到的所有示例似乎都依赖于本地的 CSV 文件,或者看起来它们的扩展性不太好。理想情况下,有一个流式 API 可以让我从 Cloud Storage 中读取足够小的片段,以便对 Datastore 进行更新调用,但我还没有找到类似的东西。
最佳答案
打开 GCS 文件时接收到的缓冲区是流式缓冲区,可以 pickle。但是 GCS 不支持迭代器协议(protocol)来读取 CSV 的行。你必须 write your own wrapper .喜欢:
with gcs.open('/app_default_bucket/csv/example.csv', 'r') as f:
csv_reader = csv.reader(iter(f.readline, ''))
for row in csv_reader:
logging.info(' - '.join(row))
如果您熟悉 blobstore,您可以使用它来使用 blobstore.create_gs_key( "/gs" + <gcs_file_name_here>)
从 GCS 读取大型 CSV .
示例 here
关于python - 将大型 CSV 从 Cloud Storage 导入 App Engine Datastore,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/30949385/