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:

    <distributed-cache name="pcache">
       <encoding>
          <key media-type="application/x-protostream"/>
          <value media-type="application/x-protostream"/>
       </encoding>
    </distributed-cache>
    Copy to Clipboard Toggle word wrap
  2. Create pcache 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
Back to top
Red Hat logoGithubredditYoutubeTwitter

Learn

Try, buy, & sell

Communities

About Red Hat Documentation

We help Red Hat users innovate and achieve their goals with our products and services with content they can trust. Explore our recent updates.

Making open source more inclusive

Red Hat is committed to replacing problematic language in our code, documentation, and web properties. For more details, see the Red Hat Blog.

About Red Hat

We deliver hardened solutions that make it easier for enterprises to work across platforms and environments, from the core datacenter to the network edge.

Theme

© 2025 Red Hat