org.apache.ignite.cache.query.QueryCursor Java Examples

The following examples show how to use org.apache.ignite.cache.query.QueryCursor. You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example #1
Source File: IgniteQueryDedicatedPoolTest.java    From ignite with Apache License 2.0 6 votes vote down vote up
/**
 * Tests that SPI queries are executed in dedicated pool
 * @throws Exception If failed.
 */
@Test
public void testSpiQueryUsesDedicatedThreadPool() throws Exception {
    startGrid("server");

    try (Ignite client = startClientGrid("client")) {
        IgniteCache<Byte, Byte> cache = client.cache(CACHE_NAME);

        for (byte b = 0; b < Byte.MAX_VALUE; ++b)
            cache.put(b, b);

        QueryCursor<Cache.Entry<Byte, Byte>> cursor = cache.query(new SpiQuery<Byte, Byte>());

        List<Cache.Entry<Byte, Byte>> all = cursor.getAll();

        assertEquals(1, all.size());
        assertEquals(GridIoPolicy.QUERY_POOL, (byte)all.get(0).getValue());

        cursor.close();
    }
}
 
Example #2
Source File: FunctionalQueryTest.java    From ignite with Apache License 2.0 6 votes vote down vote up
/**
 * @param cache Cache.
 * @param minId Minimal ID.
 * @param pageSize Page size.
 * @param expSize The size of the expected results.
 * @param exp Expected results.
 * @param lazy Lazy mode flag.
 */
private void checkSqlFieldsQuery(ClientCache<Integer, Person> cache, int minId, int pageSize, int expSize,
    Map<Integer, Person> exp, boolean lazy) {
    SqlFieldsQuery qry = new SqlFieldsQuery("select id, name from Person where id >= ?")
        .setArgs(minId)
        .setPageSize(pageSize)
        .setLazy(lazy);

    try (QueryCursor<List<?>> cur = cache.query(qry)) {
        List<List<?>> res = cur.getAll();

        assertEquals(expSize, res.size());

        Map<Integer, Person> act = res.stream().collect(Collectors.toMap(
            r -> Integer.parseInt(r.get(0).toString()),
            r -> new Person(Integer.parseInt(r.get(0).toString()), r.get(1).toString())
        ));

        assertEquals(exp, act);
    }
}
 
Example #3
Source File: IgniteCacheRandomOperationBenchmark.java    From ignite with Apache License 2.0 6 votes vote down vote up
/** {@inheritDoc} */
@Override public void run() {
    IgniteCache cache = node.cache(cacheName);

    // Getting a list of the partitions owned by this node.
    List<Integer> myPartitions = cachePart.get(node.cluster().localNode().id());

    for (Integer part : myPartitions) {

        ScanQuery scanQry = new ScanQuery();

        scanQry.setPartition(part);

        scanQry.setFilter(igniteBiPred);

        try (QueryCursor cursor = cache.query(scanQry)) {
            for (Object obj : cursor) {
                // No-op.
            }
        }

    }
}
 
Example #4
Source File: IgniteSqlSplitterSelfTest.java    From ignite with Apache License 2.0 6 votes vote down vote up
/**
 * Check results of aggregate functions if no rows are selected.
 *
 * @throws Exception If failed,
 */
@Test
public void testEmptyCacheAggregates() throws Exception {
    final String cacheName = "ints";

    IgniteCache<Integer, Value> cache = ignite(0).getOrCreateCache(cacheConfig(cacheName, true,
        Integer.class, Value.class));

    try (QueryCursor<List<?>> qry = cache.query(new SqlFieldsQuery(
        "SELECT count(fst), sum(snd), avg(snd), min(snd), max(snd) FROM Value"))) {
        List<List<?>> result = qry.getAll();

        assertEquals(1, result.size());

        List<?> row = result.get(0);

        assertEquals("count", 0L, ((Number)row.get(0)).longValue());
        assertEquals("sum", null, row.get(1));
        assertEquals("avg", null, row.get(2));
        assertEquals("min", null, row.get(3));
        assertEquals("max", null, row.get(4));
    }
    finally {
        cache.destroy();
    }
}
 
Example #5
Source File: IgniteCacheDistributedJoinNoIndexTest.java    From ignite with Apache License 2.0 6 votes vote down vote up
/**
 * @param sql SQL.
 * @param cache Cache.
 * @param expSize Expected results size.
 * @param args Arguments.
 * @return Results.
 */
private List<List<?>> checkQuery(String sql,
    IgniteCache<Object, Object> cache,
    int expSize,
    Object... args) {
    SqlFieldsQuery qry = new SqlFieldsQuery(sql);

    qry.setDistributedJoins(true);
    qry.setArgs(args);

    log.info("Plan: " + queryPlan(cache, qry));

    QueryCursor<List<?>> cur = cache.query(qry);

    List<List<?>> res = cur.getAll();

    if (expSize != res.size())
        log.info("Results: " + res);

    assertEquals(expSize, res.size());

    return res;
}
 
Example #6
Source File: IgniteSqlSplitterSelfTest.java    From ignite with Apache License 2.0 6 votes vote down vote up
/** Simple query with distinct aggregates */
private void checkSimpleQueryWithAggrMixed(IgniteCache<Integer, Value> cache) {
    try (QueryCursor<List<?>> qry = cache.query(new SqlFieldsQuery(
        "SELECT count(fst), sum(snd), avg(snd), min(snd), max(snd)," +
            "count(distinct fst), sum(distinct snd), avg(distinct snd), min(distinct snd), max(distinct snd)  " +
            "FROM Value"))) {
        List<List<?>> result = qry.getAll();

        assertEquals(1, result.size());

        List<?> row = result.get(0);

        assertEquals("count", 15L, ((Number)row.get(0)).longValue());
        assertEquals("sum", 30L, ((Number)row.get(1)).longValue());
        assertEquals("avg", 2, ((Integer)row.get(2)).intValue());
        assertEquals("min", 1, ((Integer)row.get(3)).intValue());
        assertEquals("max", 3, ((Integer)row.get(4)).intValue());
        assertEquals("count distinct", 6L, ((Number)row.get(5)).longValue());
        assertEquals("sum distinct", 6L, ((Number)row.get(6)).longValue());
        assertEquals("avg distinct", 2, ((Integer)row.get(7)).intValue());
        assertEquals("min distinct", 1, ((Integer)row.get(8)).intValue());
        assertEquals("max distinct", 3, ((Integer)row.get(9)).intValue());
    }
}
 
Example #7
Source File: IgniteCacheContinuousQueryBackupQueueTest.java    From ignite with Apache License 2.0 6 votes vote down vote up
/**
 * @throws Exception If failed.
 */
@Test
public void testBackupQueue() throws Exception {
    final CacheEventListener lsnr = new CacheEventListener();

    ContinuousQuery<Object, Object> qry = new ContinuousQuery<>();

    qry.setLocalListener(lsnr);
    qry.setRemoteFilterFactory(new AlwaysFalseFilterFactory());

    try (QueryCursor<?> ignore = grid(0).cache(CACHE_NAME).query(qry)) {
        for (int i = 0; i < KEYS_COUNT; i++) {
            log.info("Put key: " + i);

            for (int j = 0; j < 100; j++)
                grid(j % GRID_COUNT).cache(CACHE_NAME).put(i, new byte[1024 * 50]);
        }

        log.info("Finish.");
    }
}
 
Example #8
Source File: SqlQueriesExample.java    From ignite with Apache License 2.0 6 votes vote down vote up
/**
 * Example for SQL queries to calculate average salary for a specific organization.
 */
private static void sqlQueryWithAggregation() {
    IgniteCache<AffinityKey<Long>, Person> cache = Ignition.ignite().cache(COLLOCATED_PERSON_CACHE);

    // Calculate average of salary of all persons in ApacheIgnite.
    // Note that we also join on Organization cache as well.
    String sql =
        "select avg(salary) " +
        "from Person, \"" + ORG_CACHE + "\".Organization as org " +
        "where Person.orgId = org.id " +
        "and lower(org.name) = lower(?)";

    QueryCursor<List<?>> cursor = cache.query(new SqlFieldsQuery(sql).setArgs("ApacheIgnite"));

    // Calculate average salary for a specific organization.
    print("Average salary for 'ApacheIgnite' employees: ", cursor.getAll());
}
 
Example #9
Source File: CacheContinuousQueryConcurrentPartitionUpdateTest.java    From ignite with Apache License 2.0 6 votes vote down vote up
/**
 * @param cache Cache.
 * @return Event counter.
 */
private T2<AtomicInteger, QueryCursor> startListener(IgniteCache<Object, Object> cache) {
    final AtomicInteger evtCnt = new AtomicInteger();

    ContinuousQuery<Object, Object> qry = new ContinuousQuery<>();

    qry.setLocalListener(new CacheEntryUpdatedListener<Object, Object>() {
        @Override public void onUpdated(Iterable<CacheEntryEvent<?, ?>> evts) {
            for (CacheEntryEvent evt : evts) {
                assertNotNull(evt.getKey());
                assertNotNull(evt.getValue());

                if ((Integer)evt.getValue() >= 0)
                    evtCnt.incrementAndGet();
            }
        }
    });

    QueryCursor cur = cache.query(qry);

    return new T2<>(evtCnt, cur);
}
 
Example #10
Source File: IgniteCacheRandomOperationBenchmark.java    From ignite with Apache License 2.0 6 votes vote down vote up
/**
 * @param cache Ignite cache.
 * @param map Parameters map.
 * @throws Exception If failed.
 */
private void doContinuousQuery(IgniteCache<Object, Object> cache, Map<Object, Object> map) throws Exception {
    List<QueryCursor> cursors = (ArrayList<QueryCursor>)map.get(cache.getName());

    if (cursors == null) {
        cursors = new ArrayList<>(CONTINUOUS_QUERY_PER_CACHE);
        map.put(cache.getName(), cursors);
    }

    if (cursors.size() == CONTINUOUS_QUERY_PER_CACHE) {
        QueryCursor cursor = cursors.get(nextRandom(cursors.size()));
        cursor.close();
        cursors.remove(cursor);
    }

    ContinuousQuery qry = new ContinuousQuery();

    qry.setLocalListener(new ContinuousQueryUpdater());

    qry.setRemoteFilterFactory(FactoryBuilder.factoryOf(new ContinuousQueryFilter()));

    cursors.add(cache.query(qry));
}
 
Example #11
Source File: IgniteCacheAbstractFieldsQuerySelfTest.java    From ignite with Apache License 2.0 6 votes vote down vote up
/** @throws Exception If failed. */
@Test
public void testPagination() throws Exception {
    // Query with page size 20.
    QueryCursor<List<?>> qry =
        intCache.query(sqlFieldsQuery("select * from Integer").setPageSize(20));

    List<List<?>> res = new ArrayList<>(qry.getAll());

    dedup(res);

    Collections.sort(res, new Comparator<List<?>>() {
        @Override public int compare(List<?> r1, List<?> r2) {
            return ((Integer)r1.get(0)).compareTo((Integer)r2.get(0));
        }
    });

    assertEquals(200, res.size());

    for (List<?> row : res)
        assertEquals("Wrong row size: " + row, 2, row.size());
}
 
Example #12
Source File: CacheScanPartitionQueryFallbackSelfTest.java    From ignite with Apache License 2.0 6 votes vote down vote up
/**
 * Scan (with explicit {@code setLocal(true)}, no partition specified) should perform on the local node.
 *
 * @throws Exception If failed.
 */
@Test
public void testScanLocalExplicitNoPart() throws Exception {
    cacheMode = CacheMode.PARTITIONED;
    backups = 0;
    commSpiFactory = new TestLocalCommunicationSpiFactory();

    try {
        Ignite ignite = startGrids(GRID_CNT);

        IgniteCacheProxy<Integer, Integer> cache = fillCache(ignite);

        QueryCursor<Cache.Entry<Integer, Integer>> qry =
            cache.query(new ScanQuery<Integer, Integer>().setLocal(true));

        assertFalse(qry.getAll().isEmpty());
    }
    finally {
        stopAllGrids();
    }
}
 
Example #13
Source File: GridCacheContinuousQueryAbstractSelfTest.java    From ignite with Apache License 2.0 6 votes vote down vote up
/**
 * @throws Exception if failed.
 * @param bypassFilter Whether remote filter should be bypassed.
 * @param setLocLsnr Whether local listner should be setted.
 */
private void doQueryWithRemoteFilter(boolean setLocLsnr, boolean bypassFilter) throws Exception {
    FILTERED.clear();

    ContinuousQuery<Integer, Integer> qry = new ContinuousQuery<>();

    Map<Integer, Integer> listened = new ConcurrentHashMap<>();

    if (setLocLsnr) {
        qry.setLocalListener(new CacheEntryUpdatedListener<Integer, Integer>() {
            @Override public void onUpdated(Iterable<CacheEntryEvent<? extends Integer, ? extends Integer>> evts) {
                evts.forEach(event -> listened.put(event.getKey(), event.getValue()));
            }
        });
    }

    qry.setRemoteFilter(evt -> {
            FILTERED.put(evt.getKey(), evt.getValue());

            return bypassFilter;
        });

    try (QueryCursor<Cache.Entry<Integer, Integer>> qryCursor = grid(0).cache(DEFAULT_CACHE_NAME).query(qry)) {
        checkLsnrAndFilterResults(setLocLsnr, bypassFilter, listened);
    }
}
 
Example #14
Source File: IgniteDbPutGetAbstractTest.java    From ignite with Apache License 2.0 6 votes vote down vote up
/**
 * @param total Expected total entries.
 */
private void checkScan(int total) {
    for (int i = 0; i < gridCount(); i++) {
        Set<DbKey> allKeys = new HashSet<>();

        Ignite ignite0 = grid(i);

        IgniteCache<DbKey, DbValue> cache0 = ignite0.cache("non-primitive");

        ScanQuery<DbKey, DbValue> qry = new ScanQuery<>();

        QueryCursor<Cache.Entry<DbKey, DbValue>> cur = cache0.query(qry);

        for (Cache.Entry<DbKey, DbValue> e : cur) {
            allKeys.add(e.getKey());
            assertEquals(e.getKey().val, e.getValue().iVal);
        }

        assertEquals(total, allKeys.size());
    }
}
 
Example #15
Source File: IgniteCacheAbstractQuerySelfTest.java    From ignite with Apache License 2.0 6 votes vote down vote up
/**
 * @throws Exception If failed.
 */
@Test
public void testFieldsQueryMetadata() throws Exception {
    IgniteCache<UUID, Person> cache = jcache(UUID.class, Person.class);

    for (int i = 0; i < 100; i++)
        cache.put(UUID.randomUUID(), new Person("name-" + i, (i + 1) * 100));

    QueryCursor<List<?>> cur = cache.query(new SqlFieldsQuery("select name, salary from Person where name like ?")
        .setArgs("name-"));

    assertTrue(cur instanceof QueryCursorEx);

    QueryCursorEx<List<?>> curEx = (QueryCursorEx<List<?>>)cur;

    List<GridQueryFieldMetadata> meta = curEx.fieldsMeta();

    assertNotNull(meta);
    assertEquals(2, meta.size());
}
 
Example #16
Source File: OdbcUtils.java    From ignite with Apache License 2.0 6 votes vote down vote up
/**
 * Get affected rows for statement.
 * @param qryCur Cursor.
 * @return Number of table rows affected, if the query is DML, and -1 otherwise.
 */
public static long rowsAffected(QueryCursor<List<?>> qryCur) {
    QueryCursorImpl<List<?>> qryCur0 = (QueryCursorImpl<List<?>>)qryCur;

    if (qryCur0.isQuery())
        return -1;

    Iterator<List<?>> iter = qryCur0.iterator();

    if (iter.hasNext()) {
        List<?> res = iter.next();

        if (!res.isEmpty()) {
            Long affected = (Long) res.get(0);

            if (affected != null)
                return affected;
        }
    }

    return 0;
}
 
Example #17
Source File: H2ResultSetIteratorNullifyOnEndSelfTest.java    From ignite with Apache License 2.0 5 votes vote down vote up
/**
 * Extract H2ResultSetIterator by reflection for SQL Fields cases.
 *
 * @param qryCurs source cursor
 * @return target iterator or null of not extracted
 */
private H2ResultSetIterator extractGridIteratorInnerH2ResultSetIterator(QueryCursor<List<?>> qryCurs) {
    if (QueryCursorImpl.class.isAssignableFrom(qryCurs.getClass())) {
        GridQueryCacheObjectsIterator it = GridTestUtils.getFieldValue(qryCurs, QueryCursorImpl.class, "iter");

        Iterator<List<?>> h2RsIt = GridTestUtils.getFieldValue(it, GridQueryCacheObjectsIterator.class, "iter");

        if (H2ResultSetIterator.class.isAssignableFrom(h2RsIt.getClass()))
            return (H2ResultSetIterator)h2RsIt;
    }
    return null;
}
 
Example #18
Source File: TcpClientCache.java    From ignite with Apache License 2.0 5 votes vote down vote up
/** Handle scan query. */
private QueryCursor<Cache.Entry<K, V>> scanQuery(ScanQuery<K, V> qry) {
    Consumer<PayloadOutputChannel> qryWriter = payloadCh -> {
        writeCacheInfo(payloadCh);

        BinaryOutputStream out = payloadCh.out();

        if (qry.getFilter() == null)
            out.writeByte(GridBinaryMarshaller.NULL);
        else {
            serDes.writeObject(out, qry.getFilter());
            out.writeByte((byte)1); // Java platform
        }

        out.writeInt(qry.getPageSize());
        out.writeInt(qry.getPartition() == null ? -1 : qry.getPartition());
        out.writeBoolean(qry.isLocal());
    };

    return new ClientQueryCursor<>(new ClientQueryPager<>(
        ch,
        ClientOperation.QUERY_SCAN,
        ClientOperation.QUERY_SCAN_CURSOR_GET_PAGE,
        qryWriter,
        keepBinary,
        marsh
    ));
}
 
Example #19
Source File: IgniteCacheConfigVariationsQueryTest.java    From ignite with Apache License 2.0 5 votes vote down vote up
/**
 * @param expMap Expected map.
 * @param cursor Query cursor.
 */
private void checkQueryResults(Map<Object, Object> expMap, QueryCursor<Cache.Entry<Object, Object>> cursor)
    throws InterruptedException {
    Iterator<Cache.Entry<Object, Object>> iter = cursor.iterator();

    try {
        assertNotNull(iter);

        int cnt = 0;

        while (iter.hasNext()) {
            Cache.Entry<Object, Object> e = iter.next();

            assertNotNull(e.getKey());
            assertNotNull(e.getValue());

            Object expVal = expMap.get(e.getKey());

            assertNotNull("Failed to resolve expected value for key: " + e.getKey(), expVal);

            assertEquals(expVal, e.getValue());

            cnt++;
        }

        assertEquals(expMap.size(), cnt);
    }
    finally {
        cursor.close();
    }

    checkEvents();
}
 
Example #20
Source File: GatewayProtectedCacheProxy.java    From ignite with Apache License 2.0 5 votes vote down vote up
/** {@inheritDoc} */
@Override public <R> QueryCursor<R> query(Query<R> qry) {
    CacheOperationGate opGate = onEnter();

    try {
        return delegate.query(qry);
    }
    finally {
        onLeave(opGate);
    }
}
 
Example #21
Source File: IgniteRepositoryQuery.java    From ignite with Apache License 2.0 5 votes vote down vote up
/**
 * {@inheritDoc} @param values the values
 *
 * @return the object
 */
@Override public Object execute(Object[] values) {
    Object[] parameters = values;

    // config via Query annotation (dynamicQuery = false)
    DynamicQueryConfig config = staticQueryConfiguration;

    // or condition to allow query tunning
    if (config == null || dynamicQueryConfigurationIndex != -1) {
        DynamicQueryConfig newConfig = (DynamicQueryConfig)values[dynamicQueryConfigurationIndex];
        parameters = ArrayUtils.removeElement(parameters, dynamicQueryConfigurationIndex);
        if (newConfig != null) {
            // upset query configuration
            config = newConfig;
        }
    }
    // query configuration is required, via Query annotation or per parameter (within provided values param)
    if (config == null) {
        throw new IllegalStateException(
            "Unable to execute query. When passing dynamicQuery = true via org.apache.ignite.springdata"
                + ".repository.config.Query annotation, you must provide a non null method parameter of type "
                + "DynamicQueryConfig");
    }

    IgniteQuery qry = getQuery(config);

    ReturnStrategy returnStgy = getReturnStgy(qry);

    Query iQry = prepareQuery(qry, config, returnStgy, parameters);

    QueryCursor qryCursor = cache.query(iQry);

    return transformQueryCursor(qry, returnStgy, parameters, qryCursor);
}
 
Example #22
Source File: IgniteCachePartitionedFieldsQuerySelfTest.java    From ignite with Apache License 2.0 5 votes vote down vote up
/**
 * Execute given query locally and check results.
 * @param cache Cache to run query on.
 * @param fldsQry Query.
 */
private void doTestLocalQuery(IgniteCache<?, ?> cache, SqlFieldsQuery fldsQry) throws InterruptedException {
    awaitPartitionMapExchange(true, true, null);

    int exp = 0;

    for (Cache.Entry e: intCache.localEntries(CachePeekMode.PRIMARY)) {
        if (e.getValue() instanceof Integer)
            exp++;
    }

    QueryCursor<List<?>> qry = cache.query(fldsQry.setLocal(true));

    assertEquals(exp, qry.getAll().size());
}
 
Example #23
Source File: GatewayProtectedCacheProxy.java    From ignite with Apache License 2.0 5 votes vote down vote up
/** {@inheritDoc} */
@Override public <T, R> QueryCursor<R> query(Query<T> qry, IgniteClosure<T, R> transformer) {
    CacheOperationGate opGate = onEnter();

    try {
        return delegate.query(qry, transformer);
    }
    finally {
        onLeave(opGate);
    }
}
 
Example #24
Source File: StaticCacheDdlTest.java    From ignite with Apache License 2.0 5 votes vote down vote up
/**
 * @param ignite Ignite instance.
 * @param expSize Expected size.
 */
private void checkTableSize(Ignite ignite, String cacheName, int expSize) {
    SqlFieldsQuery q = new SqlFieldsQuery(
        "SELECT * FROM " + cacheName + "." + TABLE_NAME );

    try (QueryCursor<List<?>> cursor = ignite.cache(cacheName).query(q)) {
        int actualSize = 0;

        for (List<?> ignore : cursor)
            actualSize++;

        Assert.assertEquals("Check result set size", expSize, actualSize);
    }
}
 
Example #25
Source File: IgniteCacheClusterReadOnlyModeSelfTest.java    From ignite with Apache License 2.0 5 votes vote down vote up
/** */
@Test
public void testScanQueryAllowed() {
    performAction(cache -> {
        try (QueryCursor qry = cache.query(new ScanQuery<>())) {
            for (Object o : qry.getAll()) {
                IgniteBiTuple<Integer, Integer> tuple = (IgniteBiTuple<Integer, Integer>)o;

                assertEquals(o.toString(), kvMap.get(tuple.getKey()), tuple.getValue());
            }
        }
    });
}
 
Example #26
Source File: GridCacheContinuousQueryAbstractSelfTest.java    From ignite with Apache License 2.0 5 votes vote down vote up
/**
 * @throws Exception If failed.
 */
@SuppressWarnings("TryFinallyCanBeTryWithResources")
@Test
public void testNodeJoinWithoutCache() throws Exception {
    IgniteCache<Integer, Integer> cache = grid(0).cache(DEFAULT_CACHE_NAME);

    ContinuousQuery<Integer, Integer> qry = new ContinuousQuery<>();

    final CountDownLatch latch = new CountDownLatch(1);

    qry.setLocalListener(new CacheEntryUpdatedListener<Integer, Integer>() {
        @Override public void onUpdated(Iterable<CacheEntryEvent<? extends Integer, ? extends Integer>> evts) {
            latch.countDown();
        }
    });

    QueryCursor<Cache.Entry<Integer, Integer>> cur = cache.query(qry);

    try {
        try (Ignite ignite = startClientGrid(NO_CACHE_IGNITE_INSTANCE_NAME)) {
            log.info("Started node without cache: " + ignite);
        }

        cachePut(cache, 1, 1);

        assertTrue(latch.await(5000, MILLISECONDS));
    }
    finally {
        cur.close();
    }
}
 
Example #27
Source File: ClientCacheScanQueryRequest.java    From ignite with Apache License 2.0 5 votes vote down vote up
/** {@inheritDoc} */
@Override public ClientResponse process(ClientConnectionContext ctx) {
    IgniteCache cache = filterPlatform == FILTER_PLATFORM_JAVA && !isKeepBinary() ? rawCache(ctx) : cache(ctx);

    ScanQuery qry = new ScanQuery()
        .setLocal(loc)
        .setPageSize(pageSize)
        .setPartition(part)
        .setFilter(createFilter(ctx));

    ctx.incrementCursors();

    try {
        QueryCursor cur = cache.query(qry);

        ClientCacheEntryQueryCursor cliCur = new ClientCacheEntryQueryCursor(cur, pageSize, ctx);

        long cursorId = ctx.resources().put(cliCur);

        cliCur.id(cursorId);

        return new ClientCacheQueryResponse(requestId(), cliCur);
    }
    catch (Exception e) {
        ctx.decrementCursors();

        throw e;
    }
}
 
Example #28
Source File: SqlQueriesExample.java    From ignite with Apache License 2.0 5 votes vote down vote up
/**
 * Example for SQL-based fields queries that return only required
 * fields instead of whole key-value pairs.
 */
private static void sqlFieldsQuery() {
    IgniteCache<Long, Person> cache = Ignition.ignite().cache(PERSON_CACHE);

    // Execute query to get names of all employees.
    QueryCursor<List<?>> cursor = cache.query(new SqlFieldsQuery(
        "select concat(firstName, ' ', lastName) from Person"));

    // In this particular case each row will have one element with full name of an employees.
    List<List<?>> res = cursor.getAll();

    // Print names.
    print("Names of all employees:", res);
}
 
Example #29
Source File: IgniteSqlKeyValueFieldsTest.java    From ignite with Apache License 2.0 5 votes vote down vote up
/** */
private void checkSelect(IgniteCache<?, ?> cache, String selectQry, Object... expected) {
    QueryCursor<List<?>> cursor = cache.query(new SqlFieldsQuery(selectQry));

    List<List<?>> results = cursor.getAll();

    assertEquals(1, results.size());

    List<?> row0 = results.get(0);
    for (int col = 0; col < expected.length; ++col)
        assertEquals(expected[col], row0.get(col));
}
 
Example #30
Source File: IgniteSqlKeyValueFieldsTest.java    From ignite with Apache License 2.0 5 votes vote down vote up
/** */
private GridCacheVersion getVersion(IgniteCache<?, ?> cache, int key) {
    QueryCursor<List<?>> cursor = cache.query(new SqlFieldsQuery("select _ver from Person where id = ?").setArgs(key));
    List<List<?>> results = cursor.getAll();
    assertEquals(1, results.size());
    return ((GridCacheVersion) results.get(0).get(0));
}