manu
manu

Reputation: 169

Querying in the Hazelcast

How to query json objects for this particular data-structure.

Here is my complete code.

package com.rest.ser;

import com.hazelcast.core.Hazelcast;
import com.hazelcast.core.HazelcastInstance;
import com.hazelcast.core.HazelcastJsonValue;
import com.hazelcast.core.IMap;
import com.hazelcast.query.SqlPredicate;

import java.io.ByteArrayInputStream;
import java.io.ByteArrayOutputStream;
import java.io.IOException;
import java.io.ObjectInputStream;
import java.io.ObjectOutputStream;
import java.io.Serializable;
import java.util.Collection;

    
    public class User implements Serializable {

        private static final long serialVersionUID = 1L;
        private String id;
        private String name;
        private HazelcastJsonValue value;

        public User(String id, String name, HazelcastJsonValue value) {
            this.id = id;
            this.name = name;
            this.value = value;
        }
        
        private void writeObject(ObjectOutputStream out)
                throws IOException {
            out.writeObject(id);
            out.writeObject(name);
            out.writeObject(value.toString());
        }
        
        @Override
        public String toString() {
            return "User [id=" + id + ", name=" + name + ", value=" + value + "]";
        }

        private void readObject(ObjectInputStream in)
                throws IOException, ClassNotFoundException {
            id = (String) in.readObject();
            name = (String) in.readObject();
            value = new HazelcastJsonValue((String) in.readObject());
        }
    

    public static void main(String[] args)
            throws IOException, ClassNotFoundException {
        System.out.println("starts here");
        ByteArrayOutputStream baos = new ByteArrayOutputStream();
        ObjectOutputStream out = new ObjectOutputStream(baos);
        out.writeObject(new User("1", "name", new HazelcastJsonValue("{ \"fullName\":\"John\" }\r\n" + 
                "")));
        out.flush();
        
        User user = (User) new ObjectInputStream(new ByteArrayInputStream(baos.toByteArray())).readObject();
        HazelcastInstance instance = Hazelcast.newHazelcastInstance();

        IMap<String, User> map = instance.getMap("test_mapstore");
        map.put("1", user);
        Collection<User> output = map.values(new SqlPredicate("id='1' and name='name'"));
        System.out.println(output.toString());
        //output I'm getting [User [id=1, name=name, value={ "fullName":"John" }]]

    }
}

So if i want fetch id and name by querying the HazelcastJsonValue means how to do that

value.fullName='John'

value is the model class field Name and fullName is the Json object field...

Is it possible to query in this manner...?

Upvotes: 1

Views: 206

Answers (1)

Govind
Govind

Reputation: 439

Can you try this?

Collection<User> users = map.values(new SqlPredicate("value.fullName", "John"));

OR

Collection<User> usersWithGradeA = map.values(Predicates.equal("value.fullName", "John"));

Upvotes: 1

Related Questions