英文:
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<String, HazelcastJsonValue> hazelcast = instance.getMap("get");
JSON thta I'm storing
{"id":"01","name":"abc","age":33}
{"id":"02","name":" data","age":37}
{"id":"03","name":"abc","age":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<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));
}
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论