Search in sources :

Example 6 with SqlQuery

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

the class IgniteCacheConfigurationPrimitiveTypesSelfTest method testPrimitiveTypes.

/**
     * @throws Exception If failed.
     */
public void testPrimitiveTypes() throws Exception {
    Ignite ignite = startGrid(1);
    IgniteCache<Byte, Byte> cacheByte = jcache(ignite, new CacheConfiguration(DEFAULT_CACHE_NAME), byte.class, byte.class);
    byte b = 1;
    cacheByte.put(b, b);
    IgniteCache<Short, Short> cacheShort = jcache(ignite, new CacheConfiguration(DEFAULT_CACHE_NAME), short.class, short.class);
    short s = 2;
    cacheShort.put(s, s);
    IgniteCache<Integer, Integer> cacheInt = jcache(ignite, new CacheConfiguration(DEFAULT_CACHE_NAME), int.class, int.class);
    int i = 3;
    cacheInt.put(i, i);
    IgniteCache<Long, Long> cacheLong = jcache(ignite, new CacheConfiguration(DEFAULT_CACHE_NAME), long.class, long.class);
    long l = 4;
    cacheLong.put(l, l);
    IgniteCache<Float, Float> cacheFloat = jcache(ignite, new CacheConfiguration(DEFAULT_CACHE_NAME), float.class, float.class);
    float f = 5;
    cacheFloat.put(f, f);
    IgniteCache<Double, Double> cacheDouble = jcache(ignite, new CacheConfiguration(DEFAULT_CACHE_NAME), double.class, double.class);
    double d = 6;
    cacheDouble.put(d, d);
    IgniteCache<Boolean, Boolean> cacheBoolean = jcache(ignite, new CacheConfiguration(DEFAULT_CACHE_NAME), boolean.class, boolean.class);
    boolean bool = true;
    cacheBoolean.put(bool, bool);
    assertEquals(cacheByte.query(new SqlQuery<>(Byte.class, "1 = 1")).getAll().size(), 1);
    assertEquals(cacheShort.query(new SqlQuery<>(Short.class, "1 = 1")).getAll().size(), 1);
    assertEquals(cacheInt.query(new SqlQuery<>(Integer.class, "1 = 1")).getAll().size(), 1);
    assertEquals(cacheLong.query(new SqlQuery<>(Long.class, "1 = 1")).getAll().size(), 1);
    assertEquals(cacheFloat.query(new SqlQuery<>(Float.class, "1 = 1")).getAll().size(), 1);
    assertEquals(cacheDouble.query(new SqlQuery<>(Double.class, "1 = 1")).getAll().size(), 1);
    assertEquals(cacheBoolean.query(new SqlQuery<>(Boolean.class, "1 = 1")).getAll().size(), 1);
}
Also used : SqlQuery(org.apache.ignite.cache.query.SqlQuery) Ignite(org.apache.ignite.Ignite) CacheConfiguration(org.apache.ignite.configuration.CacheConfiguration)

Example 7 with SqlQuery

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

the class IgniteCacheAbstractQuerySelfTest method testMixedCustomTableName.

/**
     * JUnit.
     *
     * @throws Exception In case of error.
     */
public void testMixedCustomTableName() throws Exception {
    final IgniteCache<Integer, Object> cache = jcache(Integer.class, Object.class);
    cache.put(10, new Type1(1, "Type1 record #1"));
    cache.put(20, new Type1(2, "Type1 record #2"));
    cache.put(30, new Type2(1, "Type2 record #1"));
    cache.put(40, new Type2(2, "Type2 record #2"));
    cache.put(50, new Type2(3, "Type2 record #3"));
    QueryCursor<Cache.Entry<Integer, Type1>> qry1 = cache.query(new SqlQuery<Integer, Type1>(Type1.class, "FROM Type2"));
    List<Cache.Entry<Integer, Type1>> all = qry1.getAll();
    assertEquals(2, all.size());
    QueryCursor<Cache.Entry<Integer, Type2>> qry2 = cache.query(new SqlQuery<Integer, Type2>(Type2.class, "FROM Type1"));
    assertEquals(3, qry2.getAll().size());
    QueryCursor<List<?>> qry = cache.query(new SqlFieldsQuery("SELECT name FROM Type1"));
    assertEquals(3, qry.getAll().size());
    qry = cache.query(new SqlFieldsQuery("SELECT name FROM Type2"));
    assertEquals(2, qry.getAll().size());
    GridTestUtils.assertThrows(log, new GridPlainCallable<Void>() {

        @Override
        public Void call() throws Exception {
            QueryCursor<Cache.Entry<Integer, Type1>> qry1 = cache.query(new SqlQuery<Integer, Type1>(Type1.class, "FROM Type1"));
            qry1.getAll().size();
            return null;
        }
    }, CacheException.class, null);
}
Also used : SqlQuery(org.apache.ignite.cache.query.SqlQuery) SqlFieldsQuery(org.apache.ignite.cache.query.SqlFieldsQuery) IgniteCheckedException(org.apache.ignite.IgniteCheckedException) CacheException(javax.cache.CacheException) IOException(java.io.IOException) BinaryObject(org.apache.ignite.binary.BinaryObject) List(java.util.List) ArrayList(java.util.ArrayList) QueryCursor(org.apache.ignite.cache.query.QueryCursor) Cache(javax.cache.Cache) IgniteCache(org.apache.ignite.IgniteCache)

Example 8 with SqlQuery

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

the class IgniteClientReconnectCacheQueriesFailoverTest method testReconnectCacheQueries.

/**
     * @throws Exception If failed.
     */
public void testReconnectCacheQueries() throws Exception {
    final Ignite client = grid(serverCount());
    final IgniteCache<Integer, Person> cache = client.cache(DEFAULT_CACHE_NAME);
    assertNotNull(cache);
    reconnectFailover(new Callable<Void>() {

        @Override
        public Void call() throws Exception {
            SqlQuery<Integer, Person> sqlQry = new SqlQuery<>(Person.class, "where id > 1");
            try {
                assertEquals(9999, cache.query(sqlQry).getAll().size());
            } catch (CacheException e) {
                if (e.getCause() instanceof IgniteClientDisconnectedException)
                    throw e;
                else
                    log.info("Ignore error: " + e);
            }
            try {
                SqlFieldsQuery fieldsQry = new SqlFieldsQuery("select avg(p.id) from Person p");
                List<List<?>> res = cache.query(fieldsQry).getAll();
                assertEquals(1, res.size());
                Integer avg = (Integer) res.get(0).get(0);
                assertEquals(5_000, avg.intValue());
            } catch (CacheException e) {
                if (e.getCause() instanceof IgniteClientDisconnectedException)
                    throw e;
                else
                    log.info("Ignore error: " + e);
            }
            return null;
        }
    });
}
Also used : SqlQuery(org.apache.ignite.cache.query.SqlQuery) CacheException(javax.cache.CacheException) IgniteClientDisconnectedException(org.apache.ignite.IgniteClientDisconnectedException) IgniteClientDisconnectedException(org.apache.ignite.IgniteClientDisconnectedException) CacheException(javax.cache.CacheException) SqlFieldsQuery(org.apache.ignite.cache.query.SqlFieldsQuery) Ignite(org.apache.ignite.Ignite) List(java.util.List)

Example 9 with SqlQuery

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

the class IgniteClientReconnectQueriesTest method testReconnectQueryInProgress.

/**
     * @throws Exception If failed.
     */
public void testReconnectQueryInProgress() 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"));
    blockMessage(GridQueryNextPageResponse.class);
    final SqlQuery<Integer, Person> qry = new SqlQuery<>(Person.class, "_key <> 0");
    qry.setPageSize(1);
    final QueryCursor<Cache.Entry<Integer, Person>> cur1 = clnCache.query(qry);
    final IgniteInternalFuture<Object> fut = GridTestUtils.runAsync(new Callable<Object>() {

        @Override
        public Object call() throws Exception {
            try {
                cur1.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>> cur2 = clnCache.query(qry);
    assertEquals(3, cur2.getAll().size());
}
Also used : SqlQuery(org.apache.ignite.cache.query.SqlQuery) CacheException(javax.cache.CacheException) CacheException(javax.cache.CacheException) IgniteFutureTimeoutCheckedException(org.apache.ignite.internal.IgniteFutureTimeoutCheckedException) Ignite(org.apache.ignite.Ignite)

Example 10 with SqlQuery

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

the class IgniteCachePartitionedQueryMultiThreadedSelfTest method testLuceneAndSqlMultithreaded.

/**
     * JUnit.
     *
     * @throws Exception If failed.
     */
@SuppressWarnings({ "TooBroadScope" })
public void testLuceneAndSqlMultithreaded() throws Exception {
    // ---------- Test parameters ---------- //
    int luceneThreads = 10;
    int sqlThreads = 10;
    long duration = 10 * 1000;
    final int logMod = 100;
    final PersonObj p1 = new PersonObj("Jon", 1500, "Master");
    final PersonObj p2 = new PersonObj("Jane", 2000, "Master");
    final PersonObj p3 = new PersonObj("Mike", 1800, "Bachelor");
    final PersonObj p4 = new PersonObj("Bob", 1900, "Bachelor");
    final IgniteCache<UUID, PersonObj> cache0 = grid(0).cache(DEFAULT_CACHE_NAME);
    cache0.put(p1.id(), p1);
    cache0.put(p2.id(), p2);
    cache0.put(p3.id(), p3);
    cache0.put(p4.id(), p4);
    assertEquals(4, cache0.localSize(CachePeekMode.ALL));
    assert grid(0).cluster().nodes().size() == GRID_CNT;
    final AtomicBoolean done = new AtomicBoolean();
    final AtomicLong luceneCnt = new AtomicLong();
    // Start lucene query threads.
    IgniteInternalFuture<?> futLucene = GridTestUtils.runMultiThreadedAsync(new CAX() {

        @Override
        public void applyx() throws IgniteCheckedException {
            while (!done.get()) {
                QueryCursor<Cache.Entry<UUID, PersonObj>> master = cache0.query(new TextQuery(PersonObj.class, "Master"));
                Collection<Cache.Entry<UUID, PersonObj>> entries = master.getAll();
                checkResult(entries, p1, p2);
                long cnt = luceneCnt.incrementAndGet();
                if (cnt % logMod == 0)
                    info("Executed LUCENE queries: " + cnt);
            }
        }
    }, luceneThreads, "LUCENE-THREAD");
    final AtomicLong sqlCnt = new AtomicLong();
    // Start sql query threads.
    IgniteInternalFuture<?> futSql = GridTestUtils.runMultiThreadedAsync(new CAX() {

        @Override
        public void applyx() throws IgniteCheckedException {
            while (!done.get()) {
                QueryCursor<Cache.Entry<UUID, PersonObj>> bachelors = cache0.query(new SqlQuery(PersonObj.class, "degree = 'Bachelor'"));
                Collection<Cache.Entry<UUID, PersonObj>> entries = bachelors.getAll();
                checkResult(entries, p3, p4);
                long cnt = sqlCnt.incrementAndGet();
                if (cnt % logMod == 0)
                    info("Executed SQL queries: " + cnt);
            }
        }
    }, sqlThreads, "SQL-THREAD");
    Thread.sleep(duration);
    done.set(true);
    futLucene.get();
    futSql.get();
}
Also used : SqlQuery(org.apache.ignite.cache.query.SqlQuery) TextQuery(org.apache.ignite.cache.query.TextQuery) AtomicBoolean(java.util.concurrent.atomic.AtomicBoolean) AtomicLong(java.util.concurrent.atomic.AtomicLong) IgniteCheckedException(org.apache.ignite.IgniteCheckedException) Collection(java.util.Collection) CAX(org.apache.ignite.internal.util.typedef.CAX) UUID(java.util.UUID) QueryCursor(org.apache.ignite.cache.query.QueryCursor) Cache(javax.cache.Cache) IgniteCache(org.apache.ignite.IgniteCache)

Aggregations

SqlQuery (org.apache.ignite.cache.query.SqlQuery)57 Cache (javax.cache.Cache)20 IgniteCache (org.apache.ignite.IgniteCache)20 SqlFieldsQuery (org.apache.ignite.cache.query.SqlFieldsQuery)18 Ignite (org.apache.ignite.Ignite)17 List (java.util.List)13 IgniteCheckedException (org.apache.ignite.IgniteCheckedException)13 AtomicBoolean (java.util.concurrent.atomic.AtomicBoolean)10 CAX (org.apache.ignite.internal.util.typedef.CAX)9 ArrayList (java.util.ArrayList)8 AtomicInteger (java.util.concurrent.atomic.AtomicInteger)8 BinaryObject (org.apache.ignite.binary.BinaryObject)7 CacheException (javax.cache.CacheException)6 Collection (java.util.Collection)4 Random (java.util.Random)4 QueryCursor (org.apache.ignite.cache.query.QueryCursor)4 CacheConfiguration (org.apache.ignite.configuration.CacheConfiguration)4 IOException (java.io.IOException)3 HashSet (java.util.HashSet)3 UUID (java.util.UUID)3