英文:
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 "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...?
答案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<User> users = map.values(new SqlPredicate("value.fullName", "John"));
OR
Collection<User> usersWithGradeA = map.values(Predicates.equal("value.fullName", "John"));
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论