如何解决Ignite SqlQuery上的“无法找到类型的SQL表”异常?

问题描述 投票:0回答:1

在Ignite缓存上执行SQL查询时,我收到异常“找不到类型的SQL表”。等效的ScanQuery在同一缓存上成功执行。

nb - 从我的代码中注意我在Ignite启动后创建了缓存。这是故意的,因为我希望能够动态创建缓存。

员工类:


public class Employee implements Serializable {

    @QuerySqlField (index = true)
    private final Integer empNo;
    @QuerySqlField
    private String name;
    @QuerySqlField
    private Department dept;
    @QuerySqlField
    private Integer salary;

创建并填充Employee缓存:

    CacheConfiguration<Integer, Employee> empCacheCfg = new CacheConfiguration<>();
    empCacheCfg.setName("Employee");
    IgniteCache<Integer, Employee> empCache = ignite.getOrCreateCache(empCacheCfg);

    empCache.put(0, new Employee(0, "Bill", deptCache.get("POT"), 20000));
    empCache.put(1, new Employee(1, "Ben", deptCache.get("POT"), 21000));
    empCache.put(2, new Employee(2, "Little Weed", deptCache.get("PLANT"), 15000));
    empCache.put(3, new Employee(3, "The Gardner", deptCache.get("SVC"), 30000));

员工缓存上的SQLQuery:

    SqlQuery sql = new SqlQuery("Employee", "salary > ?");

    try (QueryCursor<Cache.Entry<Integer, Employee>> cursor2 = 
                    empCache.query(sql.setArgs(20000)))
    {
        for(var e2 : cursor2)
        {
             System.out.println(String.format("Employee: %s", e2.getValue().getName()));                   
        }
    }
    catch(Exception e)
    {
        String exmsg = e.getMessage();
        System.out.println(exmsg);
    }
sql ignite
1个回答
2
投票

您没有在缓存配置中指定查询实体。可以使用CacheConfiguration#indexedTypes属性配置它们。

以下行应修复您的示例:

empCacheCfg.setIndexedTypes(Integer.class, Employee.class);

文档:https://apacheignite.readme.io/docs/cache-queries#section-query-configuration-by-annotations

© www.soinside.com 2019 - 2024. All rights reserved.