6.3. 使用 Protobuf Schemas 查询缓存


Data Grid 会自动将 JSON 转换为 Protobuf,以便您可以使用 JSON 格式读取和写入缓存条目,并使用 Protobuf 模式查询它们。

例如,请考虑以下 JSON 文档:

lukecage.json

{
  "_type":"org.infinispan.rest.search.entity.Person",
  "id":2,
  "name":"Luke",
  "surname":"Cage",
  "gender":"MALE",
  "address":{"street":"38th St","postCode":"NY 11221"},
  "phoneNumbers":[{"number":4444},{"number":5555}]
}
Copy to Clipboard Toggle word wrap

jessicajones.json

{
  "_type":"org.infinispan.rest.search.entity.Person",
  "id":1,
  "name":"Jessica",
  "surname":"Jones",
  "gender":"FEMALE",
  "address":{"street":"46th St","postCode":"NY 10036"},
  "phoneNumbers":[{"number":1111},{"number":2222},{"number":3333}]
}
Copy to Clipboard Toggle word wrap

matthewmurdock.json

{
  "_type":"org.infinispan.rest.search.entity.Person",
  "id":3,
  "name":"Matthew",
  "surname":"Murdock",
  "gender":"MALE",
  "address":{"street":"57th St","postCode":"NY 10019"},
  "phoneNumbers":[]
}
Copy to Clipboard Toggle word wrap

前面的每个 JSON 文档都包含:

  • 标识 JSON 文档对应的 Protobuf 消息的 _type 字段。
  • person.proto 模式中对应于 datatypes 的几个字段。

流程

  1. 进入 pcache 缓存。

    [//containers/default/caches]> cd pcache
    Copy to Clipboard Toggle word wrap
  2. 将每个 JSON 文档作为条目添加到缓存中,例如:

    [//containers/default/caches/pcache]> put --encoding=application/json --file=jessicajones.json jessicajones
    [//containers/default/caches/pcache]> put --encoding=application/json --file=matthewmurdock.json matthewmurdock
    [//containers/default/caches/pcache]> put --encoding=application/json --file=lukecage.json lukecage
    Copy to Clipboard Toggle word wrap
  3. 验证条目是否存在。

    [//containers/default/caches/pcache]> ls
    lukecage
    matthewmurdock
    jessicajones
    Copy to Clipboard Toggle word wrap
  4. 查询缓存,从 Protobuf Person 实体(其中 gender datatype 为 MALE )返回条目。

    [//containers/default/caches/pcache]> query "from org.infinispan.rest.search.entity.Person p where p.gender = 'MALE'"
    {
      "total_results" : 2,
      "hits" : [ {
        "hit" : {
          "_type" : "org.infinispan.rest.search.entity.Person",
          "id" : 2,
          "name" : "Luke",
          "surname" : "Cage",
          "gender" : "MALE",
          "address" : {
            "street" : "38th St",
            "postCode" : "NY 11221"
          },
          "phoneNumbers" : [ {
            "number" : "4444"
          }, {
            "number" : "5555"
          } ]
        }
      }, {
        "hit" : {
          "_type" : "org.infinispan.rest.search.entity.Person",
          "id" : 3,
          "name" : "Matthew",
          "surname" : "Murdock",
          "gender" : "MALE",
          "address" : {
            "street" : "57th St",
            "postCode" : "NY 10019"
          }
        }
      } ]
    }
    Copy to Clipboard Toggle word wrap
返回顶部
Red Hat logoGithubredditYoutubeTwitter

学习

尝试、购买和销售

社区

关于红帽文档

通过我们的产品和服务,以及可以信赖的内容,帮助红帽用户创新并实现他们的目标。 了解我们当前的更新.

让开源更具包容性

红帽致力于替换我们的代码、文档和 Web 属性中存在问题的语言。欲了解更多详情,请参阅红帽博客.

關於紅帽

我们提供强化的解决方案,使企业能够更轻松地跨平台和环境(从核心数据中心到网络边缘)工作。

Theme

© 2025 Red Hat