Hazelcast: Issue while adding index

夙愿已清 提交于 2019-12-12 02:54:45

问题


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.


回答1:


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;
    }
}

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.



来源:https://stackoverflow.com/questions/36005130/hazelcast-issue-while-adding-index

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!