6.3. 使用 Protobuf Schemas 查询缓存
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模式中对应于 datatypes 的几个字段。
流程
进入
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查询缓存,从 Protobuf
Person实体(其中 gender datatype 为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" } } } ] }