Sure, here’s the translation: Hazelcast JSON对象查询

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

Hazelcast Json object querying

问题

以下是您要的翻译内容:

我将这些 JSON 对象存储在 Hazelcast 中:

IMap<String, HazelcastJsonValue> hazelcast = instance.getMap("get");

我正在存储的 JSON 如下:

{"id":"01","name":"abc","age":33}
{"id":"02","name":"data","age":37}
{"id":"03","name":"abc","age":39}

如果我只想选择年龄大于 35 的字段:
输出:

[37,39]

如何使用 projections 实现这一点?

英文:

I'M storing these JSON Objects the hazelcast

IMap&lt;String, HazelcastJsonValue&gt; hazelcast = instance.getMap(&quot;get&quot;);

JSON thta I'm storing

{&quot;id&quot;:&quot;01&quot;,&quot;name&quot;:&quot;abc&quot;,&quot;age&quot;:33}   

{&quot;id&quot;:&quot;02&quot;,&quot;name&quot;:&quot; data&quot;,&quot;age&quot;:37} 

{&quot;id&quot;:&quot;03&quot;,&quot;name&quot;:&quot;abc&quot;,&quot;age&quot;:39}

if i just want to select only age field with above 35
output:-

[37,39]

How to do this using projetions?

答案1

得分: 2

以下是翻译好的代码部分:

import com.hazelcast.core.*;
import com.hazelcast.projection.Projections;
import com.hazelcast.query.Predicates;
import org.junit.Test;

import java.util.Collection;

import static org.hamcrest.Matchers.containsInAnyOrder;
import static org.junit.Assert.assertThat;

[...]

@Test
public void testJsonProjection() {
  HazelcastInstance hz = Hazelcast.newHazelcastInstance();
  IMap<Integer, HazelcastJsonValue> map = instance.getMap("myMap");

  map.set(0, new HazelcastJsonValue("{\"id\":\"01\",\"name\":\"abc\",\"age\":33}"));
  map.set(1, new HazelcastJsonValue("{\"id\":\"02\",\"name\":\"data\",\"age\":37}"));
  map.set(2, new HazelcastJsonValue("{\"id\":\"03\",\"name\":\"abc\",\"age\":39}"));

  Collection<Long> projection = map.project(
    Projections.singleAttribute("age"),
    Predicates.greaterEqual("age", 35)
  );

  assertThat(projection, containsInAnyOrder(37L, 39L));
}
英文:

this works for me:

import com.hazelcast.core.*;
import com.hazelcast.projection.Projections;
import com.hazelcast.query.Predicates;
import org.junit.Test;

import java.util.Collection;

import static org.hamcrest.Matchers.containsInAnyOrder;
import static org.junit.Assert.assertThat;

[...]

@Test
public void testJsonProjection() {
  HazelcastInstance hz = Hazelcast.newHazelcastInstance();
  IMap&lt;Integer, HazelcastJsonValue&gt; map = instance.getMap(&quot;myMap&quot;);

  map.set(0, new HazelcastJsonValue(&quot;{\&quot;id\&quot;:\&quot;01\&quot;,\&quot;name\&quot;:\&quot;abc\&quot;,\&quot;age\&quot;:33}&quot;));
  map.set(1, new HazelcastJsonValue(&quot;{\&quot;id\&quot;:\&quot;02\&quot;,\&quot;name\&quot;:\&quot; data\&quot;,\&quot;age\&quot;:37} &quot;));
  map.set(2, new HazelcastJsonValue(&quot;{\&quot;id\&quot;:\&quot;03\&quot;,\&quot;name\&quot;:\&quot;abc\&quot;,\&quot;age\&quot;:39}&quot;));

  Collection&lt;Long&gt; projection = map.project(
    Projections.singleAttribute(&quot;age&quot;),
    Predicates.greaterEqual(&quot;age&quot;, 35)
  );

  assertThat(projection, containsInAnyOrder(37L, 39L));
}

huangapple
  • 本文由 发表于 2020年7月26日 03:26:49
  • 转载请务必保留本文链接:https://go.coder-hub.com/63092630.html
匿名

发表评论

匿名网友

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

确定