이 콘텐츠는 선택한 언어로 제공되지 않습니다.

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

자세한 정보

평가판, 구매 및 판매

커뮤니티

Red Hat 문서 정보

Red Hat을 사용하는 고객은 신뢰할 수 있는 콘텐츠가 포함된 제품과 서비스를 통해 혁신하고 목표를 달성할 수 있습니다. 최신 업데이트를 확인하세요.

보다 포괄적 수용을 위한 오픈 소스 용어 교체

Red Hat은 코드, 문서, 웹 속성에서 문제가 있는 언어를 교체하기 위해 최선을 다하고 있습니다. 자세한 내용은 다음을 참조하세요.Red Hat 블로그.

Red Hat 소개

Red Hat은 기업이 핵심 데이터 센터에서 네트워크 에지에 이르기까지 플랫폼과 환경 전반에서 더 쉽게 작업할 수 있도록 강화된 솔루션을 제공합니다.

Theme

© 2026 Red Hat