英文:
Hazelcast get operation
问题
获取 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 hazelcast value by key...
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\" }"));
Model Class
public class tblHeaders implements Serializable{
/**
*
*/
private static final long serialVersionUID = 1L;
private String school_id;
private String name;
private String unique_id;
Here How to get the value by passing single key...
example
HazelcastJsonValue json = person.get("school_id='0001'");
System.out.println(json.toString()); //get the value here
答案1
得分: 1
这里有几点要注意的地方:
- IMap.get() 方法只能通过完整的键值来检索;由于你只提供了部分键值,get() 方法将找不到匹配项。
- IMap.values() 方法接受一个 Predicate 参数,是在尝试基于键的部分内容或条目的完整或部分内容进行匹配查询时使用的正确方法。
- 默认情况下,Predicate 会应用于值,但你可以通过在谓词的属性字段中使用关键字 __key(两个下划线)来指定应用于键。
- 由于查询可能(也确实)匹配多个项,正确的返回类型是 HazelcastJsonValue 的集合。
以下是可以实现你想要的功能的代码:
Predicate schoolPredicate = Predicates.equal("__key.school_id", "1");
Collection<HazelcastJsonValue> json = person.values(schoolPredicate);
System.out.println(json); // 在这里获取值
这将输出:
[{ "name3":"John3" }, { "name1":"John1" }, { "name2":"John2" }]
英文:
A few things here.
- The IMap.get() method can only retrieve by a full key value; since you are providing only a partial key the get() method will not match anything.
- The IMap.values() method takes a Predicate argument, and is the right method to use when trying to do a query that matches based on the partial content of the key or the full or partial content of the entry's value.
- By default, a Predicate is applied against the value, but you can specify that it apply to the key by using the keyword __key (two underscores) in the attribute field of the predicate.
- Since the query may (and does) match multiple items, the correct return type is a Collection of HazelcastJsonValue.
Here's the code that will do what you're trying:
Predicate schoolPredicate = Predicates.equal("__key.school_id", "1");
Collection<HazelcastJsonValue> json = person.values(schoolPredicate);
System.out.println(json); //get the value here
Giving the output
[{ "name3":"John3" }, { "name1":"John1" }, { "name2":"John2" }]
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论