【发布时间】:2011-08-27 03:01:38
【问题描述】:
我现在已经花了至少两个小时试图让它发挥作用。我在 SO 和 Google 群组中看到了很多不同的问题,但似乎没有一个答案对我有用。
问题:如何将下面 CSV 文件中的数据批量上传到数据存储区,以创建在 CSV 文件中定义了 key_name 的实体(与使用下面的 add 函数的结果相同)。
这是我的模型:
class RegisteredDomain(db.Model):
"""
Domain object class. It has no fields because it's existence is
proof that it has been registered. Indivdual registered domains
can be found using keys.
"""
pass
这是我通常如何添加/删除域等:
def add(domains):
"""
Add domains. This functions accepts a single domain string or a
list of domain strings and adds them to the database. The domain(s)
must be valid unicode strings (a ValueError is thrown if the domain
strings are not valid.
"""
if not isinstance(domains, list):
domains = [domains]
cleaned_domains = []
for domain in domains:
clean_domain_ = clean_domain(domain)
is_valid_domain(clean_domain_)
cleaned_domains.append(clean_domain_)
domains = cleaned_domains
db.put([RegisteredDomain(key_name=make_key(domain)) for domain in domains])
def get(domains):
"""
Get domains. This function accepts a single domain string or a list
of domain strings and queries the database for them. It returns a
dictionary containing the domain name and RegisteredDomain object or
None if the entity was not found.
"""
if not isinstance(domains, list):
domains = [domains]
entities = db.get([Key.from_path('RegisteredDomain', make_key(domain)) for domain in domains])
return dict(zip(domains, entities))
注意:在上面的代码中,make_key 只是将域小写并在前面加上一个 'd'。
就是这样。现在我正疯狂地尝试从 CSV 文件上传一些 RegisteredDomain 实体。这是 CSV 文件(注意第一个字符 'd' 是因为键名可能不以数字开头):
key
dgoogle.com
dgoogle11.com
dfacebook.com
dcool.com
duuuuuuu.com
dsdsdsds.com
dffffooo.com
dgmail.com
我无法自动生成 bulkloader yaml 文件,因为应用引擎仍未更新我的数据存储区统计信息(1 天加上几个小时)。所以这个(以及许多类似的排列)是我想出的(主要是改变 import_transform 位):
python_preamble:
- import: google.appengine.ext.bulkload.transform
- import: google.appengine.api.datastore
- import: google.appengine.ext.db
- import: utils
- import: bulk_helper
transformers:
- kind: RegisteredDomain
connector: csv
connector_options:
encoding: utf-8
property_map:
- property: __key__
external_name: key
export_transform: bulk_helper.key_to_reverse_str
import_template: transform.create_foreign_key('RegisteredDomain')
现在由于某种原因,当我尝试上传它时说一切正常并且 x 实体已被传输等,但数据存储中没有任何更新(正如我从管理控制台中看到的那样)。这是我上传的方式:
appcfg.py upload_data --application=domain-sandwich --kind=RegisteredDomain --config_file=bulk.yaml --url=http://domain-sandwich.appspot.com/remote_api --filename=data.csv
最后这就是我的数据存储查看器的样子:
注意:我在 dev-server 和 appengine 上都这样做(无论什么都有效...)。
感谢您的帮助!
【问题讨论】:
标签: google-app-engine google-cloud-datastore