Java Code Examples for org.apache.ignite.cache.query.QueryCursor#getAll()

The following examples show how to use org.apache.ignite.cache.query.QueryCursor#getAll() . 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: IgniteCacheAbstractQuerySelfTest.java    From ignite with Apache License 2.0 6 votes vote down vote up
/**
 * @param cache Ignite cache.
 * @throws Exception If failed.
 */
private void testPaginationGet(IgniteCache<Integer, Integer> cache) throws Exception {
    for (int i = 0; i < 50; i++)
        cache.put(i, i);

    QueryCursor<Cache.Entry<Integer, Integer>> q =
        cache.query(new SqlQuery<Integer, Integer>(Integer.class, "_key >= 0"));

    List<Cache.Entry<Integer, Integer>> list = new ArrayList<>(q.getAll());

    Collections.sort(list, new Comparator<Cache.Entry<Integer, Integer>>() {
        @Override public int compare(Cache.Entry<Integer, Integer> e1, Cache.Entry<Integer, Integer> e2) {
            return e1.getKey().compareTo(e2.getKey());
        }
    });

    for (int i = 0; i < 50; i++) {
        Cache.Entry<Integer, Integer> e = list.get(i);

        assertEquals(i, (int)e.getKey());
        assertEquals(i, (int)e.getValue());
    }
}
 
Example 2
Source File: IgniteCacheDistributedQueryCancelSelfTest.java    From ignite with Apache License 2.0 6 votes vote down vote up
/** */
@Test
public void testQueryResponseFailCode() throws Exception {
    try (Ignite client = startClientGrid("client")) {

        CacheConfiguration<Integer, Integer> cfg = new CacheConfiguration<>(DEFAULT_CACHE_NAME);
        cfg.setSqlFunctionClasses(Functions.class);
        cfg.setIndexedTypes(Integer.class, Integer.class);
        cfg.setName("test");

        IgniteCache<Integer, Integer> cache = client.getOrCreateCache(cfg);

        cache.put(1, 1);

        QueryCursor<List<?>> qry = cache.query(new SqlFieldsQuery("select fail() from Integer"));

        try {
            qry.getAll();

            fail();
        }
        catch (Exception e) {
            assertTrue(e instanceof CacheException);
        }
    }
}
 
Example 3
Source File: IgniteCacheDistributedJoinQueryConditionsTest.java    From ignite with Apache License 2.0 6 votes vote down vote up
/**
 * @param sql SQL.
 * @param cache Cache.
 * @param enforceJoinOrder Enforce join order flag.
 * @param expSize Expected results size.
 * @param args Arguments.
 */
private void checkQuery(String sql,
    IgniteCache<Object, Object> cache,
    boolean enforceJoinOrder,
    int expSize,
    Object... args) {
    SqlFieldsQuery qry = new SqlFieldsQuery(sql);

    qry.setDistributedJoins(true);
    qry.setEnforceJoinOrder(enforceJoinOrder);
    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());
}
 
Example 4
Source File: IgniteCacheAbstractQuerySelfTest.java    From ignite with Apache License 2.0 6 votes vote down vote up
/**
 * @throws Exception If failed.
 */
@Test
public void testPrimitiveType() throws Exception {
    IgniteCache<Integer, Integer> cache = jcache(Integer.class, Integer.class);
    cache.put(1, 1);
    cache.put(2, 2);

    QueryCursor<Cache.Entry<Integer, Integer>> q =
        cache.query(new SqlQuery<Integer, Integer>(Integer.class, "_val > 1"));

    Collection<Cache.Entry<Integer, Integer>> res = q.getAll();

    assertEquals(1, res.size());

    for (Cache.Entry<Integer, Integer> e : res) {
        assertEquals(2, (int)e.getKey());
        assertEquals(2, (int)e.getValue());
    }
}
 
Example 5
Source File: IgniteCacheDistributedJoinPartitionedAndReplicatedTest.java    From ignite with Apache License 2.0 6 votes vote down vote up
/**
 * @param sql SQL.
 * @param cache Cache.
 * @param enforceJoinOrder Enforce join order flag.
 * @param expSize Expected results size.
 * @param args Arguments.
 * @return Results.
 */
private List<List<?>> checkQuery(String sql,
    IgniteCache<Object, Object> cache,
    boolean enforceJoinOrder,
    int expSize,
    Object... args) {
    SqlFieldsQuery qry = new SqlFieldsQuery(sql);

    qry.setDistributedJoins(true);
    qry.setEnforceJoinOrder(enforceJoinOrder);
    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: SqlQueriesExample.java    From ignite with Apache License 2.0 6 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 sqlFieldsQueryWithJoin() {
    IgniteCache<AffinityKey<Long>, Person> cache = Ignition.ignite().cache(COLLOCATED_PERSON_CACHE);

    // Execute query to get names of all employees.
    String sql =
        "select concat(firstName, ' ', lastName), org.name " +
        "from Person, \"" + ORG_CACHE + "\".Organization as org " +
        "where Person.orgId = org.id";

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

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

    // Print persons' names and organizations' names.
    print("Names of all employees and organizations they belong to: ", res);
}
 
Example 7
Source File: GridCacheFullTextQuerySelfTest.java    From ignite with Apache License 2.0 6 votes vote down vote up
/**
 * Checks cursor entries for correct keys and values.
 * Removes valid entries from expected list copy.
 *
 * @param cursor Query cursor with response
 * @param exp List of expected values.
 * @return Altered expected values list.
 */
@NotNull private static GridCacheFullTextQuerySelfTest.TestPair processExpected(Set<Integer> exp,
    QueryCursor<Cache.Entry<Integer, Person>> cursor) {
    TestPair testPair = new TestPair(exp);

    for (Cache.Entry<Integer, Person> entry : cursor.getAll()) {
        testPair.all.add(entry);

        assertEquals(entry.getKey().toString(), entry.getValue().name);

        assertEquals(entry.getKey().intValue(), entry.getValue().age);

        testPair.expected.remove(entry.getKey());
    }
    return testPair;
}
 
Example 8
Source File: IgniteSqlKeyValueFieldsTest.java    From ignite with Apache License 2.0 6 votes vote down vote up
/** Check automatic addition of index for keyFieldName column */
@Test
public void testAutoKeyFieldIndex() throws Exception {
    IgniteEx client = grid(NODE_CLIENT);
    IgniteCache<Integer, Person> cache = client.cache(CACHE_PERSON);

    QueryCursor<List<?>> cursor = cache.query(new SqlFieldsQuery("explain select * from Person where id = 1"));
    List<List<?>> results = cursor.getAll();
    assertEquals(1, results.size());
    assertTrue(((String)results.get(0).get(0)).contains("\"_key_PK_proxy\""));

    cursor = cache.query(new SqlFieldsQuery("explain select * from Person where _key = 1"));
    results = cursor.getAll();
    assertEquals(1, results.size());
    assertTrue(((String)results.get(0).get(0)).contains("\"_key_PK\""));
}
 
Example 9
Source File: IgniteSqlKeyValueFieldsTest.java    From ignite with Apache License 2.0 6 votes vote down vote up
/** Check that joins are working on keyFieldName, valueFieldName columns */
@Test
public void testJoinKeyValFields() throws Exception {
    IgniteEx client = grid(NODE_CLIENT);
    IgniteCache<Integer, Person> cache = client.cache(CACHE_PERSON);
    IgniteCache<Integer, Integer> cache2 = client.cache(CACHE_JOB);

    checkInsert(cache, "insert into Person (id, v) values (?, ?)", 1, new Person("Bob", 30));
    checkInsert(cache, "insert into Person (id, v) values (?, ?)", 2, new Person("David", 35));
    checkInsert(cache2, "insert into Integer (_key, _val) values (?, ?)", 100, 1);
    checkInsert(cache2, "insert into Integer (_key, _val) values (?, ?)", 200, 2);

    QueryCursor<List<?>> cursor = cache.query(new SqlFieldsQuery("select p.id, j._key from Person p, \"" + CACHE_JOB + "\".Integer j where p.id = j._val"));
    List<List<?>> results = cursor.getAll();
    assertEquals(2, results.size());
    assertEquals(1, results.get(0).get(0));
    assertEquals(100, results.get(0).get(1));
    assertEquals(2, results.get(1).get(0));
    assertEquals(200, results.get(1).get(1));
}
 
Example 10
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 11
Source File: IgniteSqlQueryMinMaxTest.java    From ignite with Apache License 2.0 5 votes vote down vote up
/** Check min() and max() on empty cache */
@Test
public void testQueryMinMaxEmptyCache() throws Exception {
    try (Ignite client = startClientGrid("client")) {
        IgniteCache<Integer, ValueObj> cache = client.cache(CACHE_NAME_2);

        QueryCursor<List<?>> cursor = cache.query(new SqlFieldsQuery("select min(idxVal), max(idxVal) from ValueObj"));
        List<List<?>> result = cursor.getAll();
        assertEquals(1, result.size());
        assertEquals(2, result.get(0).size());
        assertNull(result.get(0).get(0));
        assertNull(result.get(0).get(1));
    }
}
 
Example 12
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));
}
 
Example 13
Source File: IgniteSqlQueryMinMaxTest.java    From ignite with Apache License 2.0 5 votes vote down vote up
/**
 * Check min() and max() over value fields use correct index.
 * Test uses value object cache
 */
@Test
public void testMinMaxQueryPlanOnFields() throws Exception {
    try (Ignite client = startClientGrid("client")) {
        IgniteCache<Integer, ValueObj> cache = client.cache(CACHE_NAME_2);

        QueryCursor<List<?>> cursor = cache.query(new SqlFieldsQuery("explain select min(idxVal), max(idxVal) from ValueObj"));
        List<List<?>> result = cursor.getAll();
        assertEquals(2, result.size());
        assertTrue(((String)result.get(0).get(0)).toLowerCase().contains("idxval_idx"));
        assertTrue(((String)result.get(0).get(0)).toLowerCase().contains("direct lookup"));
    }
}
 
Example 14
Source File: IgniteCacheAbstractQuerySelfTest.java    From ignite with Apache License 2.0 5 votes vote down vote up
/**
 * @throws Exception If failed.
 */
@Test
public void testScanFilters() throws Exception {
    IgniteCache<Integer, Integer> cache = jcache(Integer.class, Integer.class);

    for (int i = 0; i < 50; i++)
        cache.put(i, i);

    QueryCursor<Cache.Entry<Integer, Integer>> q = cache.query(new ScanQuery<>(new IgniteBiPredicate<Integer,Integer>() {
        @Override public boolean apply(Integer k, Integer v) {
            assertNotNull(k);
            assertNotNull(v);

            return k >= 20 && v < 40;
        }
    }));

    List<Cache.Entry<Integer, Integer>> list = new ArrayList<>(q.getAll());

    Collections.sort(list, new Comparator<Cache.Entry<Integer, Integer>>() {
        @Override public int compare(Cache.Entry<Integer, Integer> e1, Cache.Entry<Integer, Integer> e2) {
            return e1.getKey().compareTo(e2.getKey());
        }
    });

    assertEquals(20, list.size());

    for (int i = 20; i < 40; i++) {
        Cache.Entry<Integer, Integer> e = list.get(i - 20);

        assertEquals(i, (int)e.getKey());
        assertEquals(i, (int)e.getValue());
    }
}
 
Example 15
Source File: IgniteCachePartitionedQuerySelfTest.java    From ignite with Apache License 2.0 5 votes vote down vote up
/**
 * @throws Exception If failed.
 */
@Test
public void testFieldsQuery() throws Exception {
    Person p1 = new Person("Jon", 1500);
    Person p2 = new Person("Jane", 2000);
    Person p3 = new Person("Mike", 1800);
    Person p4 = new Person("Bob", 1900);

    IgniteCache<UUID, Person> cache0 = jcache(UUID.class, Person.class);

    cache0.put(p1.id(), p1);
    cache0.put(p2.id(), p2);
    cache0.put(p3.id(), p3);
    cache0.put(p4.id(), p4);

    assertEquals(4, cache0.localSize(ALL));

    // Fields query
    QueryCursor<List<?>> qry = cache0
        .query(new SqlFieldsQuery("select name from Person where salary > ?").setArgs(1600));

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

    assertEquals(3, res.size());

    // Fields query count(*)
    qry = cache0.query(new SqlFieldsQuery("select count(*) from Person"));

    res = qry.getAll();

    int cnt = 0;

    for (List<?> row : res)
        cnt += (Long)row.get(0);

    assertEquals(4, cnt);
}
 
Example 16
Source File: IgniteCacheAbstractFieldsQuerySelfTest.java    From ignite with Apache License 2.0 5 votes vote down vote up
/**
 * Verifies that exactly one record is found when we have equality comparison in where clause (which is supposed
 * to use {@link BPlusTree#findOne(Object, Object)} instead of {@link BPlusTree#find(Object, Object, Object)}.
 *
 * @throws Exception If failed.
 */
@Test
public void testSingleResultUsesFindOne() throws Exception {
    QueryCursor<List<?>> qry =
        intCache.query(sqlFieldsQuery("select _val from Integer where _key = 25"));

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

    assertNotNull(res);
    assertEquals(1, res.size());
    assertEquals(1, res.get(0).size());
    assertEquals(25, res.get(0).get(0));
}
 
Example 17
Source File: IgniteCacheDeleteSqlQuerySelfTest.java    From ignite with Apache License 2.0 4 votes vote down vote up
/**
 * In binary mode, this test checks that inner forcing of keepBinary works - without it, EntryProcessors
 * inside DML engine would compare binary and non-binary objects with the same keys and thus fail.
 */
@Test
public void testDeleteSimpleWithoutKeepBinary() {
    IgniteCache p = ignite(0).cache("S2P");

    QueryCursor<List<?>> c = p.query(new SqlFieldsQuery("delete from Person p where length(p._key) = 2 " +
        "or p.secondName like '%ite'"));

    c.iterator();

    c = p.query(new SqlFieldsQuery("select _key, _val, * from Person order by id"));

    List<List<?>> leftovers = c.getAll();

    assertEquals(2, leftovers.size());

    assertEqualsCollections(Arrays.asList("SecondKey", new Person(2, "Joe", "Black"), 2, "Joe", "Black"),
        leftovers.get(0));

    assertEqualsCollections(Arrays.asList("f0u4thk3y", new Person(4, "Jane", "Silver"), 4, "Jane", "Silver"),
        leftovers.get(1));
}
 
Example 18
Source File: IgniteClientReconnectQueriesTest.java    From ignite with Apache License 2.0 4 votes vote down vote up
/**
 * @param setPart If {@code true} sets partition for scan query.
 * @throws Exception If failed.
 */
private void scanQueryReconnectInProgress(boolean setPart) throws Exception {
    Ignite cln = grid(serverCount());

    assertTrue(cln.cluster().localNode().isClient());

    final Ignite srv = clientRouter(cln);

    final IgniteCache<Integer, Person> clnCache = cln.getOrCreateCache(QUERY_CACHE);

    clnCache.put(1, new Person(1, "name1", "surname1"));
    clnCache.put(2, new Person(2, "name2", "surname2"));
    clnCache.put(3, new Person(3, "name3", "surname3"));

    final ScanQuery<Integer, Person> scanQry = new ScanQuery<>();

    scanQry.setPageSize(1);

    scanQry.setFilter(new IgniteBiPredicate<Integer, Person>() {
        @Override public boolean apply(Integer integer, Person person) {
            return true;
        }
    });

    if (setPart)
        scanQry.setPartition(1);

    blockMessage(GridCacheQueryResponse.class);

    final IgniteInternalFuture<Object> fut = GridTestUtils.runAsync(new Callable<Object>() {
        @Override public Object call() throws Exception {
            try {
                QueryCursor<Cache.Entry<Integer, Person>> qryCursor = clnCache.query(scanQry);

                qryCursor.getAll();
            }
            catch (CacheException e) {
                checkAndWait(e);

                return true;
            }

            return false;
        }
    });

    // Check that client waiting operation.
    GridTestUtils.assertThrows(log, new Callable<Object>() {
        @Override public Object call() throws Exception {
            return fut.get(200);
        }
    }, IgniteFutureTimeoutCheckedException.class, null);

    assertNotDone(fut);

    unblockMessage();

    reconnectClientNode(cln, srv, null);

    assertTrue((Boolean)fut.get(2, SECONDS));

    QueryCursor<Cache.Entry<Integer, Person>> qryCursor2 = clnCache.query(scanQry);

    List<Cache.Entry<Integer, Person>> entries = qryCursor2.getAll();

    assertEquals(setPart ? 1 : 3, entries.size());

    for (Cache.Entry<Integer, Person> entry : entries) {
        assertEquals(Integer.class, entry.getKey().getClass());
        assertEquals(Person.class, entry.getValue().getClass());
    }
}
 
Example 19
Source File: IgniteCacheAbstractQuerySelfTest.java    From ignite with Apache License 2.0 4 votes vote down vote up
/**
 * Tests UDFs.
 *
 * @throws IgniteCheckedException If failed.
 */
@Test
public void testUserDefinedFunction() throws IgniteCheckedException {
    // Without alias.
    final IgniteCache<Object, Object> cache = jcache(Object.class, Object.class);

    QueryCursor<List<?>> qry = cache.query(new SqlFieldsQuery("select square(1), square(2)"));

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

    assertEquals(1, res.size());

    List<?> row = res.iterator().next();

    assertEquals(1, row.get(0));
    assertEquals(4, row.get(1));

    // With alias.
    qry = cache.query(new SqlFieldsQuery("select _cube_(1), _cube_(2)"));

    res = qry.getAll();

    assertEquals(1, res.size());

    row = res.iterator().next();

    assertEquals(1, row.get(0));
    assertEquals(8, row.get(1));

    // Not registered.
    GridTestUtils.assertThrows(
        log,
        new Callable<Object>() {
            @Override public Object call() throws Exception {
                cache.query(new SqlFieldsQuery("select no()"));

                return null;
            }
        },
        CacheException.class,
        null
    );
}
 
Example 20
Source File: IgniteCacheDeleteSqlQuerySelfTest.java    From ignite with Apache License 2.0 4 votes vote down vote up
/**
 *
 */
@Test
public void testDeleteSingle() {
    IgniteCache p = cache();

    QueryCursor<List<?>> c = p.query(new SqlFieldsQuery("delete from Person where _key = ?")
        .setArgs("FirstKey"));

    c.iterator();

    c = p.query(new SqlFieldsQuery("select _key, _val, * from Person order by id, _key"));

    List<List<?>> leftovers = c.getAll();

    assertEquals(3, leftovers.size());

    assertEqualsCollections(Arrays.asList("SecondKey", createPerson(2, "Joe", "Black"), 2, "Joe", "Black"),
        leftovers.get(0));

    assertEqualsCollections(Arrays.asList("k3", createPerson(3, "Sylvia", "Green"), 3, "Sylvia", "Green"),
        leftovers.get(1));

    assertEqualsCollections(Arrays.asList("f0u4thk3y", createPerson(4, "Jane", "Silver"), 4, "Jane", "Silver"),
        leftovers.get(2));
}