我在我的 Google Cloud Datastore 中使用了多个命名空间
运行时:
require "google/cloud/datastore"
project_id = "PROJECT_ID"
datastore = Google::Cloud::Datastore.new project: project_id
...
query = Google::Cloud::Datastore::Query.new
query.kind "Task"
tasks = datastore.run query
它只从“默认”命名空间中检索实体
例如,在 Python 中,您可以这样定义命名空间:
dataclient = datastore.Client("PROJECT_ID", "NAMESPACE")
但是我找不到用 Ruby 做的方法。而且我也没有在资源中找到任何有用的东西。
最佳答案
我已经就此询问过 Google,他们已向我发送了一个示例:
# Datastore service client
datastore = Google::Cloud::Datastore.new
# Create a new entry with a namespace
entry = datastore.entity "kind", namespace: "test-namespace" do |entry|
entry["name"] = "YOURNAME"
end
# Save in datastore
datastore.save entry
# Query a namespaced entry
query = Google::Cloud::Datastore::Query.new
query.kind "kind"
datastore.run query, namespace: "test-namespace"
关于ruby-on-rails - 如何为查询定义命名空间,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/43003157/