Search in sources :

Example 11 with AffinityKey

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

the class CacheQueryExample method main.

/**
 * Executes example.
 *
 * @param args Command line arguments, none required.
 * @throws Exception If example execution failed.
 */
public static void main(String[] args) throws Exception {
    try (Ignite ignite = Ignition.start("examples/config/example-ignite.xml")) {
        System.out.println();
        System.out.println(">>> Cache query example started.");
        CacheConfiguration<Long, Organization> orgCacheCfg = new CacheConfiguration<>(ORG_CACHE);
        // Default.
        orgCacheCfg.setCacheMode(CacheMode.PARTITIONED);
        orgCacheCfg.setIndexedTypes(Long.class, Organization.class);
        CacheConfiguration<AffinityKey<Long>, Person> personCacheCfg = new CacheConfiguration<>(PERSON_CACHE);
        // Default.
        personCacheCfg.setCacheMode(CacheMode.PARTITIONED);
        personCacheCfg.setIndexedTypes(AffinityKey.class, Person.class);
        try {
            // Create caches.
            ignite.getOrCreateCache(orgCacheCfg);
            ignite.getOrCreateCache(personCacheCfg);
            // Populate caches.
            initialize();
            // Example for SCAN-based query based on a predicate.
            scanQuery();
            // Example for TEXT-based querying for a given string in peoples resumes.
            textQuery();
            // Example for INDEX-based query with index criteria.
            indexQuery();
        } finally {
            // Distributed cache could be removed from cluster only by Ignite.destroyCache() call.
            ignite.destroyCache(PERSON_CACHE);
            ignite.destroyCache(ORG_CACHE);
        }
        print("Cache query example finished.");
    }
}
Also used : Organization(org.apache.ignite.examples.model.Organization) Ignite(org.apache.ignite.Ignite) Person(org.apache.ignite.examples.model.Person) CacheConfiguration(org.apache.ignite.configuration.CacheConfiguration) AffinityKey(org.apache.ignite.cache.affinity.AffinityKey)

Example 12 with AffinityKey

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

the class CacheQueryExample method initialize.

/**
 * Populate cache with test data.
 */
private static void initialize() {
    IgniteCache<Long, Organization> orgCache = Ignition.ignite().cache(ORG_CACHE);
    // Clear cache before running the example.
    orgCache.clear();
    // Organizations.
    Organization org1 = new Organization("ApacheIgnite");
    Organization org2 = new Organization("Other");
    orgCache.put(org1.id(), org1);
    orgCache.put(org2.id(), org2);
    IgniteCache<AffinityKey<Long>, Person> colPersonCache = Ignition.ignite().cache(PERSON_CACHE);
    // Clear caches before running the example.
    colPersonCache.clear();
    // People.
    Person p1 = new Person(org1, "John", "Doe", 2000, "John Doe has Master Degree.");
    Person p2 = new Person(org1, "Jane", "Doe", 1000, "Jane Doe has Bachelor Degree.");
    Person p3 = new Person(org2, "John", "Smith", 1000, "John Smith has Bachelor Degree.");
    Person p4 = new Person(org2, "Jane", "Smith", 2000, "Jane Smith has Master Degree.");
    // Note that in this example we use custom affinity key for Person objects
    // to ensure that all persons are collocated with their organizations.
    colPersonCache.put(p1.key(), p1);
    colPersonCache.put(p2.key(), p2);
    colPersonCache.put(p3.key(), p3);
    colPersonCache.put(p4.key(), p4);
}
Also used : Organization(org.apache.ignite.examples.model.Organization) Person(org.apache.ignite.examples.model.Person) AffinityKey(org.apache.ignite.cache.affinity.AffinityKey)

Example 13 with AffinityKey

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

the class GridCacheAffinityMapperSelfTest method testMethodAffinityMapper.

/**
 */
@Test
public void testMethodAffinityMapper() {
    AffinityKeyMapper mapper = new GridCacheDefaultAffinityKeyMapper();
    GridTestUtils.setFieldValue(mapper, "ignite", grid());
    List<AffinityKey<Integer>> keys = new ArrayList<>();
    for (int i = 1; i <= 10; i++) keys.add(new AffinityKey<>(i, Integer.toString(i)));
    for (int i = 1; i <= 10; i++) {
        AffinityKey<Integer> key = keys.get(i - 1);
        Object mapped = mapper.affinityKey(key);
        info("Mapped key: " + mapped);
        assertNotNull(mapped);
        assertSame(key.affinityKey(), mapped);
    }
}
Also used : AffinityKeyMapper(org.apache.ignite.cache.affinity.AffinityKeyMapper) ArrayList(java.util.ArrayList) AffinityKey(org.apache.ignite.cache.affinity.AffinityKey) GridCommonAbstractTest(org.apache.ignite.testframework.junits.common.GridCommonAbstractTest) Test(org.junit.Test)

Example 14 with AffinityKey

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

the class IgniteCacheQueryNodeRestartSelfTest2 method fillCaches.

/**
 */
private void fillCaches() {
    IgniteCache<Integer, Company> co = grid(0).cache("co");
    for (int i = 0; i < COMPANY_CNT; i++) co.put(i, new Company(i));
    IgniteCache<Integer, Product> pr = grid(0).cache("pr");
    Random rnd = new GridRandom();
    for (int i = 0; i < PRODUCT_CNT; i++) pr.put(i, new Product(i, rnd.nextInt(COMPANY_CNT)));
    IgniteCache<Integer, Person> pe = grid(0).cache("pe");
    for (int i = 0; i < PERS_CNT; i++) pe.put(i, new Person(i));
    IgniteCache<AffinityKey<Integer>, Purchase> pu = grid(0).cache("pu");
    for (int i = 0; i < PURCHASE_CNT; i++) {
        int persId = rnd.nextInt(PERS_CNT);
        int prodId = rnd.nextInt(PRODUCT_CNT);
        pu.put(new AffinityKey<>(i, persId), new Purchase(persId, prodId));
    }
}
Also used : AtomicInteger(java.util.concurrent.atomic.AtomicInteger) GridRandom(org.apache.ignite.internal.util.GridRandom) Random(java.util.Random) GridRandom(org.apache.ignite.internal.util.GridRandom) AffinityKey(org.apache.ignite.cache.affinity.AffinityKey)

Example 15 with AffinityKey

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

the class IgniteCacheClientQueryReplicatedNodeRestartSelfTest method fillCaches.

/**
 */
private void fillCaches() {
    IgniteCache<Integer, Company> co = grid(0).cache("co");
    for (int i = 0; i < COMPANY_CNT; i++) co.put(i, new Company(i));
    IgniteCache<Integer, Product> pr = grid(0).cache("pr");
    Random rnd = new GridRandom();
    for (int i = 0; i < PRODUCT_CNT; i++) pr.put(i, new Product(i, rnd.nextInt(COMPANY_CNT)));
    IgniteCache<Integer, Person> pe = grid(0).cache("pe");
    for (int i = 0; i < PERS_CNT; i++) pe.put(i, new Person(i));
    IgniteCache<AffinityKey<Integer>, Purchase> pu = grid(0).cache("pu");
    for (int i = 0; i < PURCHASE_CNT; i++) {
        int persId = rnd.nextInt(PERS_CNT);
        int prodId = rnd.nextInt(PRODUCT_CNT);
        pu.put(new AffinityKey<>(i, persId), new Purchase(persId, prodId));
    }
}
Also used : AtomicInteger(java.util.concurrent.atomic.AtomicInteger) GridRandom(org.apache.ignite.internal.util.GridRandom) Random(java.util.Random) GridRandom(org.apache.ignite.internal.util.GridRandom) AffinityKey(org.apache.ignite.cache.affinity.AffinityKey)

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