此内容没有您所选择的语言版本。

Chapter 6. Querying Caches with Protobuf Metadata


Data Grid supports using Protocol Buffers (Protobuf) to structure data in the cache so that you can query it.

Prerequisites

  • Start the Data Grid CLI.
  • Connect to a running Data Grid cluster.

6.1. Configuring Media Types

Encode cache entries with different media types to store data in a format that best suits your requirements.

For example, the following procedure shows you how to configure the application/x-protostream media type.

Procedure

  1. Create a Data Grid configuration file that adds a distributed cache named qcache and configures the media type, for example:

    <infinispan>
       <cache-container>
          <distributed-cache name="qcache">
             <encoding>
                <key media-type="application/x-protostream"/>
                <value media-type="application/x-protostream"/>
             </encoding>
          </distributed-cache>
       </cache-container>
    </infinispan>
    Copy to Clipboard Toggle word wrap
  2. Create qcache from pcache.xml with the --file= option.

    [//containers/default]> create cache --file=pcache.xml pcache
    Copy to Clipboard Toggle word wrap
  3. Verify pcache.

    [//containers/default]> ls caches
    pcache
    ___protobuf_metadata
    [//containers/default]> describe caches/pcache
    {
      "distributed-cache" : {
        "mode" : "SYNC",
        "encoding" : {
          "key" : {
            "media-type" : "application/x-protostream"
          },
          "value" : {
            "media-type" : "application/x-protostream"
          }
        },
        "transaction" : {
          "mode" : "NONE"
        }
      }
    }
    Copy to Clipboard Toggle word wrap
  4. Add an entry to pcache and check the encoding.

    [//containers/default]> put --cache=pcache good morning
    [//containers/default]> cd caches/pcache
    [//containers/default/caches/pcache]> get good
    {
      "_type" : "string",
      "_value" : "morning"
    }
    Copy to Clipboard Toggle word wrap

6.2. Registering Protobuf Schemas

Protobuf schemas contain data structures known as messages in .proto definition files.

Procedure

  1. Create a schema file named person.proto with the following messages:

    package org.infinispan.rest.search.entity;
    
    message Address {
        required string street = 1;
        required string postCode = 2;
    }
    
    message PhoneNumber {
        required string number = 1;
    }
    
    message Person {
        optional int32 id = 1;
        required string name = 2;
        required string surname = 3;
        optional Address address = 4;
        repeated PhoneNumber phoneNumbers = 5;
        optional uint32 age = 6;
        enum Gender {
            MALE = 0;
            FEMALE = 1;
        }
    
        optional Gender gender = 7;
    }
    Copy to Clipboard Toggle word wrap
  2. Register person.proto.

    [//containers/default]> schema --upload=person.proto person.proto
    Copy to Clipboard Toggle word wrap
  3. Verify person.proto.

    [//containers/default]> cd caches/___protobuf_metadata
    [//containers/default/caches/___protobuf_metadata]> ls
    person.proto
    [//containers/default/caches/___protobuf_metadata]> get person.proto
    Copy to Clipboard Toggle word wrap

6.3. Querying Caches with Protobuf Schemas

Data Grid automatically converts JSON to Protobuf so that you can read and write cache entries in JSON format and use Protobuf schemas to query them.

For example, consider the following JSON documents:

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

Each of the preceding JSON documents contains:

  • a _type field that identifies the Protobuf message to which the JSON document corresponds.
  • several fields that correspond to datatypes in the person.proto schema.

Procedure

  1. Navigate to the pcache cache.

    [//containers/default/caches]> cd pcache
    Copy to Clipboard Toggle word wrap
  2. Add each JSON document as an entry to the cache, for example:

    [//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. Verify that the entries exist.

    [//containers/default/caches/pcache]> ls
    lukecage
    matthewmurdock
    jessicajones
    Copy to Clipboard Toggle word wrap
  4. Query the cache to return entries from the Protobuf Person entity where the gender datatype is 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