java - 在 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 对象字段...
可以这样查询吗……?
解决方案
你能试试这个吗?
Collection<User> users = map.values(new SqlPredicate("value.fullName", "John"));
或者
Collection<User> usersWithGradeA = map.values(Predicates.equal("value.fullName", "John"));
推荐阅读
- r - 如何在闪亮的R中显示反应数据框中的最后一个日期条目
- javascript - 如何使用一个按钮来改变其他按钮的样式?
- ios - iOS 13:UIScrollView 不再滚动
- r - 如何用锯齿形或斜线填充 ggplot geom_map 中的 NA 值?
- sharepoint - 从 SharePoint 下载 MS Graph API 文件
- python - Python blackjack:如何构造一个 if 语句来确定 Ace 是否应该变成 1?
- mysql - 使用连接 MySQL 时字段列表中的未知列
- ios - -[RCTRootView cancelTouches]` 已弃用,将很快在反应原生地图中删除
- mysql - 如何加入这两个查询?找出谁共享名字和姓氏以及有多少人
- google-sheets - 仅当第 1 列中的值包含第 2 列中的特定单词时,才从具有 2 列的范围中提取值