定义缓存时如何从自定义 class 获取 QueryFields?

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

我正在尝试通过 jdbc 获取 Ignite 缓存数据。为此,我定义了新的自定义 class 并像这样注释字段:

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;
...
}

然后我就这样开始点燃:

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

现在,当我尝试从 JDBC 获取数据时,出现错误:

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

我可以定义一组字段以获得从 JDBC

获取数据的机会
LinkedHashMap<String, String> fields = new LinkedHashMap();
fields.put("session_id", Integer.class.getName());
fields.put("records_offset", String.class.getName());
queryEntity.setFields(fields);

但是,如果我已经在 class 定义中注释了字段,为什么还需要这样做?

您可以通过三个选项来定义 SQL 架构:

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

  2. 您可以配置QueryEntity: https://apacheignite.readme.io/docs/cache-queries#section-query-configuration-using-queryentity

  3. 或者只使用纯 SQL: https://apacheignite-sql.readme.io/docs/create-table

在你的例子中,你混合了 [1] 和 [2],所以你注册了键和值用于 QueryEntity 的索引,但是定义了带有注释的字段,所以混合不同的方式是行不通的。您需要坚持打开特定的方式,就像您已经通过添加键和值注册以使用 CacheConfiguration.setIndexedTypes 方法进行索引一样。所以你现在可以去掉 QueryEntity。