Search in sources :

Example 1 with AffinityKey

use of org.apache.ignite.internal.binary.AffinityKey in project ignite by apache.

the class IgniteCacheGroupsSqlTest method joinQuery.

/**
 * @param grp1 First cache group.
 * @param grp2 Second cache group.
 * @param cm1 First cache mode.
 * @param cm2 Second cache mode.
 * @param cam1 First cache atomicity mode.
 * @param cam2 Second cache atomicity mode.
 * @throws Exception If failed.
 */
private void joinQuery(String grp1, String grp2, CacheMode cm1, CacheMode cm2, CacheAtomicityMode cam1, CacheAtomicityMode cam2) throws Exception {
    int keys = 1000;
    int accsPerPerson = 4;
    Ignite srv0 = ignite(0);
    IgniteCache pers = srv0.createCache(personCacheConfiguration(grp1, "pers").setAffinity(new RendezvousAffinityFunction().setPartitions(10)).setCacheMode(cm1).setAtomicityMode(cam1));
    IgniteCache acc = srv0.createCache(accountCacheConfiguration(grp2, "acc").setAffinity(new RendezvousAffinityFunction().setPartitions(10)).setCacheMode(cm2).setAtomicityMode(cam2));
    try (Transaction tx = cam1 == TRANSACTIONAL || cam2 == TRANSACTIONAL ? srv0.transactions().txStart() : null) {
        for (int i = 0; i < keys; i++) {
            int pKey = i - (i % accsPerPerson);
            if (i % accsPerPerson == 0)
                pers.put(pKey, new Person("pers-" + pKey));
            acc.put(new AffinityKey(i, pKey), new Account(pKey, "acc-" + i));
        }
        if (tx != null)
            tx.commit();
    }
    Ignite node = ignite(2);
    SqlFieldsQuery qry = new SqlFieldsQuery("select p._key as p_key, p.name, a._key as a_key, a.personId, a.attr \n" + "from \"pers\".Person p inner join \"acc\".Account a \n" + "on (p._key = a.personId)");
    IgniteCache<Object, Object> cache = node.cache("acc");
    List<List<?>> res = cache.query(qry).getAll();
    assertEquals(keys, res.size());
    for (List<?> row : res) assertEquals(row.get(0), row.get(3));
}
Also used : IgniteCache(org.apache.ignite.IgniteCache) SqlFieldsQuery(org.apache.ignite.cache.query.SqlFieldsQuery) Transaction(org.apache.ignite.transactions.Transaction) Ignite(org.apache.ignite.Ignite) RendezvousAffinityFunction(org.apache.ignite.cache.affinity.rendezvous.RendezvousAffinityFunction) List(java.util.List) AffinityKey(org.apache.ignite.internal.binary.AffinityKey)

Aggregations

List (java.util.List)1 Ignite (org.apache.ignite.Ignite)1 IgniteCache (org.apache.ignite.IgniteCache)1 RendezvousAffinityFunction (org.apache.ignite.cache.affinity.rendezvous.RendezvousAffinityFunction)1 SqlFieldsQuery (org.apache.ignite.cache.query.SqlFieldsQuery)1 AffinityKey (org.apache.ignite.internal.binary.AffinityKey)1 Transaction (org.apache.ignite.transactions.Transaction)1