google-app-engine


Best way to initialize datastore w/ related entities


To run my app, I need some static entities,
so I've decided to upload them w/ bulkloader through Remote API from CSV files.
But I have some entities those have relationship in between.
Like:
- kind: Category
properties:
- name: name
- kind: SubCategory
ancestor: yes
properties:
- name: parent_id
- name: name
How should I create a csv data to make it?
Is there any other way that I should take to init my app datastore?
If you define the key values (as strings) then you can create csv files with those values. One file would contain the Category values category_key,name. The other file would contain the SubCategory values subcategory_key,category_key,name. For example,
cat1,Category 1
cat2,Category 2
subcat1,cat1,SubCategory 1
subcat2,cat1,SubCategory 2
subcat3,cat2,SubCategory 3
You can read the files line by line, and create the static entities from the data like this (in Python):
import csv
with open('categories.csv') as csvfile:
categories = csv.reader(csvfile)
for row in categories:
Category.get_or_insert(row[0], name=row[1])
with open('subcategories.csv') as csvfile:
subcategories = csv.reader(csvfile)
for row in subcategories:
SubCategory.get_or_insert(row[0], parent_id=ndb.Key(Category, row[1]), name=row[2])
The parent_id value is constructed as a key. Both loops use the get_or_insert() function to prevent duplicate values so you can run it multiple times.
I see that SubCategory has an ancestor, so you could replace the last call with this (and remove the parent_id attribute):
SubCategory.get_or_insert(row[0], parent=ndb.Key(Category, row[1]), name=row[2])

Related Links

Working Maven3 configuration for AppEngine + DataNucleus + JPA 2
Spring Security ACL on App Engine Datastore
JDO 1:N issue (retriving data)
Endpoints Bad Api Configuration
Resolving an invalid memory address or nil pointer dereference
Design suggestion for an app which will wake up periodically and execute some long running tasks
Properly using map[string]interface{}?
Multiple calls to put() method when updating ndb on google app engine
GAE Can't set up a Resident Instance, related to new Migrate Settings option?
Google App Engine - Querying for arrays containing a value
Java App engine backend shuts down abruptly, how to resume work?
Google Plus Domains Api HTTP Request - add people fails
Get entities containing specific value - objectify
Google AppEngine ClientId and Client Secrets
Which SDK Version Does Appengine Use in Production
can not figure out relation between yaml and main page handler in google app engine

Categories

HOME
jpa
hyperlink
gpu
character-encoding
angular2-material
camera
saml-2.0
rom
simulink
banner
retrofit2
fhir
startup
java.util.logging
esoteric-languages
aws-sdk
chargify
nim
windows-xp
amazon-quicksight
subclass
errbot
dotspatial
panoramas
r-grid
memory-address
sammy.js
modalviewcontroller
google-earth-engine
nohup
z-notation
loading
apache-stanbol
tasm
portforwarding
sharp-snmp
tcc
openresty
ntvs
robomongo
amq
tinkerpop
static-cast
excel-dna
reshape
libjpeg
cmp
ampps
renaming
taocp
chromium-os
adafruit
avspeechsynthesizer
qiime
nservicebus5
upx
cortex-m
fink
nupic
decimalformat
computer-architecture
microsoft-reporting
django-urls
sha512
osc
user-permissions
document-repository
execve
teaspoon
raygun
mintty
cancellation
spring-remoting
qscrollarea
code-duplication
ebay-lms
rolify
octal
boost-signals
cross-database
tmx
duplex
fail-fast-fail-early

Resources

Encrypt Message