【问题标题】:How to use SQL UNION with Ignite Caches如何将 SQL UNION 与 Ignite 缓存一起使用
【发布时间】:2021-10-08 12:44:25
【问题描述】:

我正在使用 ignite 缓存并执行以下 sql 查询。

    IgniteCache<String, ClassName> cache = ignite.cache(CACHE_NAME);
    private static final String sql = "Select timestamp from cache1 where  orderId = ? and timestamp <= ? and timestamp >= ? ";
    SqlFieldsQuery sqlQ = new SqlFieldsQuery(sql).setArgs(id, t1, t2);
        try (QueryCursor<List<?>> cursor = cache.query(sqlQ)) {
            for (List<?> row : cursor) {
                    timestamps.add((Long) row.get(0));          
            }

现在我想从两个不同的缓存中查询并获取联合。我能够成功编写 SQL 联合操作。

private static final String sqlTwoCaches = "Select timestamp from cache1 union all Select timestamp from cache2 order by timestamp";

我想将两个缓存中的时间戳结果添加到单个数组列表中。我想知道如何使用查询游标?现在有两个缓存,这部分怎么做? (QueryCursor&lt;List&lt;?&gt;&gt; cursor = cache.query(sqlTwoCaches))

或者有没有其他方法可以做到这一点?

【问题讨论】:

    标签: java sql union ignite gridgain


    【解决方案1】:

    您需要“完全限定”第二个缓存名称,否则,它应该可以正常工作。

    例子:

    IgniteCache<String, ClassName1> cache1 = ignite.cache("table1");
    IgniteCache<String, ClassName2> cache2 = ignite.cache("table2");
    private static final String sql = "select timestamp from ClassName1 UNION ALL select timestamp from table2.ClassName2 ";
    SqlFieldsQuery sqlQ = new SqlFieldsQuery(sql);
    try (QueryCursor<List<?>> cursor = cache1.query(sqlQ)) {
        for (List<?> row : cursor) {
                timestamps.add((Long) row.get(0));          
        }
     }
    

    【讨论】:

    • 不清楚你说的是什么。你能解释一下吗?
    • 知道了。如果我有这样的第三个缓存,IgniteCache&lt;String, ClassName3&gt; cache3 = ignite.cache("table3"); 我可以这样做吗? private static final String sql = "select timestamp from ClassName1 UNION ALL select timestamp from table2.ClassName2 UNION ALL select timestamp from table3.ClassName3";SqlFieldsQuery sqlQ = new SqlFieldsQuery(sql);try (QueryCursor&lt;List&lt;?&gt;&gt; cursor = cache1.query(sqlQ)) { ....
    • 你的方法没有分叉。它给出了以下错误。 CacheException: Failed to parse query. Schema "UNCONFIRMED_EVENT_URGENT_MC_79" not found; : 这是 SQL 查询。 sql = "Select _val from UnconfirmedEvent where orderId = ? and startTime &lt; ? and endTime &gt; ? and type = %s UNION ALL " + "Select _val from " + String.format(UNCONFIRMED_URGENT_EVENT_CACHE,mcId) + ".UnconfirmedEvent where orderId = ? and startTime &lt; ? and endTime &gt; ? and type = %s " + " order by startTime";
    • 这是说没有你给的名字的缓存。调用Ignite#cacheNames()时会列出哪些缓存?
    • UNCONFIRMED_EVENT_URGENT_MC_79UNCONFIRMED_EVENT_MC_79 缓存在调用时在 ignite 中可用。
    猜你喜欢
    • 2017-08-08
    • 1970-01-01
    • 1970-01-01
    • 2011-04-25
    • 1970-01-01
    • 2016-05-15
    • 1970-01-01
    • 1970-01-01
    • 2011-11-20
    相关资源
    最近更新 更多