Hazelcast获得操作

问题描述

通过按键获取hazelcast值...

        IMap<tblHeaders,HazelcastJsonValue> person = hazelcastInstance.getMap("person");

        person.put(new tblHeaders("1","ram","0001"),new HazelcastJsonValue("{ \"name1\":\"John1\" }"));
        person.put(new tblHeaders("1","vikas","0002"),new HazelcastJsonValue("{ \"name2\":\"John2\" }"));
        person.put(new tblHeaders("1","datrs","0003"),new HazelcastJsonValue("{ \"name3\":\"John3\" }"));

模型类

public class tblHeaders implements Serializable{  
    /**
     * 
     */
    private static final long serialVersionUID = 1L;

    private String school_id;
    private String name;
    private String unique_id;

此处如何通过传递单个键来获取值... 例子

HazelcastJsonValue json = person.get("school_id='0001'");
System.out.println(json.toString()); //get the value here

解决方法

这里有几件事。

  • IMap.get()方法只能通过完整的键值进行检索;由于您只提供了部分密钥,因此get()方法将不匹配任何内容。
  • IMap.values()方法采用Predicate参数,是尝试根据键的部分内容或条目值的全部或部分内容进行匹配的查询时使用的正确方法。 / li>
  • 默认情况下,将对值应用谓词,但是您可以通过在谓词的属性字段中使用关键字__key(两个下划线)将其应用于键。
  • 由于查询可能(并且确实)匹配多个项目,所以正确的返回类型是HazelcastJsonValue的集合。

以下代码将执行您要尝试的操作:

Predicate schoolPredicate = Predicates.equal("__key.school_id","1");
Collection<HazelcastJsonValue> json = person.values(schoolPredicate);
System.out.println(json); //get the value here

提供输出

[{ "name3":"John3" },{ "name1":"John1" },{ "name2":"John2" }]