manu
manu

Reputation: 169

Hazelcast Json object querying

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?

Upvotes: 1

Views: 1245

Answers (1)

Jaromir Hamala
Jaromir Hamala

Reputation: 1890

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));
}

Upvotes: 2

Related Questions