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 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204
| import com.hazelcast.core.Hazelcast; import com.hazelcast.core.HazelcastInstance; import com.hazelcast.map.IMap; import com.hazelcast.query.PagingPredicate; import com.hazelcast.query.Predicate; 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++) { 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; }
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); });
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); Collection<Employee> values = map.values(pagingPredicate); for (Employee value : values) { System.out.println(value); } System.out.println("========================");
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()); collection = map.values(Predicates.in("id", collect.toArray(new String[0]))); System.out.println(collection.size());
List<Predicate<String, Employee>> list = new ArrayList<>(); Predicate<String, Employee> idLike = Predicates.equal("id", "1"); Predicate<String, Employee> nameLike = Predicates.like("name", "%李%"); Predicate<String, Employee> or = Predicates.or(idLike, nameLike); list.add(or); Predicate<String, Employee> ageEqual = Predicates.equal("age", 20); Predicate<String, Employee> activeEqual = Predicates.equal("active", true); list.add(ageEqual); list.add(activeEqual); Predicate<String, Employee> predicate = Predicates.and(list.toArray(new Predicate[0])); Collection<Employee> employeeCollection = map.values(predicate); System.out.println(); } }
|