英文:
fetch hazelcast value by key
问题
public class Model implements Serializable{
/**
*
*/
private static final long serialVersionUID = 1L;
private String id;
private String name;
private String age;
Hazelcast 数据存储
HazelcastInstance hazelCast = Hazelcast.newHazelcastInstance();
IMap<Model, String> hazelInstance = hazelCast.getMap("data");
hazelInstance.put(new Model("001","alia","23"), "john");
如何通过传递任意键获取值(John)。
在Hazelcast中是否有可能实现?
<details>
<summary>英文:</summary>
public class Model implements Serializable{
/**
*
*/
private static final long serialVersionUID = 1L;
private String id;
private String name;
private String age;
Hazelcast Data-Storage
HazelcastInstance hazelCast = Hazelcast.newHazelcastInstance();
IMap<Model, String> hazelInstance = hazelCast.getMap("data");
hazelInstance.put(new Model("001","alia","23"), "john");
how to fetch value(John) by passing the any key..
Is that possible in hazelcast..?
</details>
# 答案1
**得分**: 0
如果您是指通过地图键的属性获取值,我认为您应该使用IMap.values(Predicate)方法:
```java
@Test
public void testFetchValue() {
IMap<Model, String> map = hazelcastTestClient.getMap("data");
Model key = new Model("001","alia","23");
map.put(key, "john");
Model key2 = new Model("002","alia2","25");
map.put(key2, "eric");
Model key3 = new Model("003","alia3","23");
map.put(key3, "peter");
String value = map.get(key);
Assert.assertTrue("value should be john", "john".equals(value));
EntryObject eo = new PredicateBuilder().getEntryObject();
Predicate idPredicate = eo.key().get("id").equal("001");
Collection<String> valuesForName = map.values(idPredicate);
Assert.assertTrue("values should contain john", valuesForName.contains("john"));
Assert.assertFalse("values should not contain peter", valuesForName.contains("peter"));
Assert.assertFalse("values should not contain eric", valuesForName.contains("eric"));
eo = new PredicateBuilder().getEntryObject();
Predicate agePredicate = eo.key().get("age").equal("23");
Collection<String> valuesForAge = map.values(agePredicate);
Assert.assertTrue("values should contain john", valuesForAge.contains("john"));
Assert.assertTrue("values should contain peter", valuesForAge.contains("peter"));
Assert.assertFalse("values should not contain eric", valuesForAge.contains("eric"));
}
英文:
If you mean fetch values by attributes of the map key, I think you should use IMap.values(Predicate) :
@Test
public void testFetchValue() {
IMap<Model, String> map = hazelcastTestClient.getMap("data");
Model key = new Model("001","alia","23");
map.put(key, "john");
Model key2 = new Model("002","alia2","25");
map.put(key2, "eric");
Model key3 = new Model("003","alia3","23");
map.put(key3, "peter");
String value = map.get(key);
Assert.assertTrue("value should be john", "john".equals(value));
EntryObject eo = new PredicateBuilder().getEntryObject();
Predicate idPredicate = eo.key().get("id").equal("001");
Collection<String> valuesForName = map.values(idPredicate);
Assert.assertTrue("values should contain john", valuesForName.contains("john"));
Assert.assertFalse("values should not contain peter", valuesForName.contains("peter"));
Assert.assertFalse("values should not contain eric", valuesForName.contains("eric"));
eo = new PredicateBuilder().getEntryObject();
Predicate agePredicate = eo.key().get("age").equal("23");
Collection<String> valuesForAge = map.values(agePredicate);
Assert.assertTrue("values should contain john", valuesForAge.contains("john"));
Assert.assertTrue("values should contain peter", valuesForAge.contains("peter"));
Assert.assertFalse("values should not contain eric", valuesForAge.contains("eric"));
}
专注分享java语言的经验与见解,让所有开发者获益!
评论