Search in sources :

Example 6 with AffinityKey

use of org.apache.ignite.cache.affinity.AffinityKey in project ignite by apache.

the class IgniteCacheAbstractFieldsQuerySelfTest method testExecuteWithMetaData.

/**
 * @throws Exception If failed.
 */
@Test
public void testExecuteWithMetaData() throws Exception {
    QueryCursorImpl<List<?>> cursor = (QueryCursorImpl<List<?>>) personCache.query(sqlFieldsQuery(String.format("select p._KEY, p.name, p.age, o.name " + "from \"%s\".Person p, \"%s\".Organization o where p.orgId = o.id", personCache.getName(), orgCache.getName())));
    Collection<GridQueryFieldMetadata> meta = cursor.fieldsMeta();
    assertNotNull(meta);
    assertEquals(4, meta.size());
    Iterator<GridQueryFieldMetadata> metaIt = meta.iterator();
    assertNotNull(metaIt);
    assert metaIt.hasNext();
    GridQueryFieldMetadata field = metaIt.next();
    assertNotNull(field);
    assertEquals(personCache.getName(), field.schemaName());
    assertEquals("PERSON", field.typeName());
    assertEquals("_KEY", field.fieldName());
    assertEquals(Object.class.getName(), field.fieldTypeName());
    assert metaIt.hasNext();
    field = metaIt.next();
    assertNotNull(field);
    assertEquals(personCache.getName(), field.schemaName());
    assertEquals("PERSON", field.typeName());
    assertEquals("NAME", field.fieldName());
    assertEquals(String.class.getName(), field.fieldTypeName());
    assert metaIt.hasNext();
    field = metaIt.next();
    assertNotNull(field);
    assertEquals(personCache.getName(), field.schemaName());
    assertEquals("PERSON", field.typeName());
    assertEquals("AGE", field.fieldName());
    assertEquals(Integer.class.getName(), field.fieldTypeName());
    assert metaIt.hasNext();
    field = metaIt.next();
    assert field != null;
    assertNotNull(field);
    assertEquals(orgCache.getName(), field.schemaName());
    assertEquals("ORGANIZATION", field.typeName());
    assertEquals("NAME", field.fieldName());
    assertEquals(String.class.getName(), field.fieldTypeName());
    assert !metaIt.hasNext();
    List<List<?>> res = cursor.getAll();
    dedup(res);
    assertEquals(3, res.size());
    Collections.sort(res, new Comparator<List<?>>() {

        @Override
        public int compare(List<?> row1, List<?> row2) {
            return ((Integer) row1.get(2)).compareTo((Integer) row2.get(2));
        }
    });
    int cnt = 0;
    for (List<?> row : res) {
        assert row.size() == 4;
        if (cnt == 0) {
            assertEquals(new AffinityKey<>("p1", "o1"), row.get(0));
            assertEquals("John White", row.get(1));
            assertEquals(25, row.get(2));
            assertEquals("A", row.get(3));
        } else if (cnt == 1) {
            assertEquals(new AffinityKey<>("p2", "o1"), row.get(0));
            assertEquals("Joe Black", row.get(1));
            assertEquals(35, row.get(2));
            assertEquals("A", row.get(3));
        }
        if (cnt == 2) {
            assertEquals(new AffinityKey<>("p3", "o2"), row.get(0));
            assertEquals("Mike Green", row.get(1));
            assertEquals(40, row.get(2));
            assertEquals("B", row.get(3));
        }
        cnt++;
    }
    assertEquals(3, cnt);
}
Also used : GridQueryFieldMetadata(org.apache.ignite.internal.processors.query.GridQueryFieldMetadata) ArrayList(java.util.ArrayList) List(java.util.List) AffinityKey(org.apache.ignite.cache.affinity.AffinityKey) GridCommonAbstractTest(org.apache.ignite.testframework.junits.common.GridCommonAbstractTest) Test(org.junit.Test)

Example 7 with AffinityKey

use of org.apache.ignite.cache.affinity.AffinityKey in project ignite by apache.

the class IgniteCacheFullTextQueryNodeJoiningSelfTest method testFullTextQueryNodeJoin.

/**
 * @throws Exception If failed.
 */
@Test
public void testFullTextQueryNodeJoin() throws Exception {
    for (int r = 0; r < 5; r++) {
        startGrids(GRID_CNT);
        try {
            for (int i = 0; i < 1000; i++) {
                IndexedEntity entity = new IndexedEntity("indexed " + i);
                grid(0).cache(DEFAULT_CACHE_NAME).put(new AffinityKey<>(i, i), entity);
            }
            Ignite started = startGrid(GRID_CNT);
            for (int i = 0; i < 100; i++) {
                QueryCursor<Cache.Entry<AffinityKey<Integer>, IndexedEntity>> res = started.cache(DEFAULT_CACHE_NAME).query(new TextQuery<AffinityKey<Integer>, IndexedEntity>(IndexedEntity.class, "indexed"));
                assertEquals("Failed iteration: " + i, 1000, res.getAll().size());
            }
        } finally {
            stopAllGrids();
        }
    }
}
Also used : Ignite(org.apache.ignite.Ignite) AffinityKey(org.apache.ignite.cache.affinity.AffinityKey) GridCommonAbstractTest(org.apache.ignite.testframework.junits.common.GridCommonAbstractTest) Test(org.junit.Test)

Example 8 with AffinityKey

use of org.apache.ignite.cache.affinity.AffinityKey in project ignite by apache.

the class IgniteSqlSplitterSelfTest method testSubQueryWithAggregate.

/**
 */
@Test
public void testSubQueryWithAggregate() {
    CacheConfiguration ccfg1 = cacheConfig("pers", true, AffinityKey.class, Person2.class);
    IgniteCache<AffinityKey<Integer>, Person2> c1 = ignite(0).getOrCreateCache(ccfg1);
    try {
        int orgId = 100500;
        c1.put(new AffinityKey<>(1, orgId), new Person2(orgId, "Vasya"));
        c1.put(new AffinityKey<>(2, orgId), new Person2(orgId, "Another Vasya"));
        List<List<?>> rs = c1.query(new SqlFieldsQuery("select name, " + "select count(1) from Person2 q where q.orgId = p.orgId " + "from Person2 p order by name desc")).getAll();
        assertEquals(2, rs.size());
        assertEquals("Vasya", rs.get(0).get(0));
        assertEquals(2L, rs.get(0).get(1));
        assertEquals("Another Vasya", rs.get(1).get(0));
        assertEquals(2L, rs.get(1).get(1));
    } finally {
        c1.destroy();
    }
}
Also used : ArrayList(java.util.ArrayList) List(java.util.List) CacheConfiguration(org.apache.ignite.configuration.CacheConfiguration) AffinityKey(org.apache.ignite.cache.affinity.AffinityKey) SqlFieldsQuery(org.apache.ignite.cache.query.SqlFieldsQuery) AbstractIndexingCommonTest(org.apache.ignite.internal.processors.cache.index.AbstractIndexingCommonTest) Test(org.junit.Test)

Example 9 with AffinityKey

use of org.apache.ignite.cache.affinity.AffinityKey in project ignite by apache.

the class CacheQueryExample method sqlFieldsQueryWithJoin.

/**
     * 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);
}
Also used : List(java.util.List) Person(org.apache.ignite.examples.model.Person) AffinityKey(org.apache.ignite.cache.affinity.AffinityKey) SqlFieldsQuery(org.apache.ignite.cache.query.SqlFieldsQuery)

Example 10 with AffinityKey

use of org.apache.ignite.cache.affinity.AffinityKey in project ignite by apache.

the class SqlQueriesExample method sqlFieldsQueryWithJoin.

/**
 * 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);
}
Also used : List(java.util.List) Person(org.apache.ignite.examples.model.Person) AffinityKey(org.apache.ignite.cache.affinity.AffinityKey) SqlFieldsQuery(org.apache.ignite.cache.query.SqlFieldsQuery)

Aggregations

AffinityKey (org.apache.ignite.cache.affinity.AffinityKey)15 Person (org.apache.ignite.examples.model.Person)6 List (java.util.List)4 Ignite (org.apache.ignite.Ignite)4 CacheConfiguration (org.apache.ignite.configuration.CacheConfiguration)4 Organization (org.apache.ignite.examples.model.Organization)4 Test (org.junit.Test)4 ArrayList (java.util.ArrayList)3 SqlFieldsQuery (org.apache.ignite.cache.query.SqlFieldsQuery)3 GridCommonAbstractTest (org.apache.ignite.testframework.junits.common.GridCommonAbstractTest)3 Connection (java.sql.Connection)2 PreparedStatement (java.sql.PreparedStatement)2 Statement (java.sql.Statement)2 Random (java.util.Random)2 AtomicInteger (java.util.concurrent.atomic.AtomicInteger)2 GridRandom (org.apache.ignite.internal.util.GridRandom)2 HashMap (java.util.HashMap)1 LinkedHashMap (java.util.LinkedHashMap)1 QueryEntity (org.apache.ignite.cache.QueryEntity)1 QueryIndex (org.apache.ignite.cache.QueryIndex)1