问题描述
我有一个hazelcast实例,其键的类型为MyObject
,值是一个枚举.假设MyObject
类的属性之一是date
,其类型为java.sql.Date
.
I have a hazelcast instance whose key is of type MyObject
and value is an enum.Let's say one of the attributes of MyObject
class is date
which is of type java.sql.Date
.
class MyObject {
private Date date;
public Date getDate() {
return date;
}
public void setDate(Date date) {
this.date = date
}
}
public enum MyEnum {
TEST_ENUM;
}
我还使用谓词对键进行过滤以检索枚举值.例如:
Also I am using predicate to filter on the keys retrieve the enum value.For ex:
EntryObject entryObject = new PredicateBuilder().getEntryObject();
PredicateBuiler predicateBuilder = entryObject.key.get(date).isNull;
这是我尝试添加索引的方式:
This is how I am trying to add index:
IMap<MyObject, MyEnum> map = hazelcastInstance.getMap("test");
map.addIndex("date", true)
但是一旦执行,就会引发异常:
But as soon as this gets executed an exception is being thrown:
com.hazelcast.query.QueryException: java.lang.IllegalArgumentException: There is no suitable accessor for 'date' on class 'class com.main.constants.myEnum'
at com.hazelcast.query.impl.getters.ReflectionHelper.createGetter(ReflectionHelper.java:176)
at com.hazelcast.query.impl.getters.Extractors.instantiateGetter(Extractors.java:88)
at com.hazelcast.query.impl.getters.Extractors.getGetter(Extractors.java:73)
at com.hazelcast.query.impl.getters.Extractors.extract(Extractors.java:57)
at com.hazelcast.query.impl.QueryableEntry.extractAttributeValueFromTargetObject(QueryableEntry.java:156)
at com.hazelcast.query.impl.QueryableEntry.extractAttributeValue(QueryableEntry.java:82)
at com.hazelcast.query.impl.QueryableEntry.getAttributeValue(QueryableEntry.java:48)
at com.hazelcast.query.impl.QueryableEntry.getConverter(QueryableEntry.java:67)
at com.hazelcast.query.impl.IndexImpl.saveEntryIndex(IndexImpl.java:67)
at com.hazelcast.map.impl.operation.AddIndexOperation.run(AddIndexOperation.java:75)
我了解它正在尝试在值类中找到索引属性
I understand it's trying to find the index attribute in the value class
如何使此功能正常工作,即在键上而不是在值上添加索引.
How do I get this thing working i.e. add the index on the Key rather than on the value.
推荐答案
在编写测试时,我实际上发现了您的问题:-)
While writing a test I actually found your problem :-)
import com.hazelcast.core.Hazelcast;
import com.hazelcast.core.HazelcastInstance;
import com.hazelcast.core.IMap;
import com.hazelcast.query.EntryObject;
import com.hazelcast.query.PredicateBuilder;
import java.io.Serializable;
import java.sql.Date;
public class Test
{
public static void main(String[] args)
{
HazelcastInstance hz = Hazelcast.newHazelcastInstance();
IMap<MyObject, MyEnum> map = hz.getMap("test");
EntryObject entryObject = new PredicateBuilder().getEntryObject();
PredicateBuilder builder = entryObject.key().get("date").isNull();
map.addIndex("__key#date", true);
map.put(new MyObject(), MyEnum.TEST_ENUM);
}
public static class MyObject implements Serializable
{
private Date date;
public Date getDate()
{
return date;
}
public void setDate(Date date)
{
this.date = date;
}
}
public static enum MyEnum {
TEST_ENUM;
}
}
诀窍是基于map键而不是默认值创建索引.您已经在查询entryObject.key()
中进行了此操作,但是在索引定义__key.date
中未进行此操作.
The trick is to create the index based on the map-key and not the value which is taken by default. You already did it in your query entryObject.key()
but missed it on the index definition __key.date
.
这篇关于Hazelcast:添加索引时出现问题的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!