言成言成啊 | Kit Chen's Blog

Hazelcast实现SQL查询

2023-01-17 22:33:47 转载随意,文章会持续修订,请注明来源地址:https://meethigher.top/blog

引入hazelcast

1
2
3
4
5
<dependency>
<groupId>com.hazelcast</groupId>
<artifactId>hazelcast</artifactId>
<version>5.2.0</version>
</dependency>

实现简单的sql查询

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
import com.hazelcast.core.Hazelcast;
import com.hazelcast.core.HazelcastInstance;
import com.hazelcast.map.IMap;
import com.hazelcast.query.PagingPredicate;
import com.hazelcast.query.PredicateBuilder;
import com.hazelcast.query.Predicates;

import java.io.Serializable;
import java.util.*;
import java.util.concurrent.ThreadLocalRandom;
import java.util.stream.Collectors;


public class Application {

public static class Employee implements Serializable {
private String id;
private String name;
private int age;
private boolean active;
private double salary;
private Long createTime;

public Employee(String id, String name, int age, boolean active, double salary, Long createTime) {
this.id = id;
this.name = name;
this.age = age;
this.active = active;
this.salary = salary;
this.createTime = createTime;
}

public Employee() {
}


public String getId() {
return id;
}

public void setId(String id) {
this.id = id;
}

public String getName() {
return name;
}

public void setName(String name) {
this.name = name;
}

public int getAge() {
return age;
}

public void setAge(int age) {
this.age = age;
}

public boolean isActive() {
return active;
}

public void setActive(boolean active) {
this.active = active;
}

public double getSalary() {
return salary;
}

public void setSalary(double salary) {
this.salary = salary;
}

public Long getCreateTime() {
return createTime;
}

public void setCreateTime(Long createTime) {
this.createTime = createTime;
}

@Override
public String toString() {
final StringBuilder sb = new StringBuilder("{");
sb.append("\"id\":\"")
.append(id).append('\"');
sb.append(",\"name\":\"")
.append(name).append('\"');
sb.append(",\"age\":")
.append(age);
sb.append(",\"active\":")
.append(active);
sb.append(",\"salary\":")
.append(salary);
sb.append(",\"createTime\":")
.append(createTime);
sb.append('}');
return sb.toString();
}
}

public static String getRandomString(int length) {
String str = "赵钱孙李周吴郑王冯陈褚卫蒋沈韩杨朱秦尤许何吕施张孔曹严华金魏陶姜戚谢邹喻柏水窦章云苏潘葛奚范彭郎鲁韦昌马苗凤花方俞任袁柳酆鲍史唐费廉岑薛雷贺倪汤滕殷罗毕郝邬安常乐于时傅皮卞齐康伍余元卜顾孟平黄和穆萧尹姚邵湛汪祁毛禹狄米贝明臧计伏成戴谈宋茅庞熊纪舒屈项祝董梁杜阮蓝闵席季麻强贾路娄危江童颜郭梅盛林刁锺徐邱骆高夏蔡田樊胡凌霍虞万支柯昝管卢莫";
StringBuilder sb = new StringBuilder();
for (int i = 0; i < length; i++) {
//[ 0,str.length() )
int number = ThreadLocalRandom.current().nextInt(0, str.length());
sb.append(str.charAt(number));
}
return sb.toString();
}

public static List<Employee> mockData(int amount) {
List<Employee> list = new LinkedList<>();
for (int i = 0; i < amount; i++) {
list.add(new Employee(
UUID.randomUUID().toString(),
getRandomString(3),
//均为前闭后开
ThreadLocalRandom.current().nextInt(18, 30),
ThreadLocalRandom.current().nextBoolean(),
ThreadLocalRandom.current().nextDouble(4000, 10000),
System.currentTimeMillis()
));
}
return list;
}

/**
* [Predicates API](https://docs.hazelcast.com/hazelcast/5.2/query/predicate-overview)
*/
public static void main(String[] args) {
HazelcastInstance hazelcastInstance = Hazelcast.newHazelcastInstance();
IMap<String, Employee> map = hazelcastInstance.getMap("hz");
mockData(10000).forEach(x -> {
map.put(x.getId(), x);
});

/**
* 1. 分页查询
*/
//构建分页查询条件。排序是必需的,否则会Not comparable com.shenlan.ecitiy.app.entity.BikeLocation@750cd1bc
PagingPredicate<String, Employee> pagingPredicate = Predicates.pagingPredicate(new Comparator<Map.Entry<String, Employee>>() {
//后-前=降序,前-后=升序
@Override
public int compare(Map.Entry<String, Employee> o1, Map.Entry<String, Employee> o2) {
return (int) (o2.getValue().getCreateTime() - o1.getValue().getCreateTime());
}
}, 10);
pagingPredicate.setPage(0);
//这是个ResultSet
Collection<Employee> values = map.values(pagingPredicate);
for (Employee value : values) {
System.out.println(value);
}
System.out.println("========================");

/**
* 2. 基本条件查询。
* equal、notEqual、like、greaterThan、lessThan、in
*/
Collection<Employee> collection = map.values(Predicates.equal("id", values.stream().findFirst().get().getId()));
System.out.println(collection.size());

collection = map.values(Predicates.notEqual("active", true));
System.out.println(collection.size());

collection = map.values(Predicates.like("name", "%李%"));
System.out.println(collection.size());


PredicateBuilder.EntryObject object = Predicates.newPredicateBuilder().getEntryObject();
PredicateBuilder and = object.get("age").greaterEqual(18).and(object.get("age").lessThan(20));
collection = map.values(and);
System.out.println(collection.size());

List<String> collect = values.stream().map(Employee::getId).collect(Collectors.toList());
//in的第二个参数,接收的是个数组,类型是Comparable。java中所有的引用类型都实现了该接口
collection = map.values(Predicates.in("id", collect.toArray(new String[0])));
System.out.println(collection.size());
}
}
发布:2023-01-17 22:33:47
修改:2023-01-17 22:39:09
链接:https://meethigher.top/blog/2023/hazelcast-sql/
标签:java hazelcast sql 
付款码 打赏 分享
shift+ctrl+1可控制目录显示