簡體   English   中英

按鍵獲取hazelcast值

[英]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");

如何通過傳遞任意鍵來獲取值(約翰)..

在榛樹中這可能嗎..?

如果您的意思是通過 map 鍵的屬性獲取值,我認為您應該使用 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"));
}

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM