Search in sources :

Example 1 with AffinityKey

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

the class JdbcThinMetadataSelfTest method beforeTestsStarted.

/**
 * {@inheritDoc}
 */
@Override
protected void beforeTestsStarted() throws Exception {
    super.beforeTestsStarted();
    startGridsMultiThreaded(3);
    Map<String, Integer> orgPrecision = new HashMap<>();
    orgPrecision.put("name", 42);
    IgniteCache<String, Organization> orgCache = jcache(grid(0), cacheConfiguration(new QueryEntity(String.class.getName(), Organization.class.getName()).addQueryField("id", Integer.class.getName(), null).addQueryField("name", String.class.getName(), null).setFieldsPrecision(orgPrecision).setIndexes(Arrays.asList(new QueryIndex("id"), new QueryIndex("name", false, "org_name_index")))), "org");
    assert orgCache != null;
    orgCache.put("o1", new Organization(1, "A"));
    orgCache.put("o2", new Organization(2, "B"));
    LinkedHashMap<String, Boolean> persFields = new LinkedHashMap<>();
    persFields.put("name", true);
    persFields.put("age", false);
    IgniteCache<AffinityKey, Person> personCache = jcache(grid(0), cacheConfiguration(new QueryEntityEx(new QueryEntity(AffinityKey.class.getName(), Person.class.getName()).addQueryField("name", String.class.getName(), null).addQueryField("age", Integer.class.getName(), null).addQueryField("orgId", Integer.class.getName(), null).setIndexes(Arrays.asList(new QueryIndex("orgId"), new QueryIndex().setFields(persFields)))).setNotNullFields(new HashSet<>(Arrays.asList("age", "name")))), "pers");
    assert personCache != null;
    personCache.put(new AffinityKey<>("p1", "o1"), new Person("John White", 25, 1));
    personCache.put(new AffinityKey<>("p2", "o1"), new Person("Joe Black", 35, 1));
    personCache.put(new AffinityKey<>("p3", "o2"), new Person("Mike Green", 40, 2));
    jcache(grid(0), defaultCacheConfiguration().setIndexedTypes(Integer.class, Department.class), "dep");
    try (Connection conn = DriverManager.getConnection(URL)) {
        Statement stmt = conn.createStatement();
        stmt.execute("CREATE TABLE TEST (ID INT, NAME VARCHAR(50) default 'default name', " + "age int default 21, VAL VARCHAR(50), PRIMARY KEY (ID, NAME))");
        stmt.execute("CREATE TABLE \"Quoted\" (\"Id\" INT primary key, \"Name\" VARCHAR(50)) WITH WRAP_KEY");
        stmt.execute("CREATE INDEX \"MyTestIndex quoted\" on \"Quoted\" (\"Id\" DESC)");
        stmt.execute("CREATE INDEX IDX ON TEST (ID ASC)");
        stmt.execute("CREATE TABLE TEST_DECIMAL_COLUMN (ID INT primary key, DEC_COL DECIMAL(8, 3))");
        stmt.execute("CREATE TABLE TEST_DECIMAL_COLUMN_PRECISION (ID INT primary key, DEC_COL DECIMAL(8))");
        stmt.execute("CREATE TABLE TEST_DECIMAL_DATE_COLUMN_META (ID INT primary key, DEC_COL DECIMAL(8), DATE_COL DATE)");
    }
}
Also used : HashMap(java.util.HashMap) LinkedHashMap(java.util.LinkedHashMap) QueryEntityEx(org.apache.ignite.internal.processors.query.QueryEntityEx) PreparedStatement(java.sql.PreparedStatement) Statement(java.sql.Statement) Connection(java.sql.Connection) QueryEntity(org.apache.ignite.cache.QueryEntity) LinkedHashMap(java.util.LinkedHashMap) QueryIndex(org.apache.ignite.cache.QueryIndex) AffinityKey(org.apache.ignite.cache.affinity.AffinityKey)

Example 2 with AffinityKey

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

the class JdbcMetadataSelfTest method beforeTestsStarted.

/**
 * {@inheritDoc}
 */
@Override
protected void beforeTestsStarted() throws Exception {
    startGridsMultiThreaded(3);
    IgniteCache<String, Organization> orgCache = grid(0).cache("org");
    orgCache.put("o1", new Organization(1, "A"));
    orgCache.put("o2", new Organization(2, "B"));
    IgniteCache<AffinityKey<String>, Person> personCache = grid(0).cache("pers");
    personCache.put(new AffinityKey<>("p1", "o1"), new Person("John White", 25, 1));
    personCache.put(new AffinityKey<>("p2", "o1"), new Person("Joe Black", 35, 1));
    personCache.put(new AffinityKey<>("p3", "o2"), new Person("Mike Green", 40, 2));
    jcache(grid(0), defaultCacheConfiguration().setIndexedTypes(Integer.class, Department.class), "dep");
    try (Connection conn = DriverManager.getConnection(BASE_URL)) {
        Statement stmt = conn.createStatement();
        stmt.execute("CREATE TABLE PUBLIC.TEST (ID INT, NAME VARCHAR(50) default 'default name', " + "age int default 21, VAL VARCHAR(50), PRIMARY KEY (ID, NAME))");
        stmt.execute("CREATE TABLE PUBLIC.\"Quoted\" (\"Id\" INT primary key, \"Name\" VARCHAR(50)) WITH WRAP_KEY");
        stmt.execute("CREATE INDEX \"MyTestIndex quoted\" on PUBLIC.\"Quoted\" (\"Id\" DESC)");
        stmt.execute("CREATE INDEX IDX ON PUBLIC.TEST (ID ASC)");
        stmt.execute("CREATE TABLE PUBLIC.TEST_DECIMAL_COLUMN (ID INT primary key, DEC_COL DECIMAL(8, 3))");
    }
}
Also used : PreparedStatement(java.sql.PreparedStatement) Statement(java.sql.Statement) Connection(java.sql.Connection) AffinityKey(org.apache.ignite.cache.affinity.AffinityKey)

Example 3 with AffinityKey

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

the class AffinityCollocationExample method configureAffinitKeyWithAffinityKeyClass.

// tag::affinity-key-class[]
public void configureAffinitKeyWithAffinityKeyClass() {
    CacheConfiguration<AffinityKey<Integer>, Person> personCfg = new CacheConfiguration<AffinityKey<Integer>, Person>("persons");
    personCfg.setBackups(1);
    CacheConfiguration<String, Company> companyCfg = new CacheConfiguration<String, Company>("companies");
    companyCfg.setBackups(1);
    Ignite ignite = Ignition.start();
    IgniteCache<AffinityKey<Integer>, Person> personCache = ignite.getOrCreateCache(personCfg);
    IgniteCache<String, Company> companyCache = ignite.getOrCreateCache(companyCfg);
    Company c1 = new Company("company1", "My company");
    Person p1 = new Person(1, c1.getId(), "John");
    // Both the p1 and c1 objects will be cached on the same node
    personCache.put(new AffinityKey<Integer>(p1.getId(), c1.getId()), p1);
    companyCache.put(c1.getId(), c1);
    // Get the person object
    p1 = personCache.get(new AffinityKey(1, "company1"));
}
Also used : Ignite(org.apache.ignite.Ignite) AffinityKey(org.apache.ignite.cache.affinity.AffinityKey) CacheConfiguration(org.apache.ignite.configuration.CacheConfiguration)

Example 4 with AffinityKey

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

the class SqlQueriesExample 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(">>> SQL queries 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> colPersonCacheCfg = new CacheConfiguration<>(COLLOCATED_PERSON_CACHE);
        // Default.
        colPersonCacheCfg.setCacheMode(CacheMode.PARTITIONED);
        colPersonCacheCfg.setIndexedTypes(AffinityKey.class, Person.class);
        CacheConfiguration<Long, Person> personCacheCfg = new CacheConfiguration<>(PERSON_CACHE);
        // Default.
        personCacheCfg.setCacheMode(CacheMode.PARTITIONED);
        personCacheCfg.setIndexedTypes(Long.class, Person.class);
        try {
            // Create caches.
            ignite.getOrCreateCache(orgCacheCfg);
            ignite.getOrCreateCache(colPersonCacheCfg);
            ignite.getOrCreateCache(personCacheCfg);
            // Populate caches.
            initialize();
            // Example for SQL-based querying employees based on salary ranges.
            sqlQuery();
            // Example for SQL-based querying employees for a given organization
            // (includes SQL join for collocated objects).
            sqlQueryWithJoin();
            // Example for SQL-based querying employees for a given organization
            // (includes distributed SQL join).
            sqlQueryWithDistributedJoin();
            // Example for SQL-based querying to calculate average salary
            // among all employees within a company.
            sqlQueryWithAggregation();
            // Example for SQL-based fields queries that return only required
            // fields instead of whole key-value pairs.
            sqlFieldsQuery();
            // Example for SQL-based fields queries that uses joins.
            sqlFieldsQueryWithJoin();
        } finally {
            // Distributed cache could be removed from cluster only by Ignite.destroyCache() call.
            ignite.destroyCache(COLLOCATED_PERSON_CACHE);
            ignite.destroyCache(PERSON_CACHE);
            ignite.destroyCache(ORG_CACHE);
        }
        print("SQL queries 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 5 with AffinityKey

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

the class SqlQueriesExample 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(COLLOCATED_PERSON_CACHE);
    IgniteCache<Long, Person> personCache = Ignition.ignite().cache(PERSON_CACHE);
    // Clear caches before running the example.
    colPersonCache.clear();
    personCache.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);
    // These Person objects are not collocated with their organizations.
    personCache.put(p1.id, p1);
    personCache.put(p2.id, p2);
    personCache.put(p3.id, p3);
    personCache.put(p4.id, p4);
}
Also used : Organization(org.apache.ignite.examples.model.Organization) Person(org.apache.ignite.examples.model.Person) 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