Ce contenu n'est pas disponible dans la langue sélectionnée.
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 Copier lienLien copié sur presse-papiers!
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
Create a Data Grid configuration file that adds a distributed cache named
qcacheand configures the media type, for example:Copy to Clipboard Copied! Toggle word wrap Toggle overflow Create
qcachefrompcache.xmlwith the--file=option.[//containers/default]> create cache --file=pcache.xml pcache
[//containers/default]> create cache --file=pcache.xml pcacheCopy to Clipboard Copied! Toggle word wrap Toggle overflow Verify
pcache.Copy to Clipboard Copied! Toggle word wrap Toggle overflow Add an entry to
pcacheand check the encoding.Copy to Clipboard Copied! Toggle word wrap Toggle overflow
6.2. Registering Protobuf Schemas Copier lienLien copié sur presse-papiers!
Protobuf schemas contain data structures known as messages in .proto definition files.
Procedure
Create a schema file named
person.protowith the following messages:Copy to Clipboard Copied! Toggle word wrap Toggle overflow Register
person.proto.[//containers/default]> schema --upload=person.proto person.proto
[//containers/default]> schema --upload=person.proto person.protoCopy to Clipboard Copied! Toggle word wrap Toggle overflow 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
[//containers/default]> cd caches/___protobuf_metadata [//containers/default/caches/___protobuf_metadata]> ls person.proto [//containers/default/caches/___protobuf_metadata]> get person.protoCopy to Clipboard Copied! Toggle word wrap Toggle overflow
6.3. Querying Caches with Protobuf Schemas Copier lienLien copié sur presse-papiers!
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
jessicajones.json
matthewmurdock.json
Each of the preceding JSON documents contains:
-
A
_typefield that identifies the Protobuf message to which the JSON document corresponds. -
Several fields that correspond to datatypes in the
person.protoschema.
Procedure
Navigate to the
pcachecache.[//containers/default/caches]> cd pcache
[//containers/default/caches]> cd pcacheCopy to Clipboard Copied! Toggle word wrap Toggle overflow 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
[//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 lukecageCopy to Clipboard Copied! Toggle word wrap Toggle overflow Verify that the entries exist.
[//containers/default/caches/pcache]> ls lukecage matthewmurdock jessicajones
[//containers/default/caches/pcache]> ls lukecage matthewmurdock jessicajonesCopy to Clipboard Copied! Toggle word wrap Toggle overflow Query the cache to return entries from the Protobuf
Personentity where the gender datatype isMALE.Copy to Clipboard Copied! Toggle word wrap Toggle overflow