0

I was having a ignite cache and doing sql queries as follows.

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

Now I want to query from two different caches and get the union. I was able to successfully write the SQL union operation.

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

I want to add the timestamp results from both caches to a single arraylist. I want to know how to use the query cursor? Now there are two caches and how to do the this part? (QueryCursor<List<?>> cursor = cache.query(sqlTwoCaches))

Or is there any other way to do this?

sachith
  • 129
  • 2
  • 12

1 Answers1

0

You need to "fully qualify" the second cache name, but otherwise, it should Just Work.

Example:

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));          
    }
 }
Stephen Darlington
  • 51,577
  • 12
  • 107
  • 152
  • Not clear what you said. Could you please explain it? – sachith Aug 17 '21 at 10:29
  • Got it. If I have a third cache like this, `IgniteCache cache3 = ignite.cache("table3");` Can I do this? `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> cursor = cache1.query(sqlQ)) { .... ` – sachith Aug 18 '21 at 06:04
  • Your method is not forking. It gives following error. `CacheException: Failed to parse query. Schema "UNCONFIRMED_EVENT_URGENT_MC_79" not found; :` Here is the SQL query. `sql = "Select _val from UnconfirmedEvent where orderId = ? and startTime < ? and endTime > ? and type = %s UNION ALL " + "Select _val from " + String.format(UNCONFIRMED_URGENT_EVENT_CACHE,mcId) + ".UnconfirmedEvent where orderId = ? and startTime < ? and endTime > ? and type = %s " + " order by startTime"; ` – sachith Aug 31 '21 at 09:51
  • It's saying that there's no cache with the name you've given. Which caches are listed when you call `Ignite#cacheNames()`? – Stephen Darlington Aug 31 '21 at 15:53
  • `UNCONFIRMED_EVENT_URGENT_MC_79` and `UNCONFIRMED_EVENT_MC_79` caches were available in the ignite when calling this. – sachith Sep 01 '21 at 04:50
  • This is how I define the caches. `IgniteCache unconfirmedEventCache = ignite.cache(String.format(UNCONFIRMED_NON_URGENT_EVENT_CACHE,mcId)); IgniteCache unconfirmedUrgentEventCache = ignite.cache(String.format(UNCONFIRMED_URGENT_EVENT_CACHE,mcId));` – sachith Sep 01 '21 at 04:52