【问题标题】:How to read from Datastore using an Ancestor query and latest golang libraries如何使用 Ancestor 查询和最新的 golang 库从 Datastore 中读取数据
【发布时间】:2021-02-23 19:06:08
【问题描述】:

我想从一种数据存储类型中读取所有实体(大约 6 个实体/记录)。

我有一个数据存储区,该数据存储区键入了我想要理解的奇怪类型。我在执行查询的键上找不到任何唯一性。 The table looks like this: GCP Datastore representing data I want to read into my Go app

When I click on a record, it looks like this: Key literal exposed and used from here on out to try and get the records in the Go app

``我可以像这样在控制台中执行祖先查询:``` GCP Datastore queried using Ancestor query

太棒了!所以现在我想从我的 Golang 应用程序中检索这些数据?但是怎么做? 我在网上看到很多关于使用q.Get(...) // where q is a *Query struct 的解决方案 这些解决方案中的任何一个都不起作用,因为它们导入了google.golang.org/appengine/datastore。我知道这是遗留问题并且已被弃用。所以我想要一个导入cloud.google.com/go/datastore的解决方案。

我尝试了一些类似的方法,但运气不佳: First try using GetAll and query

接下来我尝试了这个: Second try attempting to use ancestor query... not ready yet

最后我尝试直接获取一条记录: Lastly I tried to get the record directly

在所有情况下,我的err 不为零,应该从数据存储查询填充的dts 也为零。

有任何指导可以帮助我了解如何查询此键类型吗?我是否错过了该表的键控和查询方式的一些基本内容?

谢谢

然后我尝试了这个:

【问题讨论】:

  • 请编辑您的帖子,包括代码而不是图片。

标签: go google-cloud-platform google-cloud-datastore deprecated ancestor


【解决方案1】:

看来你只是缺少你的命名空间

// Merchant Struct
type MerchantDetails struct {
    MEID  string
    LinkTo *datastore.Key
    Title string
}

// Struct array to store in
var tokens []MerchantDetails

// Ancestor Key to filter by
parentKey := datastore.NameKey("A1_1113", "activate", nil)
parentKey.Namespace = "Devs1"

// The call using the new datastore UI. Basically query.Run(), but datastore.GetAll()
keys, err := helpers.DatastoreClient.GetAll(
    helpers.Ctx,
    datastore.NewQuery("A1_1112").Ancestor(parentKey).Namespace("Devs1"),
    &tokens,
)
if err != nil {
 return "", err
}

// Print all name/id from the found values
fmt.Printf("keys: %v", keys)

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2015-06-04
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2021-09-02
    相关资源
    最近更新 更多