Hazel没有将IMDG String转换为HazelcastJsonValue转换。

问题声明-在HazelCast IMDG中将json作为字符串格式存储。使用HazelcastJsonValue ..获取以下异常进行检索

请帮助解决问题。

如果HazelcastJsonValue对象存储在hazelcast IMDG ..中,则它起作用。
但是StringHazelcastJsonValue无效。获取以下异常:

Exception in thread "main" com.hazelcast.query.QueryException: java.lang.IllegalArgumentException: There is no suitable accessor for 'age' on class 'java.lang.String'
    at com.hazelcast.query.impl.getters.ReflectionHelper.createGetter(ReflectionHelper.java:171)
    at com.hazelcast.query.impl.getters.Extractors.instantiateGetter(Extractors.java:152)
    at com.hazelcast.query.impl.getters.Extractors.getGetter(Extractors.java:118)
    at com.hazelcast.query.impl.getters.Extractors.extract(Extractors.java:73)


public static void main(String args[]) {

    HazelcastInstance instance = com.hazelcast.core.Hazelcast.newHazelcastInstance(config());
    String person1 = "{ \"name\": \"John\", \"age\": 35 }";
    String person2 = "{ \"name\": \"Jane\", \"age\": 24 }";
    String person3 = "{ \"name\": \"Trey\", \"age\": 17 }";
    //Storing as String
    IMap<Integer, String> idPersonMap = instance.getMap("jsonValues");
    idPersonMap.put(1, person1);
    idPersonMap.put(2, person2);
    idPersonMap.put(3, person3);

    //Retrieving as HazelcastJsonValue
    IMap<Integer, HazelcastJsonValue> idPersonMapRead =  instance.getMap("jsonValues");
    Collection<HazelcastJsonValue> peopleUnder21 = (Collection<HazelcastJsonValue>) idPersonMapRead.values(Predicates.lessThan("age", 50));
    System.out.println( "-------------------Result--------------------");
    System.out.println( peopleUnder21.stream().collect(Collectors.toList()));

}

 public static Config config(){
        Config config = new Config();
        config.setInstanceName("hazelcast-instance")
                .addMapConfig(
                        new MapConfig().setMetadataPolicy(MetadataPolicy.OFF)
                                .setName("configuration")
                                .setMaxSizeConfig(new MaxSizeConfig(200, MaxSizeConfig.MaxSizePolicy.FREE_HEAP_SIZE))
                                .setEvictionPolicy(EvictionPolicy.LRU)
                                .setTimeToLiveSeconds(-1));

        return config;
    }

最佳答案

您将String实例放入IMap。不要期望HazelcastJsonValue实例返回get实例。它将再次是String

当您调用方法get时,它不知道您的目标
通用类型。为了方便用户,Hazelcast不会返回原始类型,但是
它进行了未经检查的转换功能。

如果您想在看跌期权范围内进行自动值转换
操作,您可以使用MapInterceptor例如:

IMap<Integer, String> idPersonMap = instance.getMap("jsonValues");
idPersonMap.addInterceptor(new ConvertOnPutMapInterceptor());

    private static class ConvertOnPutMapInterceptor implements MapInterceptor {

        @Override
        public Object interceptGet(Object value) {
            return value;
        }

        @Override
        public void afterGet(Object value) {
        }

        @Override
        public Object interceptPut(Object oldValue, Object newValue) {
            return new HazelcastJsonValue((String) newValue);
        }

        @Override
        public void afterPut(Object value) {
        }

        @Override
        public Object interceptRemove(Object removedValue) {
            return null;
        }

        @Override
        public void afterRemove(Object value) {
        }
    }

09-18 22:22