初始化具有相关实体的数据存储的最佳方法(Best way to initialize datastore w/ related entities)

编程入门 行业动态 更新时间:2024-10-19 06:18:51
初始化具有相关实体的数据存储的最佳方法(Best way to initialize datastore w/ related entities)

要运行我的应用程序,我需要一些静态实体,

所以我决定通过远程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: 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?

最满意答案

如果您定义键值(作为字符串),则可以使用这些值创建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 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])

更多推荐

本文发布于:2023-08-07 02:12:00,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1459028.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:初始化   实体   数据存储   方法   related

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!