Java Code Examples for org.apache.ignite.cache.query.SqlQuery#setLocal()

The following examples show how to use org.apache.ignite.cache.query.SqlQuery#setLocal() . 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: IgniteCacheLocalQuerySelfTest.java    From ignite with Apache License 2.0 6 votes vote down vote up
/** {@inheritDoc} */
@Test
@Override public void testLocalSqlQueryFromClient() throws Exception {
    try (Ignite g = startClientGrid("client")) {
        IgniteCache<Integer, Integer> c = jcache(g, Integer.class, Integer.class);

        for (int i = 0; i < 10; i++)
            c.put(i, i);

        SqlQuery<Integer, Integer> qry = new SqlQuery<>(Integer.class, "_key >= 5 order by _key");

        qry.setLocal(true);

        try (QueryCursor<Cache.Entry<Integer, Integer>> qryCursor = c.query(qry)) {
            assertNotNull(qryCursor);

            List<Cache.Entry<Integer, Integer>> res = qryCursor.getAll();

            assertNotNull(res);

            assertEquals(5, res.size());
        }
    }
}
 
Example 2
Source File: CacheSqlQueryValueCopySelfTest.java    From ignite with Apache License 2.0 6 votes vote down vote up
/**
 * Tests local sql query.
 */
@Test
public void testLocalSqlQuery() {
    IgniteCache<Integer, Value> cache = grid(0).cache(DEFAULT_CACHE_NAME);

    SqlQuery<Integer, Value> qry = new SqlQuery<>(Value.class.getSimpleName(), "select * from Value");
    qry.setLocal(true);

    List<Cache.Entry<Integer, Value>> all = cache.query(qry).getAll();

    assertFalse(all.isEmpty());

    for (Cache.Entry<Integer, Value> entry : all)
        entry.getValue().str = "after";

    check(cache);
}
 
Example 3
Source File: IgniteCacheAbstractQuerySelfTest.java    From ignite with Apache License 2.0 6 votes vote down vote up
/**
 * @throws Exception If failed.
 */
@Test
public void testLocalSqlQueryFromClient() throws Exception {
    try (Ignite g = startClientGrid("client")) {
        IgniteCache<Integer, Integer> c = jcache(g, Integer.class, Integer.class);

        for (int i = 0; i < 10; i++)
            c.put(i, i);

        SqlQuery<Integer, Integer> qry = new SqlQuery<>(Integer.class, "_key >= 5 order by _key");

        qry.setLocal(true);

        assertThrowsWithCause(() -> c.query(qry), CacheException.class);
    }
}
 
Example 4
Source File: IgniteBinaryObjectQueryArgumentsTest.java    From ignite with Apache License 2.0 5 votes vote down vote up
/**
 * Test simple query by key.
 *
 * @param cacheName Cache name.
 * @param key1 Key 1.
 * @param key2 Key 2.
 * @param <T> Key type.
 */
private <T> void testKeyQuery(final String cacheName, final T key1, final T key2) {
    final IgniteCache<T, Person> cache = ignite(0).cache(cacheName);

    final Person p1 = new Person("p1");
    final Person p2 = new Person("p2");

    cache.put(key1, p1);
    cache.put(key2, p2);

    final SqlQuery<T, Person> qry = new SqlQuery<>(Person.class, "where _key=?");

    final SqlFieldsQuery fieldsQry = new SqlFieldsQuery("select _key, _val, * from Person where _key=?");

    qry.setLocal(isLocal());
    fieldsQry.setLocal(isLocal());

    qry.setArgs(key1);
    fieldsQry.setArgs(key1);

    final List<Cache.Entry<T, Person>> res = cache.query(qry).getAll();
    final List<List<?>> fieldsRes = cache.query(fieldsQry).getAll();

    assertEquals(1, res.size());
    assertEquals(1, fieldsRes.size());

    assertEquals(p1, res.get(0).getValue());
    assertEquals(key1, res.get(0).getKey());

    assertTrue(fieldsRes.get(0).size() >= 2);
    assertEquals(key1, fieldsRes.get(0).get(0));
    assertEquals(p1, fieldsRes.get(0).get(1));
}
 
Example 5
Source File: IgniteBinaryObjectQueryArgumentsTest.java    From ignite with Apache License 2.0 5 votes vote down vote up
/**
 * Test simple query by value.
 *
 * @param cacheName Cache name.
 * @param val1 Value 1.
 * @param val2 Value 2.
 * @param <T> Value type.
 */
private <T> void testValQuery(final String cacheName, final T val1, final T val2) {
    final IgniteCache<Person, T> cache = ignite(0).cache(cacheName);

    final Class<?> valType = val1.getClass();

    final Person p1 = new Person("p1");
    final Person p2 = new Person("p2");

    cache.put(p1, val1);
    cache.put(p2, val2);

    final SqlQuery<Person, T> qry = new SqlQuery<>(valType, "where _val=?");

    final SqlFieldsQuery fieldsQry = new SqlFieldsQuery("select _key, _val, * from " + valType.getSimpleName() + " where _val=?");

    qry.setLocal(isLocal());
    fieldsQry.setLocal(isLocal());

    qry.setArgs(val1);
    fieldsQry.setArgs(val1);

    final List<Cache.Entry<Person, T>> res = cache.query(qry).getAll();
    final List<List<?>> fieldsRes = cache.query(fieldsQry).getAll();

    assertEquals(1, res.size());
    assertEquals(1, fieldsRes.size());

    assertEquals(p1, res.get(0).getKey());
    assertEquals(val1, res.get(0).getValue());

    assertTrue(fieldsRes.get(0).size() >= 2);
    assertEquals(p1, fieldsRes.get(0).get(0));
    assertEquals(val1, fieldsRes.get(0).get(1));
}
 
Example 6
Source File: IgniteBinaryObjectQueryArgumentsTest.java    From ignite with Apache License 2.0 5 votes vote down vote up
/**
 * @throws Exception If failed.
 */
@Test
public void testFieldSearch() throws Exception {
    final IgniteCache<Integer, SearchValue> cache = ignite(0).cache(FIELD_CACHE);

    final Map<Integer, SearchValue> map = new HashMap<>();

    for (int i = 0; i < 10; i++) {
        map.put(i,
            new SearchValue(
                UUID.randomUUID(),
                String.valueOf(i),
                new BigDecimal(i * 0.1),
                i,
                new Date(i),
                new Timestamp(i),
                new Person(String.valueOf("name-" + i)),
                i % 2 == 0 ? EnumKey.KEY1 : EnumKey.KEY2)
        );
    }

    cache.putAll(map);

    SqlQuery<Integer, SearchValue> qry = new SqlQuery<>(SearchValue.class,
        "where uuid=? and str=? and decimal=? and integer=? and date=? and ts=? and person=? and enumKey=?");

    final int k = ThreadLocalRandom.current().nextInt(10);

    final SearchValue val = map.get(k);

    qry.setLocal(isLocal());
    qry.setArgs(val.uuid, val.str, val.decimal, val.integer, val.date, val.ts, val.person, val.enumKey);

    final List<Cache.Entry<Integer, SearchValue>> res = cache.query(qry).getAll();

    assertEquals(1, res.size());

    assertEquals(val.integer, res.get(0).getKey());
    assertEquals(val, res.get(0).getValue());
}
 
Example 7
Source File: IgniteCacheDistributedPartitionQuerySelfTest.java    From ignite with Apache License 2.0 5 votes vote down vote up
/** Tests local query over partitions. */
@Test
public void testLocalQuery() {
    Affinity<Object> affinity = grid(0).affinity("cl");

    int[] parts = affinity.primaryPartitions(grid(0).localNode());

    Arrays.sort(parts);

    IgniteCache<ClientKey, Client> cl = grid(0).cache("cl");

    SqlQuery<ClientKey, Client> qry1 = new SqlQuery<>(Client.class, "1=1");
    qry1.setLocal(true);
    qry1.setPartitions(parts[0]);

    List<Cache.Entry<ClientKey, Client>> clients = cl.query(qry1).getAll();

    for (Cache.Entry<ClientKey, Client> client : clients)
        assertEquals("Incorrect partition", parts[0], affinity.partition(client.getKey()));

    SqlFieldsQuery qry2 = new SqlFieldsQuery("select cl._KEY, cl._VAL from \"cl\".Client cl");
    qry2.setLocal(true);
    qry2.setPartitions(parts[0]);

    List<List<?>> rows = cl.query(qry2).getAll();

    for (List<?> row : rows)
        assertEquals("Incorrect partition", parts[0], affinity.partition(row.get(0)));
}
 
Example 8
Source File: CacheSqlQueryValueCopySelfTest.java    From ignite with Apache License 2.0 3 votes vote down vote up
/**
 * Test collecting info about running.
 *
 * @throws Exception If failed.
 */
@Test
public void testRunningSqlQuery() throws Exception {
    IgniteInternalFuture<?> fut = runQueryAsync(new SqlQuery<Integer, Value>(Value.class, "id > sleep(100)"));

    Thread.sleep(500);

    GridQueryProcessor qryProc = grid(0).context().query();

    Collection<GridRunningQueryInfo> queries = qryProc.runningQueries(0);

    assertEquals(1, queries.size());

    fut.get();

    queries = qryProc.runningQueries(0);

    assertEquals(0, queries.size());

    SqlQuery<Integer, Value> qry = new SqlQuery<>(Value.class, "id > sleep(100)");
    qry.setLocal(true);

    fut = runQueryAsync(qry);

    Thread.sleep(500);

    queries = qryProc.runningQueries(0);

    assertEquals(1, queries.size());

    fut.get();

    queries = qryProc.runningQueries(0);

    assertEquals(0, queries.size());
}