【问题标题】:ClassCastException on thinClient in Apache IgniteApache Ignite 中 ThinClient 上的 ClassCastException
【发布时间】:2020-04-05 12:48:39
【问题描述】:
    ClientConfiguration cfg = new ClientConfiguration().setAddresses("127.0.0.1:10800");

    try (IgniteClient igniteClient = Ignition.startClient(cfg)) {

        System.out.println(">>> Thin client put-get example started.");

        final String CACHE_NAME = "put-get-example";

        ClientCache<Integer, Object> cache = igniteClient.getOrCreateCache(CACHE_NAME);

        Person p = new Person();

        //put
        HashMap<Integer, Person> hm = new HashMap<Integer, Person>();
        hm.put(1, p);
        cache.put(1, hm);

        //get
        HashMap<Integer, Person> map = (HashMap<Integer, Person>)cache.get(1);
        Person p2 = map.get(1);

        System.out.format(">>> Loaded [%s] from the cache.\n",p2);

    }
    catch (ClientException e) {
        System.err.println(e.getMessage());
        e.printStackTrace();
    }
    catch (Exception e) {
        System.err.format("Unexpected failure: %s\n", e);
        e.printStackTrace();
    }

我用的是apache-ignite的瘦客户端。

我创建一个 hashmap 并将 Person 类(org.apache.ignite.examples.model.Person)对象放入其中。

当我将它从 hashmap 中取出时,我得到以下异常:

> java.lang.ClassCastException:
> org.apache.enite.internal.binary.BinaryObjectImpl cannot be cast to
> org.apache.engite.examples.model.Person.

下面的代码中给出了一个异常。

Person p2 = map.get(1);

但是,如果我将代码修改如下,也不例外:

BinaryObject bo = (BinaryObject) map.get(1);

Person p2 = bo.deserialize();

我认为没有必要。还有其他解决方案吗?

【问题讨论】:

  • 你真的想把一个HashMap作为一个缓存值而不是一个普通的Person吗?考虑:Person p = new Person();缓存.put(1, p);

标签: hashmap ignite thin-client


【解决方案1】:

更改客户端缓存定义

ClientCache<Integer, Person> cache = igniteClient.getOrCreateCache(CACHE_NAME)

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2016-08-13
    • 2019-03-15
    • 2017-11-05
    • 1970-01-01
    • 2023-02-21
    • 1970-01-01
    • 2016-07-09
    • 1970-01-01
    相关资源
    最近更新 更多