使用谓词在Hazelcast中搜索键集 [英] Using Predicate to search a keyset in Hazelcast
问题描述
我是Hazelcast的新手-进行评估和原型设计,看它是否适合我们的分布式内存缓存需求. 要求之一是能够使用通配符搜索给定地图中的键.查看IMap文档,看起来可以使用keySet(Predicate predicate).但是我无法弄清楚如何以这样的方式使用谓词:给定通配符字符串,将返回包含所有匹配键的keySet.一个例子会有所帮助.
I'm new to Hazelcast - evaluating and prototyping to see if it fits our distributed memory cache needs. One of the requirements was to be able to use wild cards to search for keys in a given map. Looking through the IMap documentation, looks like keySet(Predicate predicate) can be used. But I couldn't figure how to use the Predicate in such a way that given a wild card string, a keySet is returned containing all keys that match. An example will be helpful.
我的代码段.这是客户端.
A snippet of my code. This is the client side.
IMap<String, String> mapTest1 = client.getMap("testMap");
mapTest1.put( "testKey1", "This is a Test" );
mapTest1.put( "testKey2", "This value has a long key name" );
mapTest1.put( "key3", "Another value" );
// Need a wild card search on the map, such that all keys with "%test%" will be returned.
谢谢.
推荐答案
如果我正确理解了您的请求,这应该可以解决问题:
This should do the trick if I understood your request correctly:
public class Test {
public static void main(String[] args) {
HazelcastInstance hazelcastInstance = Hazelcast.newHazelcastInstance();
IMap<String, String> map = hazelcastInstance.getMap("someMap");
Collection<String> keys = map.keySet(
new RegexPredicate("[a-z].*[a-z0-9]"));
System.out.println(keys);
}
public static class RegexPredicate implements Predicate<String, String> {
private String regex;
private transient Pattern pattern;
public RegexPredicate(String regex) {
this.regex = regex;
}
@Override
public boolean apply(Map.Entry<String, String> mapEntry) {
if (pattern == null) {
pattern = Pattern.compile(regex);
}
Matcher matcher = pattern.matcher(mapEntry.getKey());
return matcher.matches();
}
}
}
这篇关于使用谓词在Hazelcast中搜索键集的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!