Sure, here’s the translation: Hazelcast 获取操作

huangapple go评论62阅读模式
英文:

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&lt;tblHeaders, HazelcastJsonValue&gt; person = hazelcastInstance.getMap(&quot;person&quot;);

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

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(&quot;school_id=&#39;0001&#39;&quot;);
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(&quot;__key.school_id&quot;, &quot;1&quot;);
Collection&lt;HazelcastJsonValue&gt; json = person.values(schoolPredicate);
System.out.println(json); //get the value here

Giving the output

[{ &quot;name3&quot;:&quot;John3&quot; }, { &quot;name1&quot;:&quot;John1&quot; }, { &quot;name2&quot;:&quot;John2&quot; }]

huangapple
  • 本文由 发表于 2020年8月19日 16:33:50
  • 转载请务必保留本文链接:https://go.coder-hub.com/63483053.html
匿名

发表评论

匿名网友

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

确定