Hazelcast-我有一个Java.Map和String字段需要查询。我正在尝试使用ValueExtractor来获取Map的值和Strings。有人有一些有关如何执行此操作的示例吗?
编辑:也许是一个类似的问题,但我需要一些示例:hazelcast - is there a way to iterate over a map keys and/or values to query
public class Client{
Company company;
Map<String, String> programs = new HashMap<String, String>();
String name;
//some other code here
}
// in my search class
public List<Client> search(Map<DirProperty, String> criteria) {
ArrayList<Client> users = new ArrayList<Client>();
if (cachedClients != null) {
try {
SqlPredicate predicate = getCachePredicate(criteria);
log.debug(String.format("Searching forms in cache: %s", criteria.toString()));
Collection<Client> clientCollection = cachedClients.values(predicate);
clients = new ArrayList<Client>(clientCollection);
} catch (Exception e) {
log.error("Search failed: " + criteria.toString(), e);
}
}
return users;
}
private SqlPredicate getCachePredicate(Map<DirProperty, String> criteria) throws ParseException {
String sql = getSqlString(criteria);
log.debug("Using SQL to search cache: " + sql);
return new SqlPredicate(sql);
}
private String getSqlString(Map<DirProperty, String> criteria) throws ParseException {
StringBuilder result = new StringBuilder();
if (criteria != null && !criteria.isEmpty()) {
boolean appendAnd = false;
for (Map.Entry<DirProperty, String> entry : criteria.entrySet()) {
String key = entry.getKey().getCacheId();
String value = entry.getValue();
value = value.replace("'", "''"); // Escape apostrophe
if (appendAnd) {
result.append(" AND ");
}
// Look for delimiter. Multiple values will be "or"ed together
if (value.contains("|")) {
StringTokenizer st = new StringTokenizer(value, "|");
if (st.countTokens() > 0) {
result.append("(");
boolean appendOr = false;
while (st.hasMoreTokens()) {
if (appendOr) {
result.append(" OR ");
}
result.append(getExpression(key, st.nextToken().trim()));
appendOr = true;
}
result.append(")");
}
} else if (value.startsWith("!")) {
value = value.substring(1);
result.append(getExpression(key, value).replace("=", "!="));
} else {
result.append(getExpression(key, value));
}
appendAnd = true;
}
}
return result.toString();
}
private String getExpression(String key, String value) throws ParseException {
String result = null;
if(key.equals("program")) {
String op = ">=";
key = "documentUploadRecords[any].sequenceNumber";
result = key + " " + op + " '" + value + "'";
// other if statements here
else {
result = key + " = '" + value + "'";
}
return result;
}
运行查询时,这是我收到的错误消息:
java.lang.ClassCastException: java.util.HashMap cannot be cast to java.lang.Comparable
at com.hazelcast.query.impl.predicates.AbstractPredicate.apply(AbstractPredicate.java:62)
at com.hazelcast.map.impl.query.MapQueryEngineImpl.queryTheLocalPartition(MapQueryEngineImpl.java:285)
at com.hazelcast.map.impl.query.MapQueryEngineImpl.querySequential(MapQueryEngineImpl.java:187)
at com.hazelcast.map.impl.query.MapQueryEngineImpl.queryUsingFullTableScan(MapQueryEngineImpl.java:176)
at com.hazelcast.map.impl.query.MapQueryEngineImpl.queryLocalPartitions(MapQueryEngineImpl.java:131)
at com.hazelcast.map.impl.query.QueryOperation.run(QueryOperation.java:51)
答案 0 :(得分:0)
Hazelcast代码示例已经有该用例的示例。查看此演示:https://github.com/hazelcast/hazelcast-code-samples/blob/master/distributed-map/custom-attributes/src/main/java/com/test/car/attribute/CarAttributeDemo.java
汽车类包含地图和示例,使用提取器查询这些属性。