Search in sources :

Example 1 with Organization

use of org.apache.ignite.examples.model.Organization in project ignite by apache.

the class CacheQueryDmlExample method main.

/**
     * Executes example.
     *
     * @param args Command line arguments, none required.
     * @throws Exception If example execution failed.
     */
@SuppressWarnings({ "unused", "ThrowFromFinallyBlock" })
public static void main(String[] args) throws Exception {
    try (Ignite ignite = Ignition.start("examples/config/example-ignite.xml")) {
        print("Cache query DML example started.");
        CacheConfiguration<Long, Organization> orgCacheCfg = new CacheConfiguration<>(ORG_CACHE);
        orgCacheCfg.setIndexedTypes(Long.class, Organization.class);
        CacheConfiguration<Long, Person> personCacheCfg = new CacheConfiguration<>(PERSON_CACHE);
        personCacheCfg.setIndexedTypes(Long.class, Person.class);
        // Auto-close cache at the end of the example.
        try (IgniteCache<Long, Organization> orgCache = ignite.getOrCreateCache(orgCacheCfg);
            IgniteCache<Long, Person> personCache = ignite.getOrCreateCache(personCacheCfg)) {
            insert(orgCache, personCache);
            select(personCache, "Insert data");
            update(personCache);
            select(personCache, "Update salary for Master degrees");
            delete(personCache);
            select(personCache, "Delete non-Apache employees");
        } finally {
            // Distributed cache could be removed from cluster only by #destroyCache() call.
            ignite.destroyCache(PERSON_CACHE);
            ignite.destroyCache(ORG_CACHE);
        }
        print("Cache query DML 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)

Example 2 with Organization

use of org.apache.ignite.examples.model.Organization in project ignite by apache.

the class CacheClientBinaryPutGetExample method putGetBinary.

/**
 * Execute individual put and get, getting value in binary format, without de-serializing it.
 *
 * @param cache Cache.
 */
private static void putGetBinary(IgniteCache<Integer, Organization> cache) {
    // Create new Organization binary object to store in cache.
    Organization org = new Organization(// Name.
    "Microsoft", // Address.
    new Address("1096 Eddy Street, San Francisco, CA", 94109), // Type.
    OrganizationType.PRIVATE, // Last update time.
    new Timestamp(System.currentTimeMillis()));
    // Put created data entry to cache.
    cache.put(1, org);
    // Get cache that will get values as binary objects.
    IgniteCache<Integer, BinaryObject> binaryCache = cache.withKeepBinary();
    // Get recently created organization as a binary object.
    BinaryObject po = binaryCache.get(1);
    // Get organization's name from binary object (note that
    // object doesn't need to be fully deserialized).
    String name = po.field("name");
    System.out.println();
    System.out.println(">>> Retrieved organization name from binary object: " + name);
}
Also used : Organization(org.apache.ignite.examples.model.Organization) Address(org.apache.ignite.examples.model.Address) BinaryObject(org.apache.ignite.binary.BinaryObject) Timestamp(java.sql.Timestamp)

Example 3 with Organization

use of org.apache.ignite.examples.model.Organization in project ignite by apache.

the class PersistentStoreExample method main.

/**
 * @param args Program arguments, ignored.
 * @throws Exception If failed.
 */
public static void main(String[] args) throws Exception {
    Ignition.setClientMode(true);
    try (Ignite ignite = Ignition.start("examples/config/persistentstore/example-persistent-store.xml")) {
        // Activate the cluster. Required to do if the persistent store is enabled because you might need
        // to wait while all the nodes, that store a subset of data on disk, join the cluster.
        ignite.active(true);
        CacheConfiguration<Long, Organization> cacheCfg = new CacheConfiguration<>(ORG_CACHE);
        cacheCfg.setAtomicityMode(CacheAtomicityMode.TRANSACTIONAL);
        cacheCfg.setBackups(1);
        cacheCfg.setWriteSynchronizationMode(CacheWriteSynchronizationMode.FULL_SYNC);
        cacheCfg.setIndexedTypes(Long.class, Organization.class);
        IgniteCache<Long, Organization> cache = ignite.getOrCreateCache(cacheCfg);
        if (UPDATE) {
            System.out.println("Populating the cache...");
            try (IgniteDataStreamer<Long, Organization> streamer = ignite.dataStreamer(ORG_CACHE)) {
                streamer.allowOverwrite(true);
                for (long i = 0; i < 100_000; i++) {
                    streamer.addData(i, new Organization(i, "organization-" + i));
                    if (i > 0 && i % 10_000 == 0)
                        System.out.println("Done: " + i);
                }
            }
        }
        // Run SQL without explicitly calling to loadCache().
        QueryCursor<List<?>> cur = cache.query(new SqlFieldsQuery("select id, name from Organization where name like ?").setArgs("organization-54321"));
        System.out.println("SQL Result: " + cur.getAll());
        // Run get() without explicitly calling to loadCache().
        Organization org = cache.get(54321l);
        System.out.println("GET Result: " + org);
    }
}
Also used : Organization(org.apache.ignite.examples.model.Organization) Ignite(org.apache.ignite.Ignite) List(java.util.List) CacheConfiguration(org.apache.ignite.configuration.CacheConfiguration) SqlFieldsQuery(org.apache.ignite.cache.query.SqlFieldsQuery)

Example 4 with Organization

use of org.apache.ignite.examples.model.Organization 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 Organization

use of org.apache.ignite.examples.model.Organization 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

Organization (org.apache.ignite.examples.model.Organization)17 Ignite (org.apache.ignite.Ignite)8 Timestamp (java.sql.Timestamp)7 CacheConfiguration (org.apache.ignite.configuration.CacheConfiguration)7 Address (org.apache.ignite.examples.model.Address)7 Person (org.apache.ignite.examples.model.Person)6 HashMap (java.util.HashMap)4 BinaryObject (org.apache.ignite.binary.BinaryObject)4 AffinityKey (org.apache.ignite.cache.affinity.AffinityKey)4 Employee (org.apache.ignite.examples.model.Employee)2 EmployeeKey (org.apache.ignite.examples.model.EmployeeKey)2 ArrayList (java.util.ArrayList)1 LinkedHashMap (java.util.LinkedHashMap)1 List (java.util.List)1 Map (java.util.Map)1 Cache (javax.cache.Cache)1 IgniteCache (org.apache.ignite.IgniteCache)1 Ignition (org.apache.ignite.Ignition)1 CacheKeyConfiguration (org.apache.ignite.cache.CacheKeyConfiguration)1 QueryEntity (org.apache.ignite.cache.QueryEntity)1