获取Hazelcast中的键对应的值

huangapple 未分类评论45阅读模式
英文:

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&lt;Model, String&gt; map = hazelcastTestClient.getMap(&quot;data&quot;);
	Model key = new Model(&quot;001&quot;,&quot;alia&quot;,&quot;23&quot;);
	map.put(key, &quot;john&quot;);
	Model key2 = new Model(&quot;002&quot;,&quot;alia2&quot;,&quot;25&quot;);
	map.put(key2, &quot;eric&quot;);
	Model key3 = new Model(&quot;003&quot;,&quot;alia3&quot;,&quot;23&quot;);
	map.put(key3, &quot;peter&quot;);
	
	String value = map.get(key);
	Assert.assertTrue(&quot;value should be john&quot;, &quot;john&quot;.equals(value));
	
	EntryObject eo = new PredicateBuilder().getEntryObject();
	Predicate idPredicate = eo.key().get(&quot;id&quot;).equal(&quot;001&quot;);
	Collection&lt;String&gt; valuesForName = map.values(idPredicate);
	Assert.assertTrue(&quot;values should contain john&quot;, valuesForName.contains(&quot;john&quot;));
	Assert.assertFalse(&quot;values should not contain peter&quot;, valuesForName.contains(&quot;peter&quot;));
	Assert.assertFalse(&quot;values should not contain eric&quot;, valuesForName.contains(&quot;eric&quot;));
	
	eo = new PredicateBuilder().getEntryObject();
	Predicate agePredicate = eo.key().get(&quot;age&quot;).equal(&quot;23&quot;);
	Collection&lt;String&gt; valuesForAge = map.values(agePredicate);
	Assert.assertTrue(&quot;values should contain john&quot;, valuesForAge.contains(&quot;john&quot;));
	Assert.assertTrue(&quot;values should contain peter&quot;, valuesForAge.contains(&quot;peter&quot;));
	Assert.assertFalse(&quot;values should not contain eric&quot;, valuesForAge.contains(&quot;eric&quot;));
}

huangapple
  • 本文由 发表于 2020年7月23日 11:58:33
  • 转载请务必保留本文链接:https://java.coder-hub.com/63046679.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定