Search in sources :

Example 26 with Person

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

use of org.apache.ignite.examples.model.Person 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();
        } 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 28 with Person

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

the class CacheAutoStoreExample method cacheConfiguration.

/**
 * Configure cache with store.
 */
private static CacheConfiguration<Long, Person> cacheConfiguration() {
    CacheConfiguration<Long, Person> cfg = new CacheConfiguration<>(CACHE_NAME);
    CacheJdbcPojoStoreExampleFactory storeFactory = new CacheJdbcPojoStoreExampleFactory();
    storeFactory.setDialect(new H2Dialect());
    JdbcType jdbcType = new JdbcType();
    jdbcType.setCacheName(CACHE_NAME);
    jdbcType.setDatabaseSchema("PUBLIC");
    jdbcType.setDatabaseTable("PERSON");
    jdbcType.setKeyType("java.lang.Long");
    jdbcType.setKeyFields(new JdbcTypeField(Types.BIGINT, "ID", Long.class, "id"));
    jdbcType.setValueType("org.apache.ignite.examples.model.Person");
    jdbcType.setValueFields(new JdbcTypeField(Types.BIGINT, "ID", Long.class, "id"), new JdbcTypeField(Types.VARCHAR, "FIRST_NAME", String.class, "firstName"), new JdbcTypeField(Types.VARCHAR, "LAST_NAME", String.class, "lastName"));
    storeFactory.setTypes(jdbcType);
    cfg.setCacheStoreFactory(storeFactory);
    // Set atomicity as transaction, since we are showing transactions in the example.
    cfg.setAtomicityMode(TRANSACTIONAL);
    cfg.setReadThrough(true);
    cfg.setWriteThrough(true);
    return cfg;
}
Also used : H2Dialect(org.apache.ignite.cache.store.jdbc.dialect.H2Dialect) JdbcType(org.apache.ignite.cache.store.jdbc.JdbcType) JdbcTypeField(org.apache.ignite.cache.store.jdbc.JdbcTypeField) Person(org.apache.ignite.examples.model.Person) CacheConfiguration(org.apache.ignite.configuration.CacheConfiguration)

Example 29 with Person

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

the class CacheAutoStoreExample 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...");
        // Auto-close cache at the end of the example.
        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 30 with Person

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

the class CacheJdbcPersonStore method write.

/**
 * {@inheritDoc}
 */
@Override
public void write(Cache.Entry<? extends Long, ? extends Person> entry) {
    Long key = entry.getKey();
    Person val = entry.getValue();
    System.out.println(">>> Store write [key=" + key + ", val=" + val + ']');
    try {
        Connection conn = ses.attachment();
        int updated;
        // Some databases would allow these to be done in one 'upsert' operation.
        try (PreparedStatement st = conn.prepareStatement("update PERSON set first_name = ?, last_name = ? where id = ?")) {
            st.setString(1, val.firstName);
            st.setString(2, val.lastName);
            st.setLong(3, val.id);
            updated = st.executeUpdate();
        }
        // If update failed, try to insert.
        if (updated == 0) {
            try (PreparedStatement st = conn.prepareStatement("insert into PERSON (id, first_name, last_name) values (?, ?, ?)")) {
                st.setLong(1, val.id);
                st.setString(2, val.firstName);
                st.setString(3, val.lastName);
                st.executeUpdate();
            }
        }
    } catch (SQLException e) {
        throw new CacheWriterException("Failed to write object [key=" + key + ", val=" + val + ']', e);
    }
}
Also used : SQLException(java.sql.SQLException) Connection(java.sql.Connection) PreparedStatement(java.sql.PreparedStatement) Person(org.apache.ignite.examples.model.Person) CacheWriterException(javax.cache.integration.CacheWriterException)

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