6.3. Protobuf スキーマを使用したキャッシュのクエリー
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}] }
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}] }
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":[] }
前述の各 JSON ドキュメントには次が含まれます。
-
JSON ドキュメントが対応する Protobuf メッセージを識別する
_type
フィールド。 -
person.proto
スキーマのデータ型に対応するいくつかのフィールド。
手順
pcache
キャッシュに移動します。[//containers/default/caches]> cd pcache
各 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
エントリーが存在することを確認します。
[//containers/default/caches/pcache]> ls lukecage matthewmurdock jessicajones
キャッシュをクエリーして、性別データ型が
MALE
である ProtobufPerson
エンティティーからエントリーを返します。[//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" } } } ] }