You are viewing a plain text version of this content. The canonical link for it is here.
Posted to user@ignite.apache.org by Ambha <am...@wipro.com> on 2016/03/15 07:54:02 UTC

Querying HashMap stored as value in IgniteCache

For my application, I need to store <String, HashMap> as key-value pair in
IgniteCache and later query the cache on HashMap keys as shown in example
below. Is that possible in Ignite? If it is not possible, do let me know the
way to extend query/indexing functionality to enable this feature or is
there any other way of achieving the same result but the IgniteCache's value
must be HashMap because the keys are dynamic

Ex: 

Ignite ignite = Ignition.start("ignite-config.xml")
IgniteCache cache = ignite.getOrCreateCache("mycache");

Map<String,Object> valueMap = new HashMap<String,Object>();
valueMap.put("id", 111);
valueMap.put("name", "cache name");
valueMap.put("isActive", Boolean.TRUE);

cache.put("111", valueMap);

valueMap = new HashMap<String,Object>();
valueMap.put("id", 222);
valueMap.put("name", "cache name");
valueMap.put("isActive", Boolean.FALSE);

cache.put("222", valueMap);

valueMap = new HashMap<String,Object>();
valueMap.put("id", 333);
valueMap.put("name", "cache name");
valueMap.put("isActive", Boolean.TRUE);

cache.put("333", valueMap);

My query looks like "get all HashMaps which are active (i.e. having the
isActive = Boolean.TRUE)"
the expected output is "111" and "333"



--
View this message in context: http://apache-ignite-users.70518.x6.nabble.com/Querying-HashMap-stored-as-value-in-IgniteCache-tp3507.html
Sent from the Apache Ignite Users mailing list archive at Nabble.com.

Re: Querying HashMap stored as value in IgniteCache

Posted by vkulichenko <va...@gmail.com>.
Hi limabean,

Can you please properly subscribe to the mailing list so that the community
receives email notifications? Here is the instruction:
http://apache-ignite-users.70518.x6.nabble.com/mailing_list/MailingListOptions.jtp?forum=1


limabean wrote
> I have a similar question to the one that is asked, except instead of a
> HashMap, I have a class that I have defined with indexes on the some of
> the properties.  The code looks something like this:
> 
> Ignite ignite = Ignition.start("ignite-config.xml")
> IgniteCache cache = ignite.getOrCreateCache("mycache");
> 
> Map&lt;Integer,Object&gt; valueMap = new HashMap&lt;Integer,Object&gt;();
> Person p1 = new Person(1, 11, "Roger", 2000.00);
> cache.put(111, p1);
> 
> Person p2 = new Person(2, 11, "Doger", 2500.00);
> cache.put(222, p2);
> 
> I can run a SQL query like this:
> SqlQuery&lt;AffinityKey&lt;Integer&gt;, Person> qry1 =
>                     new SqlQuery<>(Person.class, "select * from Person
> where salary < ?");
> qry1.setArgs(3000);
> 
> But is there a way other than SQL to reference the fields in the Person
> class without
> first going through the cache.get(
> <some cache id here>
>  )  on each entry ?
> 
> For example, is it possible to find all Person objects that have 11 in
> that second field just using Java syntax on a client ?
> 
> Thank you,

You can use predicate-based scan queries [1]. But note that it will have to
iterate through all the entries in cache, while with SQL you can create
indexes for fields you're querying by and the lookup will be much faster.

[1] https://apacheignite.readme.io/docs/cache-queries#scan-queries

-Val



--
View this message in context: http://apache-ignite-users.70518.x6.nabble.com/Querying-HashMap-stored-as-value-in-IgniteCache-tp3507p3550.html
Sent from the Apache Ignite Users mailing list archive at Nabble.com.

Re: Querying HashMap stored as value in IgniteCache

Posted by Alexey Goncharuk <al...@gmail.com>.
Hi,

It may be the case that you can utilize BinaryObjectBuilder instead of
HashMap for the use-case you described [1]. It is an abstraction that was
created to handle cases when no class definitions exist. You can also
change the structure of your binary objects in runtime.

So the code you have described would look like so:

Ignite ignite = Ignition.start("ignite-config.xml");
IgniteCache cache = ignite.getOrCreateCache("mycache");
IgniteBinary binary = ignite.binary();

BinaryObjectBuilder valBuilder = binary.builder("myType");
valBuilder.setField("id", 111);
valBuilder.setField("name", "cache name");
valBuilder.setField("isActive", Boolean.TRUE);

cache.put("111", valBuilder.build());

valBuilder = binary.builder("myType");
valBuilder.setField("id", 222);
valBuilder.setField("name", "cache name");
valBuilder.setField("isActive", Boolean.FALSE);

cache.put("222", valBuilder.build());

valBuilder = binary.builder("myType");
valBuilder.setField("id", 333);
valBuilder.setField("name", "cache name");
valBuilder.setField("isActive", Boolean.TRUE);

cache.put("333", valBuilder.build());


In this case you will be able to configure indexing via QueryEntity and use
standard Ignite SQL queries [2].

Let us know if this works for you.

--
AG

[1] https://apacheignite.readme.io/docs/binary-marshaller
[2] https://apacheignite.readme.io/docs/sql-queries

Re: Querying HashMap stored as value in IgniteCache

Posted by vkulichenko <va...@gmail.com>.
Hi,

Are you going to query based on 'isActive' field only, or you need to
indexes be dynamic as well?

-Val



--
View this message in context: http://apache-ignite-users.70518.x6.nabble.com/Querying-HashMap-stored-as-value-in-IgniteCache-tp3507p3521.html
Sent from the Apache Ignite Users mailing list archive at Nabble.com.