Hi, I have below sample bean which I am storing as value in cache. I want to
build a map such that it gives me count of trade status for each trade
type(Pls see sample output, done thru java 8 streams). 
Problem with this approach is I have to pull millions of entries from cache
to some collection and manipulate them.

Is there a way to query cache using SQL/ScanQueries to build same map in
more efficient way. Below is my sample code to explain the problem.

public class TradeCacheExample {
        public static void main(String[] args) {
                Trade trade1 = new Trade(1, TradeStatus.NEW, "type1");
                Trade trade2 = new Trade(2, TradeStatus.FAILED, "type2");
                Trade trade3 = new Trade(3, TradeStatus.NEW, "type1");
                Trade trade4 = new Trade(4, TradeStatus.NEW, "type3");
                Trade trade5 = new Trade(5, TradeStatus.CHANGED, "type2");
                Trade trade6 = new Trade(6, TradeStatus.EXPIRED, "type1");
                
                Ignite ignite = 
Ignition.start("examples/config/example-ignite.xml");
                CacheConfiguration<Integer, Trade> config = new
CacheConfiguration<>("mycache");
                config.setIndexedTypes(Integer.class, Trade.class);
                IgniteCache<Integer, Trade> cache = 
ignite.getOrCreateCache(config);
                cache.put(trade1.getId(), trade1);
                cache.put(trade2.getId(), trade2);
                cache.put(trade3.getId(), trade3);
                cache.put(trade4.getId(), trade4);
                cache.put(trade5.getId(), trade5);
                cache.put(trade6.getId(), trade6);
                List<Trade> trades = cache.query(new ScanQuery<Integer,
Trade>()).getAll().stream().map(item->item.getValue()).collect(toList());
                
                Map<String, Map&lt;TradeStatus, Long>> resultMap =
trades.stream().collect(
                                groupingBy(item -> item.getTradeType(), 
groupingBy(Trade::getStatus,
counting())));
                System.out.println(resultMap);
                //{type3={NEW=1}, type2={CHANGED=1, FAILED=1}, 
type1={EXPIRED=1, NEW=2}}
        }
}

public class Trade {
        private int id;
        private TradeStatus status;
        private String tradeType;
        public Trade(int id, TradeStatus status, String tradeType) {
                this.id = id;
                this.status = status;
                this.tradeType = tradeType;
        }
        
//setter getter, equals, hashcode methods
        
        public enum TradeStatus {
                NEW, CHANGED, EXPIRED, FAILED, UNCHANGED
        }




--
View this message in context: 
http://apache-ignite-users.70518.x6.nabble.com/Building-complex-queries-to-query-ignite-Cache-tp9392.html
Sent from the Apache Ignite Users mailing list archive at Nabble.com.

Reply via email to