aryndin
aryndin

Reputation: 591

How do I get QueryFields from custom class when defining cache?

I'm trying to get Ignite Cache data through jdbc. For that purpose I define new custom class and annotate fields like that :

public class MyClass implements Serializable {
    @QuerySqlField(index = true)
    public Integer id;

    @QuerySqlField(index = true)
    public String records_offset;

    @QuerySqlField(index = true)
    public Integer session_id;
...
}

Then I start ignite in this way:

CacheConfiguration conf = new CacheConfiguration();
conf.setBackups(1);
conf.setName("test");

QueryEntity queryEntity = new QueryEntity();
queryEntity.setKeyType(Integer.class.getName());
queryEntity.setValueType(CDR.class.getName());
queryEntity.setTableName("CDR");

conf.setQueryEntities(Arrays.asList(queryEntity));
IgniteConfiguration iconf = new IgniteConfiguration();
iconf.setCacheConfiguration(conf);
iconf.setPeerClassLoadingEnabled(true);
this.ignite = Ignition.start(iconf);
this.cache = ignite.getOrCreateCache("test");

Now when I try to get data from JDBC, I get error:

Error: class org.apache.ignite.binary.BinaryObjectException: Custom objects are not supported (state=50000,code=0)

I could define a set of fields to get opportunity to fetch data from JDBC

LinkedHashMap<String, String> fields = new LinkedHashMap();
fields.put("session_id", Integer.class.getName());
fields.put("records_offset", String.class.getName());
queryEntity.setFields(fields);

But Why do I need to do this if I've already annotated field in class definition?

Upvotes: 0

Views: 843

Answers (1)

Michael
Michael

Reputation: 650

You have three options to define SQL schema:

  1. Annotations and CacheConfiguration.setIndexedTypes https://apacheignite.readme.io/docs/cache-queries#section-query-configuration-by-annotations

  2. You can configure QueryEntity: https://apacheignite.readme.io/docs/cache-queries#section-query-configuration-using-queryentity

  3. or just use pure SQL: https://apacheignite-sql.readme.io/docs/create-table

In your case, you mixed [1] and [2], so you registered key and value for indexing by QueryEntity, but defined fields with annotations, so mixing of different ways doesn't work. you need to stick to open specific way like you already did by adding key and value registration for indexing with CacheConfiguration.setIndexedTypes method. So you can get rid of QueryEntity now.

Upvotes: 1

Related Questions