Search in sources :

Example 36 with SqlQuery

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

the class IgniteCacheProxyImpl method convertToBinary.

/**
 * Convert query arguments to BinaryObjects if binary marshaller used.
 *
 * @param qry Query.
 */
private void convertToBinary(final Query qry) {
    if (ctx.binaryMarshaller()) {
        if (qry instanceof SqlQuery) {
            final SqlQuery sqlQry = (SqlQuery) qry;
            convertToBinary(sqlQry.getArgs());
        } else if (qry instanceof SpiQuery) {
            final SpiQuery spiQry = (SpiQuery) qry;
            convertToBinary(spiQry.getArgs());
        } else if (qry instanceof SqlFieldsQuery) {
            final SqlFieldsQuery fieldsQry = (SqlFieldsQuery) qry;
            convertToBinary(fieldsQry.getArgs());
        }
    }
}
Also used : SqlQuery(org.apache.ignite.cache.query.SqlQuery) SpiQuery(org.apache.ignite.cache.query.SpiQuery) SqlFieldsQuery(org.apache.ignite.cache.query.SqlFieldsQuery)

Example 37 with SqlQuery

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

the class IgniteCacheProxyImpl method query.

/**
 * {@inheritDoc}
 */
@SuppressWarnings("unchecked")
@Override
public <R> QueryCursor<R> query(Query<R> qry) {
    A.notNull(qry, "qry");
    try {
        ctx.checkSecurity(SecurityPermission.CACHE_READ);
        validate(qry);
        convertToBinary(qry);
        CacheOperationContext opCtxCall = ctx.operationContextPerCall();
        boolean keepBinary = opCtxCall != null && opCtxCall.isKeepBinary();
        if (qry instanceof ContinuousQuery || qry instanceof ContinuousQueryWithTransformer)
            return (QueryCursor<R>) queryContinuous((AbstractContinuousQuery) qry, qry.isLocal(), keepBinary);
        if (qry instanceof SqlQuery)
            return (QueryCursor<R>) ctx.kernalContext().query().querySql(ctx, (SqlQuery) qry, keepBinary);
        if (qry instanceof SqlFieldsQuery)
            return (FieldsQueryCursor<R>) ctx.kernalContext().query().querySqlFields(ctx, (SqlFieldsQuery) qry, null, keepBinary, true).get(0);
        if (qry instanceof ScanQuery)
            return query((ScanQuery) qry, null, projection(qry.isLocal()));
        return (QueryCursor<R>) query(qry, projection(qry.isLocal()));
    } catch (Exception e) {
        if (e instanceof CacheException)
            throw (CacheException) e;
        throw new CacheException(e.getMessage(), e);
    }
}
Also used : ContinuousQueryWithTransformer(org.apache.ignite.cache.query.ContinuousQueryWithTransformer) AbstractContinuousQuery(org.apache.ignite.cache.query.AbstractContinuousQuery) ContinuousQuery(org.apache.ignite.cache.query.ContinuousQuery) SqlQuery(org.apache.ignite.cache.query.SqlQuery) CacheException(javax.cache.CacheException) ScanQuery(org.apache.ignite.cache.query.ScanQuery) SqlFieldsQuery(org.apache.ignite.cache.query.SqlFieldsQuery) QueryCursor(org.apache.ignite.cache.query.QueryCursor) FieldsQueryCursor(org.apache.ignite.cache.query.FieldsQueryCursor) IgniteCheckedException(org.apache.ignite.IgniteCheckedException) IgniteException(org.apache.ignite.IgniteException) EntryProcessorException(javax.cache.processor.EntryProcessorException) CacheException(javax.cache.CacheException) NoSuchElementException(java.util.NoSuchElementException) IgniteCacheRestartingException(org.apache.ignite.IgniteCacheRestartingException) IOException(java.io.IOException) GridClosureException(org.apache.ignite.internal.util.lang.GridClosureException)

Example 38 with SqlQuery

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

the class BinarySerializationQuerySelfTest method check.

/**
 * Internal check routine.
 *
 * @param cls Entity class.
 * @throws Exception If failed.
 */
@SuppressWarnings("unchecked")
private void check(Class cls) throws Exception {
    cache.put(1, createInstance(cls, 10));
    cache.put(2, createInstance(cls, 20));
    cache.put(3, createInstance(cls, 30));
    Iterator iter = cache.query(new SqlQuery(cls, "val=20")).iterator();
    assert iter.hasNext();
    Cache.Entry res = (Cache.Entry) iter.next();
    assertEquals(2, res.getKey());
    assertEquals(Integer.valueOf(20), U.field(res.getValue(), "val"));
    assert !iter.hasNext();
    iter = cache.query(new SqlFieldsQuery("SELECT p.val FROM " + cls.getSimpleName() + " p WHERE p.val=20")).iterator();
    assert iter.hasNext();
    List<Object> fieldsRes = (List<Object>) iter.next();
    assertEquals(20, fieldsRes.get(0));
    assert !iter.hasNext();
}
Also used : SqlQuery(org.apache.ignite.cache.query.SqlQuery) Iterator(java.util.Iterator) ArrayList(java.util.ArrayList) List(java.util.List) SqlFieldsQuery(org.apache.ignite.cache.query.SqlFieldsQuery) Cache(javax.cache.Cache) IgniteCache(org.apache.ignite.IgniteCache)

Example 39 with SqlQuery

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

the class CacheAbstractQueryMetricsSelfTest method testSqlQueryNotFullyFetchedMetrics.

/**
 * Test metrics for Sql queries.
 *
 * @throws Exception In case of error.
 */
public void testSqlQueryNotFullyFetchedMetrics() throws Exception {
    IgniteCache<Integer, String> cache = grid(0).context().cache().jcache("A");
    SqlQuery qry = new SqlQuery<>("String", "from String");
    qry.setPageSize(10);
    checkQueryNotFullyFetchedMetrics(cache, qry, true);
}
Also used : SqlQuery(org.apache.ignite.cache.query.SqlQuery)

Example 40 with SqlQuery

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

the class CacheQueryExample method sqlQueryWithDistributedJoin.

/**
     * Example for SQL queries based on all employees working
     * for a specific organization (query uses distributed join).
     */
private static void sqlQueryWithDistributedJoin() {
    IgniteCache<Long, Person> cache = Ignition.ignite().cache(PERSON_CACHE);
    // SQL clause query which joins on 2 types to select people for a specific organization.
    String joinSql = "from Person, \"" + ORG_CACHE + "\".Organization as org " + "where Person.orgId = org.id " + "and lower(org.name) = lower(?)";
    SqlQuery qry = new SqlQuery<Long, Person>(Person.class, joinSql).setArgs("ApacheIgnite");
    // Enable distributed joins for query.
    qry.setDistributedJoins(true);
    // Execute queries for find employees for different organizations.
    print("Following people are 'ApacheIgnite' employees (distributed join): ", cache.query(qry).getAll());
    qry.setArgs("Other");
    print("Following people are 'Other' employees (distributed join): ", cache.query(qry).getAll());
}
Also used : SqlQuery(org.apache.ignite.cache.query.SqlQuery) Person(org.apache.ignite.examples.model.Person)

Aggregations

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