Search in sources :

Example 6 with Query

use of org.apache.ignite.cache.query.Query in project ignite by apache.

the class IgniteCrossCachesJoinsQueryTest method checkPersonAccountsJoin.

/**
 * @param cache Cache.
 * @param cnts Accounts per person counts.
 */
private void checkPersonAccountsJoin(IgniteCache cache, Map<Integer, Integer> cnts) {
    if (skipQuery(cache, PERSON_CACHE_NAME, ACC_CACHE_NAME))
        return;
    qry = "checkPersonAccountsJoin";
    List<Query> qrys = new ArrayList<>();
    qrys.add(new SqlFieldsQuery("select p.name from " + "\"" + PERSON_CACHE_NAME + "\".Person p, " + "\"" + ACC_CACHE_NAME + "\".Account a " + "where p.id = a.personId and p.id=?"));
    qrys.add(new SqlFieldsQuery("select p.name from " + "\"" + PERSON_CACHE_NAME + "\".Person p, " + "\"" + ACC_CACHE_NAME + "\".Account a " + "where p.dateId = a.personDateId and p.id=?"));
    qrys.add(new SqlFieldsQuery("select p.name from " + "\"" + PERSON_CACHE_NAME + "\".Person p, " + "\"" + ACC_CACHE_NAME + "\".Account a " + "where p.strId = a.personStrId and p.id=?"));
    qrys.add(new SqlFieldsQuery("select p.name from " + "\"" + PERSON_CACHE_NAME + "\".Person p, " + "\"" + ACC_CACHE_NAME + "\".Account a " + "where p.id = a.personId and p.id=?"));
    qrys.add(new SqlFieldsQuery("select p.name from " + "\"" + PERSON_CACHE_NAME + "\".Person p, " + "\"" + ACC_CACHE_NAME + "\".Account a " + "where p.dateId = a.personDateId and p.id=?"));
    qrys.add(new SqlFieldsQuery("select p.name from " + "\"" + PERSON_CACHE_NAME + "\".Person p, " + "\"" + ACC_CACHE_NAME + "\".Account a " + "where p.strId = a.personStrId and p.id=?"));
    if (PERSON_CACHE_NAME.equals(cache.getName())) {
        qrys.add(new SqlQuery(Person.class, "from \"" + PERSON_CACHE_NAME + "\".Person , \"" + ACC_CACHE_NAME + "\".Account  " + "where Person.id = Account.personId and Person.id=?"));
        qrys.add(new SqlQuery(Person.class, "from \"" + PERSON_CACHE_NAME + "\".Person , \"" + ACC_CACHE_NAME + "\".Account  " + "where Person.id = Account.personId and Person.id=?"));
    }
    List<Integer> keys = new ArrayList<>(cnts.keySet());
    for (int i = 0; i < 10; i++) {
        Integer key = keys.get(rnd.nextInt(keys.size()));
        List<List<Object>> res;
        for (Query q : qrys) {
            if (q instanceof SqlFieldsQuery) {
                ((SqlFieldsQuery) q).setDistributedJoins(distributedJoins());
                ((SqlFieldsQuery) q).setArgs(key);
            } else {
                ((SqlQuery) q).setDistributedJoins(distributedJoins());
                ((SqlQuery) q).setArgs(key);
            }
            res = cache.query(q).getAll();
            assertEquals((int) cnts.get(key), res.size());
        }
    }
    qrys.clear();
    qrys.add(new SqlFieldsQuery("select count(*) " + "from \"" + PERSON_CACHE_NAME + "\".Person p, \"" + ACC_CACHE_NAME + "\".Account" + " a " + "where p.id = a.personId"));
    qrys.add(new SqlFieldsQuery("select count(*) " + "from \"" + PERSON_CACHE_NAME + "\".Person p, \"" + ACC_CACHE_NAME + "\".Account" + " a " + "where p.Dateid = a.personDateId"));
    qrys.add(new SqlFieldsQuery("select count(*) " + "from \"" + PERSON_CACHE_NAME + "\".Person p, \"" + ACC_CACHE_NAME + "\".Account" + " a " + "where p.strId = a.personStrId"));
    qrys.add(new SqlFieldsQuery("select count(*) " + "from \"" + PERSON_CACHE_NAME + "\".Person p, \"" + ACC_CACHE_NAME + "\".Account" + " a " + "where p.id = a.personId"));
    long total = 0;
    for (Integer cnt : data.accountsPerPerson.values()) total += cnt;
    for (Query q : qrys) {
        ((SqlFieldsQuery) q).setDistributedJoins(distributedJoins());
        List<List<Object>> res = cache.query(q).getAll();
        assertEquals(1, res.size());
        assertEquals(total, res.get(0).get(0));
    }
}
Also used : SqlQuery(org.apache.ignite.cache.query.SqlQuery) SqlFieldsQuery(org.apache.ignite.cache.query.SqlFieldsQuery) SqlQuery(org.apache.ignite.cache.query.SqlQuery) Query(org.apache.ignite.cache.query.Query) ArrayList(java.util.ArrayList) ArrayList(java.util.ArrayList) List(java.util.List) SqlFieldsQuery(org.apache.ignite.cache.query.SqlFieldsQuery)

Example 7 with Query

use of org.apache.ignite.cache.query.Query in project ignite by apache.

the class IgniteRepositoryQuery method prepareQuery.

/**
 * @param prmtrs Prmtrs.
 * @return prepared query for execution
 */
@NotNull
private Query prepareQuery(Object[] prmtrs) {
    Object[] parameters = prmtrs;
    String sql = qry.sql();
    Query query;
    switch(qry.options()) {
        case SORTING:
            sql = IgniteQueryGenerator.addSorting(new StringBuilder(sql), (Sort) parameters[parameters.length - 1]).toString();
            parameters = Arrays.copyOfRange(parameters, 0, parameters.length - 1);
            break;
        case PAGINATION:
            sql = IgniteQueryGenerator.addPaging(new StringBuilder(sql), (Pageable) parameters[parameters.length - 1]).toString();
            parameters = Arrays.copyOfRange(parameters, 0, parameters.length - 1);
            break;
    }
    if (qry.isFieldQuery()) {
        SqlFieldsQuery sqlFieldsQry = new SqlFieldsQuery(sql);
        sqlFieldsQry.setArgs(parameters);
        query = sqlFieldsQry;
    } else {
        SqlQuery sqlQry = new SqlQuery(type, sql);
        sqlQry.setArgs(parameters);
        query = sqlQry;
    }
    return query;
}
Also used : SqlQuery(org.apache.ignite.cache.query.SqlQuery) Query(org.apache.ignite.cache.query.Query) SqlFieldsQuery(org.apache.ignite.cache.query.SqlFieldsQuery) SqlQuery(org.apache.ignite.cache.query.SqlQuery) RepositoryQuery(org.springframework.data.repository.query.RepositoryQuery) SqlFieldsQuery(org.apache.ignite.cache.query.SqlFieldsQuery) NotNull(org.jetbrains.annotations.NotNull)

Example 8 with Query

use of org.apache.ignite.cache.query.Query in project ignite by apache.

the class LoadTest method testMultithreading.

/**
 * Test thin client in multi-thread environment.
 */
@Test
public void testMultithreading() throws Exception {
    final int THREAD_CNT = 8;
    final int ITERATION_CNT = 20;
    final int BATCH_SIZE = 1000;
    final int PAGE_CNT = 3;
    IgniteConfiguration srvCfg = Config.getServerConfiguration();
    // No peer class loading from thin clients: we need the server to know about this class to deserialize
    // ScanQuery filter.
    srvCfg.setBinaryConfiguration(new BinaryConfiguration().setTypeConfigurations(Arrays.asList(new BinaryTypeConfiguration(getClass().getName()), new BinaryTypeConfiguration(SerializedLambda.class.getName()))));
    try (Ignite ignored = Ignition.start(srvCfg);
        IgniteClient client = Ignition.startClient(new ClientConfiguration().setAddresses(Config.SERVER))) {
        ClientCache<Integer, String> cache = client.createCache("testMultithreading");
        AtomicInteger cnt = new AtomicInteger(1);
        AtomicReference<Throwable> error = new AtomicReference<>();
        Runnable assertion = () -> {
            try {
                int rangeStart = cnt.getAndAdd(BATCH_SIZE);
                int rangeEnd = rangeStart + BATCH_SIZE;
                Map<Integer, String> data = IntStream.range(rangeStart, rangeEnd).boxed().collect(Collectors.toMap(i -> i, i -> String.format("String %s", i)));
                cache.putAll(data);
                Query<Cache.Entry<Integer, String>> qry = new ScanQuery<Integer, String>().setPageSize(data.size() / PAGE_CNT).setFilter((i, s) -> i >= rangeStart && i < rangeEnd);
                try (QueryCursor<Cache.Entry<Integer, String>> cur = cache.query(qry)) {
                    List<Cache.Entry<Integer, String>> res = cur.getAll();
                    assertEquals("Unexpected number of entries", data.size(), res.size());
                    Map<Integer, String> act = res.stream().collect(Collectors.toMap(Cache.Entry::getKey, Cache.Entry::getValue));
                    assertEquals("Unexpected entries", data, act);
                }
            } catch (Throwable ex) {
                error.set(ex);
            }
        };
        CountDownLatch complete = new CountDownLatch(THREAD_CNT);
        Runnable manyAssertions = () -> {
            for (int i = 0; i < ITERATION_CNT && error.get() == null; i++) assertion.run();
            complete.countDown();
        };
        ExecutorService threadPool = Executors.newFixedThreadPool(THREAD_CNT);
        IntStream.range(0, THREAD_CNT).forEach(t -> threadPool.submit(manyAssertions));
        assertTrue("Timeout", complete.await(180, TimeUnit.SECONDS));
        String errMsg = error.get() == null ? "" : error.get().getMessage();
        assertNull(errMsg, error.get());
    }
}
Also used : IntStream(java.util.stream.IntStream) Arrays(java.util.Arrays) AtomicReference(java.util.concurrent.atomic.AtomicReference) BinaryConfiguration(org.apache.ignite.configuration.BinaryConfiguration) AtomicInteger(java.util.concurrent.atomic.AtomicInteger) SerializedLambda(java.lang.invoke.SerializedLambda) Map(java.util.Map) Cache(javax.cache.Cache) Timeout(org.junit.rules.Timeout) ExecutorService(java.util.concurrent.ExecutorService) Query(org.apache.ignite.cache.query.Query) BinaryTypeConfiguration(org.apache.ignite.binary.BinaryTypeConfiguration) Assert.assertTrue(org.junit.Assert.assertTrue) Test(org.junit.Test) Ignite(org.apache.ignite.Ignite) Collectors(java.util.stream.Collectors) Executors(java.util.concurrent.Executors) GridTestUtils(org.apache.ignite.testframework.GridTestUtils) TimeUnit(java.util.concurrent.TimeUnit) CountDownLatch(java.util.concurrent.CountDownLatch) List(java.util.List) IgniteConfiguration(org.apache.ignite.configuration.IgniteConfiguration) Ignition(org.apache.ignite.Ignition) Rule(org.junit.Rule) Assert.assertNull(org.junit.Assert.assertNull) ClientConfiguration(org.apache.ignite.configuration.ClientConfiguration) QueryCursor(org.apache.ignite.cache.query.QueryCursor) ScanQuery(org.apache.ignite.cache.query.ScanQuery) Assert.assertEquals(org.junit.Assert.assertEquals) Query(org.apache.ignite.cache.query.Query) ScanQuery(org.apache.ignite.cache.query.ScanQuery) Ignite(org.apache.ignite.Ignite) List(java.util.List) QueryCursor(org.apache.ignite.cache.query.QueryCursor) AtomicReference(java.util.concurrent.atomic.AtomicReference) CountDownLatch(java.util.concurrent.CountDownLatch) AtomicInteger(java.util.concurrent.atomic.AtomicInteger) IgniteConfiguration(org.apache.ignite.configuration.IgniteConfiguration) BinaryConfiguration(org.apache.ignite.configuration.BinaryConfiguration) AtomicInteger(java.util.concurrent.atomic.AtomicInteger) ExecutorService(java.util.concurrent.ExecutorService) BinaryTypeConfiguration(org.apache.ignite.binary.BinaryTypeConfiguration) Map(java.util.Map) ClientConfiguration(org.apache.ignite.configuration.ClientConfiguration) Cache(javax.cache.Cache) Test(org.junit.Test)

Example 9 with Query

use of org.apache.ignite.cache.query.Query in project ignite by apache.

the class FunctionalQueryTest method testGettingEmptyResultWhenQueryingEmptyTable.

/**
 */
@Test
public void testGettingEmptyResultWhenQueryingEmptyTable() throws Exception {
    try (Ignite ignored = Ignition.start(Config.getServerConfiguration());
        IgniteClient client = Ignition.startClient(new ClientConfiguration().setAddresses(Config.SERVER))) {
        final String TBL = "Person";
        client.query(new SqlFieldsQuery(String.format("CREATE TABLE IF NOT EXISTS " + TBL + " (id INT PRIMARY KEY, name VARCHAR) WITH \"VALUE_TYPE=%s\"", Person.class.getName())).setSchema("PUBLIC")).getAll();
        // IgniteClient#query() API
        List<List<?>> res = client.query(new SqlFieldsQuery("SELECT * FROM " + TBL)).getAll();
        assertNotNull(res);
        assertEquals(0, res.size());
        // ClientCache#query(SqlFieldsQuery) API
        ClientCache<Integer, Person> cache = client.cache("SQL_PUBLIC_" + TBL.toUpperCase());
        res = cache.query(new SqlFieldsQuery("SELECT * FROM " + TBL)).getAll();
        assertNotNull(res);
        assertEquals(0, res.size());
        // ClientCache#query(ScanQuery) and ClientCache#query(SqlQuery) API
        Collection<Query<Cache.Entry<Integer, Person>>> queries = Arrays.asList(new ScanQuery<>(), new SqlQuery<>(Person.class, "1 = 1"));
        for (Query<Cache.Entry<Integer, Person>> qry : queries) {
            try (QueryCursor<Cache.Entry<Integer, Person>> cur = cache.query(qry)) {
                List<Cache.Entry<Integer, Person>> res2 = cur.getAll();
                assertNotNull(res2);
                assertEquals(0, res2.size());
            }
        }
    }
}
Also used : SqlFieldsQuery(org.apache.ignite.cache.query.SqlFieldsQuery) SqlQuery(org.apache.ignite.cache.query.SqlQuery) Query(org.apache.ignite.cache.query.Query) ScanQuery(org.apache.ignite.cache.query.ScanQuery) SqlFieldsQuery(org.apache.ignite.cache.query.SqlFieldsQuery) Ignite(org.apache.ignite.Ignite) List(java.util.List) ClientConfiguration(org.apache.ignite.configuration.ClientConfiguration) Cache(javax.cache.Cache) Test(org.junit.Test)

Example 10 with Query

use of org.apache.ignite.cache.query.Query in project ignite by apache.

the class FunctionalQueryTest method testQueries.

/**
 * Tested API:
 * <ul>
 * <li>{@link ClientCache#query(Query)}</li>
 * </ul>
 */
@Test
public void testQueries() throws Exception {
    IgniteConfiguration srvCfg = Config.getServerConfiguration();
    // No peer class loading from thin clients: we need the server to know about this class to deserialize
    // ScanQuery filter.
    srvCfg.setBinaryConfiguration(new BinaryConfiguration().setTypeConfigurations(Arrays.asList(new BinaryTypeConfiguration(getClass().getName()), new BinaryTypeConfiguration(SerializedLambda.class.getName()))));
    try (Ignite ignored = Ignition.start(srvCfg);
        IgniteClient client = Ignition.startClient(getClientConfiguration())) {
        ClientCache<Integer, Person> cache = client.getOrCreateCache(Config.DEFAULT_CACHE_NAME);
        Map<Integer, Person> data = IntStream.rangeClosed(1, 100).boxed().collect(Collectors.toMap(i -> i, i -> new Person(i, String.format("Person %s", i))));
        cache.putAll(data);
        int minId = data.size() / 2 + 1;
        int pageSize = (data.size() - minId) / 3;
        // expected query result size
        int expSize = data.size() - minId + 1;
        // Expected result
        Map<Integer, Person> exp = data.entrySet().stream().filter(e -> e.getKey() >= minId).collect(Collectors.toMap(Map.Entry::getKey, Map.Entry::getValue));
        // Scan and SQL queries
        Collection<Query<Cache.Entry<Integer, Person>>> queries = Arrays.asList(new ScanQuery<Integer, Person>((i, p) -> p.getId() >= minId).setPageSize(pageSize), new SqlQuery<Integer, Person>(Person.class, "id >= ?").setArgs(minId).setPageSize(pageSize));
        for (Query<Cache.Entry<Integer, Person>> qry : queries) {
            try (QueryCursor<Cache.Entry<Integer, Person>> cur = cache.query(qry)) {
                List<Cache.Entry<Integer, Person>> res = cur.getAll();
                assertEquals(String.format("Unexpected number of rows from %s", qry.getClass().getSimpleName()), expSize, res.size());
                Map<Integer, Person> act = res.stream().collect(Collectors.toMap(Cache.Entry::getKey, Cache.Entry::getValue));
                assertEquals(String.format("unexpected rows from %s", qry.getClass().getSimpleName()), exp, act);
            }
        }
        checkSqlFieldsQuery(cache, minId, pageSize, expSize, exp, true);
        checkSqlFieldsQuery(cache, minId, pageSize, expSize, exp, false);
    }
}
Also used : IntStream(java.util.stream.IntStream) Arrays(java.util.Arrays) SqlFieldsQuery(org.apache.ignite.cache.query.SqlFieldsQuery) BinaryConfiguration(org.apache.ignite.configuration.BinaryConfiguration) SerializedLambda(java.lang.invoke.SerializedLambda) Map(java.util.Map) SqlQuery(org.apache.ignite.cache.query.SqlQuery) Cache(javax.cache.Cache) Timeout(org.junit.rules.Timeout) Query(org.apache.ignite.cache.query.Query) BinaryTypeConfiguration(org.apache.ignite.binary.BinaryTypeConfiguration) Assert.assertNotNull(org.junit.Assert.assertNotNull) Collection(java.util.Collection) Test(org.junit.Test) Ignite(org.apache.ignite.Ignite) Field(java.lang.reflect.Field) Collectors(java.util.stream.Collectors) GridTestUtils(org.apache.ignite.testframework.GridTestUtils) List(java.util.List) IgniteConfiguration(org.apache.ignite.configuration.IgniteConfiguration) Ignition(org.apache.ignite.Ignition) Rule(org.junit.Rule) ClientConfiguration(org.apache.ignite.configuration.ClientConfiguration) QueryCursor(org.apache.ignite.cache.query.QueryCursor) ScanQuery(org.apache.ignite.cache.query.ScanQuery) Assert.assertEquals(org.junit.Assert.assertEquals) SqlFieldsQuery(org.apache.ignite.cache.query.SqlFieldsQuery) SqlQuery(org.apache.ignite.cache.query.SqlQuery) Query(org.apache.ignite.cache.query.Query) ScanQuery(org.apache.ignite.cache.query.ScanQuery) ScanQuery(org.apache.ignite.cache.query.ScanQuery) IgniteConfiguration(org.apache.ignite.configuration.IgniteConfiguration) BinaryConfiguration(org.apache.ignite.configuration.BinaryConfiguration) BinaryTypeConfiguration(org.apache.ignite.binary.BinaryTypeConfiguration) Ignite(org.apache.ignite.Ignite) Map(java.util.Map) Cache(javax.cache.Cache) Test(org.junit.Test)

Aggregations

Query (org.apache.ignite.cache.query.Query)11 List (java.util.List)7 SqlFieldsQuery (org.apache.ignite.cache.query.SqlFieldsQuery)7 ScanQuery (org.apache.ignite.cache.query.ScanQuery)6 SqlQuery (org.apache.ignite.cache.query.SqlQuery)6 QueryCursor (org.apache.ignite.cache.query.QueryCursor)5 Test (org.junit.Test)5 Map (java.util.Map)4 Ignition (org.apache.ignite.Ignition)4 ArrayList (java.util.ArrayList)3 Arrays (java.util.Arrays)3 Collection (java.util.Collection)3 TimeUnit (java.util.concurrent.TimeUnit)3 Cache (javax.cache.Cache)3 Ignite (org.apache.ignite.Ignite)3 ClientConfiguration (org.apache.ignite.configuration.ClientConfiguration)3 U (org.apache.ignite.internal.util.typedef.internal.U)3 SerializedLambda (java.lang.invoke.SerializedLambda)2 Collections (java.util.Collections)2 Iterator (java.util.Iterator)2