How to use CacheConfiguration.setIndexedTypes for Ignite Cache

Viewed 1313

I am following the code for the running SQL queries in the Ignite cache, but am able to fully realize the use of the CacheConfiguration.setIndexedTypes API.

I am following the only help that I could find at the ignite site.

The documentation here says to use CacheConfiguration.setIndexedTypes(MyKey.class, MyValue.class).

Now lets say in the Person class

@QuerySqlField(index = true)
private long id;

@QuerySqlField
private String firstName;

Which are the parameters that I should be passing in the setIndexedType method?

2 Answers

setIndexedTypes takes an even number of parameters. Every odd parameter corresponds to a key type, and every even - to a value type. In your case you should probably use id parameter as a key, so you should call it this way:

cacheConfig.setIndexedTypes(Long.class, Person.class);

Javadoc for setIndexedTypes method contains a pretty good explanation of this method: https://ignite.apache.org/releases/latest/javadoc/org/apache/ignite/configuration/CacheConfiguration.html#setIndexedTypes(java.lang.Class...)

UPD:

There will be registered a table in SQL for each pair of parameters that you provide to setIndexedTypes method.

Your SQL entities will map to cache records and they will have _key and _val columns in addition to the ones that you configured as QuerySqlField-s. So, you should specify types of keys and values that will be used in cache for each table.

You can refer to this page for more information: https://apacheignite.readme.io/docs/dml#basic-configuration

In your case it will be

cacheConfig.setIndexedTypes(KeyType.class, Person.class)

where KeyType is the type you use for keys while calling cache.put(key, person) or insert into Person(_key, ...) ...
Please refer to this documentation section

Related