【发布时间】:2017-10-05 20:35:16
【问题描述】:
我需要在 Google Cloud Storage 的存储桶中写入一个来自 MySQL 的 csv 格式的结果集。
按照here 的说明,我创建了以下示例代码:
import cloudstorage
from google.appengine.api import app_identity
import db # My own Mysql wrapper
dump = db.get_table_dump(schema) # Here I made a simples SQL SELECT and fetchall()
bucket_name = app_identity.get_default_gcs_bucket_name()
file_name = "/" + bucket_name + "/finalfiles/" + schema + "/" +"myfile.csv"
with cloudstorage.open(file_name, "w") as gcsFile:
gcsFile.write(dump)
它不起作用,因为write 需要一个字符串参数,而dump 是来自fetchall() 的元组结果。
我不能使用this approach(或类似的),因为我不能在GAE环境中写入文件,我也不能从像here这样的元组创建一个CSV字符串,因为我的结果集的大小(其实我试过了,时间太长,还没完成就超时了。
那么,我的问题是,从 MySQL 获取结果集并将其以 CSV 格式保存在 Google Cloud Storage Bucket 中的最佳方法是什么?
【问题讨论】:
-
你能写入内存文件吗?
-
对不起@JohannesReichard,但我不明白你的问题。我是GAE的新手。内存文件是我可以写入的吗?
-
我从未使用过 GAE,但 python 不仅支持写入硬盘驱动器上的文件,还支持写入内存中的对象之类的文件:docs.python.org/3/library/io。 html#内存中
-
感谢@JohannesReichard 的链接。我会看看它是否有效,但看起来很有希望。
标签: python mysql csv google-app-engine google-cloud-storage