要运行我的应用程序,我需要一些静态实体,
所以我决定通过远程API从CSV文件上传它们w / bulkloader。
但我有一些实体,他们之间有关系。 喜欢:
- kind: Category properties: - name: name - kind: SubCategory ancestor: yes properties: - name: parent_id - name: name我应该如何创建一个csv数据来制作它? 是否还有其他方法可以用来初始化我的应用数据存储区?
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: nameHow should I create a csv data to make it? Is there any other way that I should take to init my app datastore?
最满意答案
如果您定义键值(作为字符串),则可以使用这些值创建csv文件。 一个文件将包含Category值category_key,name 。 另一个文件将包含SubCategory值subcategory_key,category_key,name 。 例如,
cat1,Category 1 cat2,Category 2 subcat1,cat1,SubCategory 1 subcat2,cat1,SubCategory 2 subcat3,cat2,SubCategory 3您可以逐行读取文件,并从数据中创建静态实体(在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])parent_id值构造为键。 两个循环都使用get_or_insert()函数来防止重复值,因此您可以多次运行它。
我看到SubCategory有一个祖先,所以你可以用这个替换最后一个调用(并删除parent_id属性):
SubCategory.get_or_insert(row[0], parent=ndb.Key(Category, row[1]), name=row[2])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 3You 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])更多推荐
发布评论