我正在尝试通过 jdbc 获取 Ignite Cache 数据。为此,我定义了新的自定义类并注释了这样的字段:
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);
但是如果我已经在类定义中注释了字段,为什么还需要这样做呢?
最佳答案
您可以使用三个选项来定义 SQL 架构:
注释和 CacheConfiguration.setIndexedTypes https://apacheignite.readme.io/docs/cache-queries#section-query-configuration-by-annotations
您可以配置 QueryEntity: https://apacheignite.readme.io/docs/cache-queries#section-query-configuration-using-queryentity
或者只使用纯 SQL: https://apacheignite-sql.readme.io/docs/create-table
在您的例子中,您混合了 [1] 和 [2],因此您注册了 QueryEntity 索引的键和值,但使用注释定义了字段,因此不同方式的混合不起作用。您需要坚持打开特定的方式,就像您已经通过使用 CacheConfiguration.setIndexedTypes 方法添加用于索引的键和值注册一样。所以您现在可以摆脱 QueryEntity。
关于java - 定义缓存时如何从自定义类获取 QueryFields?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/52202850/