Google App Engine NDB:如何存储文档结构? [英] Google App Engine NDB: How to store document structure?

查看:109
本文介绍了Google App Engine NDB:如何存储文档结构?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

从App Engine NDB 文档


NDB API在无模式对象
数据存储中提供持久存储。它支持自动缓存,复杂的查询和
原子事务。 NDB非常适合存储结构化数据
记录。

我想创建一个如下所示的使用NDB的结构,其中每个实例如下所示:

  {
city:'SFO'
date:'2013-01 -27'
数据:{
'关键字1':COUNT1,
'关键字2':COUNT2,
'KEYWORD3':共3个记录,
'keyword4':count4,
'keyword5':count5,
....
}
}

如何使用NDB在Google App Engine(GAE)中设计这样的无模式实体?

我是GAE的新手,不确定如何实现这一点。



谢谢

解决方案

如果您不需要查询数据可以使用性质中的一种作为由@voscausa提到:



JsonProperty

  class MyModel(ndb.Model):
city = ndb。 StringProperty()
date = ndb.DateProperty()
data = ndb.JsonProperty()
$ b $ my_model = MyModel(city =somewhere,
date = datetime .date.today(),
数据= { '关键字1':3,
'关键字2':5,
'KEYWORD3':1,})


$ b

StructuredProperty:

  class Data( ndb.Model):
keyword = ndb.StringProperty()
count = ndb.IntegerProperty()

class MyModel(ndb.Model):
city = ndb .StringProperty()
date = ndb.DateProperty()
data = ndb.StructuredProperty(Data,repeated = True)

my_model = MyModel(city =somewhere,
date = datetime.date.today(),
data = [Data(keyword =keyword1,count = 3),
Data(keyword =keyword2,count = 5),
Data(keyword =keyword3,count = 1)])
my_model.put()

这里的问题是过滤结构化属性。关键字的属性被视为平行数组。做一个查询,例如:

  q = MyModel.query(MyModel.data.keyword =='keyword1',
MyModel.data.count> 4)

会错误地包含 my_model



https: //developers.google.com/appengine/docs/python/ndb/queries#filtering_structured_properties



使用expando模型可以工作,并允许您查询关键字:

  class MyModel(ndb.Expando):
city = ndb.StringProperty()
date = ndb.DateProperty()

m = MyModel(city =Somewhere,date = datetime.date.today())
m.keyword1 = 3
m.keyword2 = 5
m.keyword3 = 1
m.put()

q = MyModel.query(ndb.GenericProperty('keyword1')> 2)

https://developers.google.com/appengine/docs/python/ndb/entities#expando


From App Engine NDB documentation:

The NDB API provides persistent storage in a schemaless object datastore. It supports automatic caching, sophisticated queries, and atomic transactions. NDB is well-suited to storing structured data records.

I want to create a structure like the following using NDB, where each instance looks like :

{
 city: 'SFO'
 date: '2013-01-27'
 data: {
           'keyword1': count1,
           'keyword2': count2,
           'keyword3': count3,
           'keyword4': count4,
           'keyword5': count5,
           ....
       }
}

How can I design such a schema-less entity in Google App Engine(GAE) using NDB?
I am new to GAE and not sure how to achieve this

Thank you

解决方案

If you don't need to query for the attributes in data you can use one of the properties as mentioned by @voscausa:

JsonProperty

class MyModel(ndb.Model):
  city = ndb.StringProperty()
  date = ndb.DateProperty()
  data = ndb.JsonProperty()

my_model = MyModel(city="somewhere", 
                   date=datetime.date.today(),
                   data={'keyword1': 3,
                         'keyword2': 5,
                         'keyword3': 1,})

StructuredProperty:

class Data(ndb.Model):
  keyword = ndb.StringProperty()
  count = ndb.IntegerProperty()

class MyModel(ndb.Model):
  city = ndb.StringProperty()
  date = ndb.DateProperty()
  data = ndb.StructuredProperty(Data, repeated=True)

my_model = MyModel(city="somewhere", 
                   date=datetime.date.today(),
                   data=[Data(keyword="keyword1", count=3),
                         Data(keyword="keyword2", count=5),
                         Data(keyword="keyword3", count=1)])
my_model.put()

The problem here is filtering for structured properties. The properties of Keyword are viewed as parallel arrays. Doing a query such as:

q = MyModel.query(MyModel.data.keyword=='keyword1',
                  MyModel.data.count > 4)

would incorrectly include my_model.

https://developers.google.com/appengine/docs/python/ndb/queries#filtering_structured_properties

Using an expando model would work and allow you to query for keywords:

class MyModel(ndb.Expando):
  city = ndb.StringProperty()
  date = ndb.DateProperty()

m = MyModel(city="Somewhere", date=datetime.date.today())
m.keyword1 = 3
m.keyword2 = 5
m.keyword3 = 1
m.put()

q = MyModel.query(ndb.GenericProperty('keyword1') > 2) 

https://developers.google.com/appengine/docs/python/ndb/entities#expando

这篇关于Google App Engine NDB:如何存储文档结构?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆