gpt4 book ai didi

python - 通过 gcloud api 中的键查询 google 数据存储

转载 作者:行者123 更新时间:2023-11-30 22:58:47 25 4
gpt4 key购买 nike

我正在尝试使用我刚刚发现的 gcloud API 查询一些数据。我想查询 KeyPropery。例如:

from google.appengine.ext import ndb

class User(ndb.Model):
email = ndb.StringProperty()

class Data(ndb.Model):
user = ndb.KeyProperty('User')
data = ndb.JsonProperty()

在 GAE 中,假设我有用户 key ,我可以很容易地查询:

user = User.query(User.email == 'me@domain.com').get()
data_records = Data.query(Data.user == user.key).fetch()

我想使用 gcloud 做类似的事情:

from gcloud import datastore

client = datastore.Client(project='my-project-id')
user_qry = client.query(kind='User')
user_qry.add_filter('email', '=', 'me@domain.com')
users = list(user_qry.fetch())
user = users[0]

data_qry = client.query(kind='Data')
data_qry.add_filter('user', '=', user.key) # This doesn't work ...
results = list(data_qry.fetch()) # results = []

查看 add_filter 的文档,Entity.key 似乎不是 supported type :

value (int, str, bool, float, NoneType, :classdatetime.datetime) – The value to filter on.

是否可以为关键属性添加过滤器?

<小时/>

我做了更多的调查,试图弄清楚这里到底发生了什么。我不确定这对我目前理解这个问题是否有帮助,但也许对其他人会有帮助。

我模拟了各个库中的底层调用,以记录正在序列化并发送到服务器的 Protocol Buffer 。对于 GAE,它似乎是 datastore_query 中的 Batch.create_async模块。

对于 gcloud,它是 datastore.Client.connection.run_query方法。看看生成的 Protocol Buffer (匿名),我看到:

gcloud 查询 pb。

kind {
name: "Data"
}
filter {
composite_filter {
operator: AND
filter {
property_filter {
property {
name: "user"
}
operator: EQUAL
value {
key_value {
partition_id {
dataset_id: "s~app-id"
}
path_element {
kind: "User"
name: "user_string_id"
}
}
}
}
}
}
}

GAE 查询 pb。

kind: "Data"
Filter {
op: 5
property <
name: "User"
value <
ReferenceValue {
app: "s~app-id"
PathElement {
type: "User"
name: "user_string_id"
}
}
>
multiple: false
>
}

据我所知,这两个库使用不同版本的原型(prototype),但传递的数据看起来非常相似......

最佳答案

这是使用 ndb 库时的一个微妙错误:

All ndb properties accept a single positional argument that specifies the property's name in Datastore

查看模型定义,您会看到 user = ndb.KeyProperty('User') 。这实际上并不是说 user属性是 User 的关键实体,但它应该以属性名称 User 存储在数据存储中。您可以在 gae Protocol Buffer 查询中验证这一点,其中属性名称为(区分大小写)User .

如果您想将 key 限制为单一种类,则需要使用 kind 选项指定它。

user = ndb.KeyProperty(kind="User") 

KeyProperty还支持:

user = ndb.KeyProperty(User)   # User is a class here, not a string

Here is a description of all the magic .

现在,您的 gcloud 查询正在查询错误的用户,应该是:

data_qry = client.query(kind='Data')
data_qry.add_filter('User', '=', user.key)

关于python - 通过 gcloud api 中的键查询 google 数据存储,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/36047819/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com