You are viewing a plain text version of this content. The canonical link for it is here.
Posted to user@ignite.apache.org by Dominik Przybysz <al...@gmail.com> on 2020/04/01 06:09:35 UTC
Using SQL cache with javax cache api
Hi,
I created SQL cache, but sometimes I want to use it via javax.cache.api and
it doesn't work.
I created cache with SQL:
CREATE TABLE IF NOT EXISTS Person6 (
id varchar primary key ,
city_id int,
name varchar,
age int,
company varchar
) WITH
"template=replicated,backups=1,wrap_key=false,value_type=java.util.HashMap,cache_name=Person6";
insert into PERSON6(ID, CITY_ID, NAME, AGE, COMPANY) values (
'1', 1, 'TEST', 20, 'Bla'
);
insert into PERSON6(ID, CITY_ID, NAME, AGE, COMPANY) values (
'2', 1, 'TEST2', 20, 'Bla 1'
);
Next I created client and fetching data with SqlFieldQuery works without
problems:
SqlFieldsQuery query = new SqlFieldsQuery("SELECT * from Person6");
FieldsQueryCursor<List<?>> cursor = cache.query(query);
but when I tried to query the cache with get:
IgniteCache<String, Object> cache = ignite.cache("Person6");
System.out.println("CacheSize: " + cache.size(CachePeekMode.PRIMARY));
System.out.println("1: " + cache.get("1"));
I received:
CacheSize: 2
1: null
To solve it I added withKeepBinary():
IgniteCache<String, Object> cache =
ignite.cache("Person6").withKeepBinary();
and then I received valid data:
CacheSize: 2
1: java.util.HashMap [idHash=660595570, hash=-1179353910, CITY_ID=1,
ID=null, NAME=TEST, AGE=20, COMPANY=Bla]
but now I cannot add HashMap to the cache:
Map<String, Object> value = new HashMap<>();
value.put("ID", uuid);
value.put("CITY_ID", 1);
value.put("NAME", "c");
value.put("AGE", 90);
value.put("COMPANY", "AAAAA");
cache. put(uuid, value);
throws:
Exception in thread "main" javax.cache.CacheException: class
org.apache.ignite.IgniteCheckedException: Unexpected binary object class
[type=class
org.apache.ignite.internal.processors.cacheobject.UserCacheObjectImpl]
and adding works only with objects created with BinaryObjectBuilder.
Is it expected behaviour for caches String->HashMap?
--
Pozdrawiam / Regards,
Dominik Przybysz
Re: Using SQL cache with javax cache api
Posted by Evgenii Zhuravlev <e....@gmail.com>.
Hi,
I'm not sure why wrap_key doesn't work here, but wey_type should solve this
issue.
Evgenii
вт, 31 мар. 2020 г. в 23:10, Dominik Przybysz <al...@gmail.com>:
> Hi,
> I created SQL cache, but sometimes I want to use it via javax.cache.api
> and it doesn't work.
>
> I created cache with SQL:
>
> CREATE TABLE IF NOT EXISTS Person6 (
> id varchar primary key ,
> city_id int,
> name varchar,
> age int,
> company varchar
> ) WITH
> "template=replicated,backups=1,wrap_key=false,value_type=java.util.HashMap,cache_name=Person6";
>
> insert into PERSON6(ID, CITY_ID, NAME, AGE, COMPANY) values (
> '1', 1, 'TEST', 20, 'Bla'
> );
>
> insert into PERSON6(ID, CITY_ID, NAME, AGE, COMPANY) values (
> '2', 1, 'TEST2', 20, 'Bla 1'
> );
>
> Next I created client and fetching data with SqlFieldQuery works without
> problems:
>
> SqlFieldsQuery query = new SqlFieldsQuery("SELECT * from Person6");
> FieldsQueryCursor<List<?>> cursor = cache.query(query);
>
> but when I tried to query the cache with get:
>
> IgniteCache<String, Object> cache = ignite.cache("Person6");
> System.out.println("CacheSize: " + cache.size(CachePeekMode.PRIMARY));
> System.out.println("1: " + cache.get("1"));
>
> I received:
>
> CacheSize: 2
> 1: null
>
> To solve it I added withKeepBinary():
>
> IgniteCache<String, Object> cache =
> ignite.cache("Person6").withKeepBinary();
>
> and then I received valid data:
>
> CacheSize: 2
> 1: java.util.HashMap [idHash=660595570, hash=-1179353910, CITY_ID=1,
> ID=null, NAME=TEST, AGE=20, COMPANY=Bla]
>
> but now I cannot add HashMap to the cache:
>
> Map<String, Object> value = new HashMap<>();
> value.put("ID", uuid);
> value.put("CITY_ID", 1);
> value.put("NAME", "c");
> value.put("AGE", 90);
> value.put("COMPANY", "AAAAA");
> cache. put(uuid, value);
>
> throws:
>
> Exception in thread "main" javax.cache.CacheException: class
> org.apache.ignite.IgniteCheckedException: Unexpected binary object class
> [type=class
> org.apache.ignite.internal.processors.cacheobject.UserCacheObjectImpl]
>
> and adding works only with objects created with BinaryObjectBuilder.
>
> Is it expected behaviour for caches String->HashMap?
>
> --
> Pozdrawiam / Regards,
> Dominik Przybysz
>
Re: Using SQL cache with javax cache api
Posted by Ilya Kasnacheev <il...@gmail.com>.
Hello!
Ignite cannot store multi-columnstables as maps in cache. Those columns
will still be stored as BinaryObject/POJO, and you only confuse Ignite
internal by pretending that it is HashMap.
I recommend getting rid of HashMap in table declaration and using
ignite.binary().builder("typeName") instead - use same type name in
value_type and in builder().
Also you should not put ID into value, it makes no sense.
Regards,
--
Ilya Kasnacheev
ср, 1 апр. 2020 г. в 09:10, Dominik Przybysz <al...@gmail.com>:
> Hi,
> I created SQL cache, but sometimes I want to use it via javax.cache.api
> and it doesn't work.
>
> I created cache with SQL:
>
> CREATE TABLE IF NOT EXISTS Person6 (
> id varchar primary key ,
> city_id int,
> name varchar,
> age int,
> company varchar
> ) WITH
> "template=replicated,backups=1,wrap_key=false,value_type=java.util.HashMap,cache_name=Person6";
>
> insert into PERSON6(ID, CITY_ID, NAME, AGE, COMPANY) values (
> '1', 1, 'TEST', 20, 'Bla'
> );
>
> insert into PERSON6(ID, CITY_ID, NAME, AGE, COMPANY) values (
> '2', 1, 'TEST2', 20, 'Bla 1'
> );
>
> Next I created client and fetching data with SqlFieldQuery works without
> problems:
>
> SqlFieldsQuery query = new SqlFieldsQuery("SELECT * from Person6");
> FieldsQueryCursor<List<?>> cursor = cache.query(query);
>
> but when I tried to query the cache with get:
>
> IgniteCache<String, Object> cache = ignite.cache("Person6");
> System.out.println("CacheSize: " + cache.size(CachePeekMode.PRIMARY));
> System.out.println("1: " + cache.get("1"));
>
> I received:
>
> CacheSize: 2
> 1: null
>
> To solve it I added withKeepBinary():
>
> IgniteCache<String, Object> cache =
> ignite.cache("Person6").withKeepBinary();
>
> and then I received valid data:
>
> CacheSize: 2
> 1: java.util.HashMap [idHash=660595570, hash=-1179353910, CITY_ID=1,
> ID=null, NAME=TEST, AGE=20, COMPANY=Bla]
>
> but now I cannot add HashMap to the cache:
>
> Map<String, Object> value = new HashMap<>();
> value.put("ID", uuid);
> value.put("CITY_ID", 1);
> value.put("NAME", "c");
> value.put("AGE", 90);
> value.put("COMPANY", "AAAAA");
> cache. put(uuid, value);
>
> throws:
>
> Exception in thread "main" javax.cache.CacheException: class
> org.apache.ignite.IgniteCheckedException: Unexpected binary object class
> [type=class
> org.apache.ignite.internal.processors.cacheobject.UserCacheObjectImpl]
>
> and adding works only with objects created with BinaryObjectBuilder.
>
> Is it expected behaviour for caches String->HashMap?
>
> --
> Pozdrawiam / Regards,
> Dominik Przybysz
>