使用 Google Cloud Datastore 的 GQL 查询

GQL query with Google Cloud Datasore

Google Datastore Runquery 提供 http 客户端来执行 GQL 查询,如下所示:

POST https://datastore.googleapis.com/v1/projects/my-project-id:runQuery?key={YOUR_API_KEY}

{
 "gqlQuery": {
  "queryString": "SELECT * FROM User WHERE email = 'user@example.com'"
 }
}

但我收到如下错误响应:

{
  "error": {
    "code": 400,
    "message": "Disallowed literal: 'user@example.com'.",
    "status": "INVALID_ARGUMENT"
  }
}

但是,Google Cloud Datastore Http Client 可以完美地处理像 'Select * from User'.

这样的简单查询

那么,如何使用 Datastore Http 客户端执行此 GQL 查询?

您必须将 allowLiterals 参数设置为 true 或使用参数绑定(命名或位置)。

请参阅以下文档:

https://cloud.google.com/datastore/reference/rest/v1/projects/runQuery#GqlQuery

所以如果你想在查询中允许文字(常量值),你应该将请求更改为

{
 "gqlQuery": {
  "queryString": "SELECT * FROM User WHERE email = 'user@example.com'"
  "allowLiterals": true
 }
}