I have spent at least two hours now trying to get this to work. I have seen quite a few different questions on SO and in the Google groups, but none of the answers seem to work for me.
Question: How do I bulk upload data as in the CSV file below to the datastore to create entities that have the key_name defined in the CSV file (same result as using the add function below).
This is my model:
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
Here is how I usually add/delete domains, etc:
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))
Note: in the above code make_key simply makes the domain lowercase and prepends a 'd'.
So there's that. Now I am going crazy trying to upload some RegisteredDomain entities from a CSV file. Here is the CSV file (note the first char 'd' is there because of the fact that a key name may not start with a number):
key
dgoogle.com
dgoogle11.com
dfacebook.com
dcool.com
duuuuuuu.com
dsdsdsds.com
dffffooo.com
dgmail.com
I have not been able to auto-generate the bulkloader yaml file because app engine has still not update my datastore stats (1 day plus a few hours). So this (and many similar permutations) is what I have come up with (mostly changing the import_transform bit):
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')
Now for some reason when I try to upload its says that all goes fine and x entities have been transferred etc, but nothing gets updated in the datastore (as I can see from the admin console). Here is how I upload:
appcfg.py upload_data --application=domain-sandwich --kind=RegisteredDomain --config_file=bulk.yaml --url=http://domain-sandwich.appspot.com/remote_api --filename=data.csv
And finally this is what my datastore viewer looks like:
Note: I am doing this both on the dev-server and on appengine (whatever works...).
Thanks for any help!