在Hazelcast中查询

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

Querying in the Hazelcast

问题

以下是翻译好的内容:

如何查询这种特定数据结构的 JSON 对象。

这是我的完整代码。

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" }]]

    }
}

所以,如果我想通过查询 HazelcastJsonValue 来提取 id 和 name,应该如何操作?

value.fullName='John'

这里的 value 是模型类的字段名称,而 fullName 是 JSON 对象的字段名称。

是否可以用这种方式进行查询呢?

英文:

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 &quot;User [id=&quot; + id + &quot;, name=&quot; + name + &quot;, value=&quot; + value + &quot;]&quot;;
}
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(&quot;starts here&quot;);
ByteArrayOutputStream baos = new ByteArrayOutputStream();
ObjectOutputStream out = new ObjectOutputStream(baos);
out.writeObject(new User(&quot;1&quot;, &quot;name&quot;, new HazelcastJsonValue(&quot;{ \&quot;fullName\&quot;:\&quot;John\&quot; }\r\n&quot; + 
&quot;&quot;)));
out.flush();
User user = (User) new ObjectInputStream(new ByteArrayInputStream(baos.toByteArray())).readObject();
HazelcastInstance instance = Hazelcast.newHazelcastInstance();
IMap&lt;String, User&gt; map = instance.getMap(&quot;test_mapstore&quot;);
map.put(&quot;1&quot;, user);
Collection&lt;User&gt; output = map.values(new SqlPredicate(&quot;id=&#39;1&#39; and name=&#39;name&#39;&quot;));
System.out.println(output.toString());
//output I&#39;m getting [User [id=1, name=name, value={ &quot;fullName&quot;:&quot;John&quot; }]]
}
}

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

value.fullName=&#39;John&#39;

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

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

答案1

得分: 1

可以尝试这个吗?

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

或者

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

英文:

Can you try this?

Collection&lt;User&gt; users = map.values(new SqlPredicate(&quot;value.fullName&quot;, &quot;John&quot;));

OR

Collection&lt;User&gt; usersWithGradeA = map.values(Predicates.equal(&quot;value.fullName&quot;, &quot;John&quot;));

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

发表评论

匿名网友

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

确定