Search in sources :

Example 16 with Person

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

the class CacheQueryExample method sqlFieldsQuery.

/**
     * Example for SQL-based fields queries that return only required
     * fields instead of whole key-value pairs.
     */
private static void sqlFieldsQuery() {
    IgniteCache<Long, Person> cache = Ignition.ignite().cache(PERSON_CACHE);
    // Execute query to get names of all employees.
    QueryCursor<List<?>> cursor = cache.query(new SqlFieldsQuery("select concat(firstName, ' ', lastName) from Person"));
    // In this particular case each row will have one element with full name of an employees.
    List<List<?>> res = cursor.getAll();
    // Print names.
    print("Names of all employees:", res);
}
Also used : List(java.util.List) Person(org.apache.ignite.examples.model.Person) SqlFieldsQuery(org.apache.ignite.cache.query.SqlFieldsQuery)

Example 17 with Person

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

the class CacheBinaryAutoStoreExample 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 {
    // To start ignite with desired configuration uncomment the appropriate line.
    try (Ignite ignite = Ignition.start("examples/config/example-ignite.xml")) {
        System.out.println();
        System.out.println(">>> Populate database with data...");
        DbH2ServerStartup.populateDatabase();
        System.out.println();
        System.out.println(">>> Cache auto store example started...");
        try (IgniteCache<Long, Person> cache = ignite.getOrCreateCache(cacheConfiguration())) {
            try (Transaction tx = ignite.transactions().txStart()) {
                Person val = cache.get(id);
                System.out.println(">>> Read value: " + val);
                val = cache.getAndPut(id, new Person(id, 1L, "Isaac", "Newton", 100.10, "English physicist and mathematician"));
                System.out.println(">>> Overwrote old value: " + val);
                val = cache.get(id);
                System.out.println(">>> Read value: " + val);
                System.out.println(">>> Update salary in transaction...");
                val.salary *= 2;
                cache.put(id, val);
                tx.commit();
            }
            System.out.println(">>> Read value after commit: " + cache.get(id));
            cache.clear();
            System.out.println(">>> ------------------------------------------");
            System.out.println(">>> Load data to cache from DB with custom SQL...");
            // Load cache on all data nodes with custom SQL statement.
            cache.loadCache(null, "java.lang.Long", "select * from PERSON where id <= 3");
            System.out.println(">>> Loaded cache entries: " + cache.size());
            cache.clear();
            // Load cache on all data nodes with default SQL statement.
            System.out.println(">>> Load ALL data to cache from DB...");
            cache.loadCache(null);
            System.out.println(">>> Loaded cache entries: " + cache.size());
        } finally {
            // Distributed cache could be removed from cluster only by #destroyCache() call.
            ignite.destroyCache(CACHE_NAME);
        }
    }
}
Also used : Transaction(org.apache.ignite.transactions.Transaction) Ignite(org.apache.ignite.Ignite) Person(org.apache.ignite.examples.model.Person)

Example 18 with Person

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

the class SpringDataExample method queryRepository.

/**
 * Execute advanced queries over the repository.
 */
private static void queryRepository() {
    System.out.println("\n>>> Persons with name 'John':");
    List<Person> persons = repo.findByFirstName("John");
    for (Person person : persons) System.out.println("   >>>   " + person);
    Cache.Entry<Long, Person> topPerson = repo.findTopByLastNameLike("Smith");
    System.out.println("\n>>> Top Person with surname 'Smith': " + topPerson.getValue());
    List<Long> ids = repo.selectId(1000L, new PageRequest(0, 4));
    System.out.println("\n>>> Persons working for organization with ID > 1000: ");
    for (Long id : ids) System.out.println("   >>>   [id=" + id + "]");
}
Also used : PageRequest(org.springframework.data.domain.PageRequest) Person(org.apache.ignite.examples.model.Person) Cache(javax.cache.Cache)

Example 19 with Person

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

the class SqlDmlExample 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 20 with Person

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

the class CacheJdbcStoreExample method executeTransaction.

/**
 * Executes transaction with read/write-through to persistent store.
 *
 * @param cache Cache to execute transaction on.
 */
private static void executeTransaction(IgniteCache<Long, Person> cache) {
    try (Transaction tx = Ignition.ignite().transactions().txStart()) {
        Person val = cache.get(id);
        System.out.println("Read value: " + val);
        val = cache.getAndPut(id, new Person(id, "Isaac", "Newton"));
        System.out.println("Overwrote old value: " + val);
        val = cache.get(id);
        System.out.println("Read value: " + val);
        tx.commit();
    }
    System.out.println("Read value after commit: " + cache.get(id));
    // Clear entry from memory, but keep it in store.
    cache.clear(id);
    // Operations on this cache will not affect store.
    IgniteCache<Long, Person> cacheSkipStore = cache.withSkipStore();
    System.out.println("Read value skipping store (expecting null): " + cacheSkipStore.get(id));
    System.out.println("Read value with store lookup (expecting NOT null): " + cache.get(id));
    // Expecting not null, since entry should be in memory since last call.
    System.out.println("Read value skipping store (expecting NOT null): " + cacheSkipStore.get(id));
}
Also used : Transaction(org.apache.ignite.transactions.Transaction) Person(org.apache.ignite.examples.model.Person)

Aggregations

Person (org.apache.ignite.examples.model.Person)34 Ignite (org.apache.ignite.Ignite)10 CacheConfiguration (org.apache.ignite.configuration.CacheConfiguration)9 AffinityKey (org.apache.ignite.cache.affinity.AffinityKey)6 Organization (org.apache.ignite.examples.model.Organization)6 List (java.util.List)5 CacheLoaderException (javax.cache.integration.CacheLoaderException)5 Transaction (org.apache.ignite.transactions.Transaction)5 SQLException (java.sql.SQLException)4 SqlFieldsQuery (org.apache.ignite.cache.query.SqlFieldsQuery)4 Connection (java.sql.Connection)3 PreparedStatement (java.sql.PreparedStatement)3 ResultSet (java.sql.ResultSet)3 CacheStoreSession (org.apache.ignite.cache.store.CacheStoreSession)3 CacheStoreSessionListener (org.apache.ignite.cache.store.CacheStoreSessionListener)3 HibernateException (org.hibernate.HibernateException)3 Session (org.hibernate.Session)3 CacheWriterException (javax.cache.integration.CacheWriterException)2 SqlQuery (org.apache.ignite.cache.query.SqlQuery)2 JdbcType (org.apache.ignite.cache.store.jdbc.JdbcType)2