0
    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的瘦客户端。

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

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

> 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();

我不认为这是必要的。还有其他解决方案吗?

4

1 回答 1

0

更改客户端缓存定义

ClientCache<Integer, Person> cache = igniteClient.getOrCreateCache(CACHE_NAME)
于 2020-01-21T13:06:12.820 回答