Search in sources :

Example 1 with Person

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

the class CacheBinaryAutoStoreExample method cacheConfiguration.

/**
     * Configure cache with store.
     */
private static CacheConfiguration<Long, Person> cacheConfiguration() {
    CacheJdbcPojoStoreFactory<Long, Person> storeFactory = new CacheJdbcPojoStoreFactory<>();
    storeFactory.setDataSourceBean("h2-example-db");
    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);
    CacheConfiguration<Long, Person> cfg = new CacheConfiguration<>(CACHE_NAME);
    cfg.setCacheStoreFactory(storeFactory);
    // Set atomicity as transaction, since we are showing transactions in the example.
    cfg.setAtomicityMode(TRANSACTIONAL);
    // This option will allow to start remote nodes without having user classes in classpath.
    cfg.setStoreKeepBinary(true);
    cfg.setReadThrough(true);
    cfg.setWriteThrough(true);
    return cfg;
}
Also used : CacheJdbcPojoStoreFactory(org.apache.ignite.cache.store.jdbc.CacheJdbcPojoStoreFactory) 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 2 with Person

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

the class CacheLoadOnlyStoreExample method main.

/**
     * Executes example.
     *
     * @param args Command line arguments, none required.
     * @throws IgniteException If example execution failed.
     */
public static void main(String[] args) throws IgniteException {
    try (Ignite ignite = Ignition.start("examples/config/example-ignite.xml")) {
        System.out.println();
        System.out.println(">>> CacheLoadOnlyStoreExample started.");
        ProductLoader productLoader = new ProductLoader("examples/src/main/resources/person.csv");
        productLoader.setThreadsCount(2);
        productLoader.setBatchSize(10);
        productLoader.setBatchQueueSize(1);
        try (IgniteCache<Long, Person> cache = ignite.getOrCreateCache(cacheConfiguration(productLoader))) {
            // load data.
            cache.loadCache(null);
            System.out.println(">>> Loaded number of items: " + cache.size(CachePeekMode.PRIMARY));
            System.out.println(">>> Data for the person by id1: " + cache.get(1L));
        } finally {
            // Distributed cache could be removed from cluster only by #destroyCache() call.
            ignite.destroyCache(CACHE_NAME);
        }
    }
}
Also used : Ignite(org.apache.ignite.Ignite) Person(org.apache.ignite.examples.model.Person)

Example 3 with Person

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

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

the class CacheSpringPersonStore method loadCache.

/** {@inheritDoc} */
@Override
public void loadCache(final IgniteBiInClosure<Long, Person> clo, Object... args) {
    if (args == null || args.length == 0 || args[0] == null)
        throw new CacheLoaderException("Expected entry count parameter is not provided.");
    int entryCnt = (Integer) args[0];
    final AtomicInteger cnt = new AtomicInteger();
    jdbcTemplate.query("select * from PERSON limit ?", new RowCallbackHandler() {

        @Override
        public void processRow(ResultSet rs) throws SQLException {
            Person person = new Person(rs.getLong(1), rs.getString(2), rs.getString(3));
            clo.apply(person.id, person);
            cnt.incrementAndGet();
        }
    }, entryCnt);
    System.out.println(">>> Loaded " + cnt + " values into cache.");
}
Also used : AtomicInteger(java.util.concurrent.atomic.AtomicInteger) AtomicInteger(java.util.concurrent.atomic.AtomicInteger) SQLException(java.sql.SQLException) CacheLoaderException(javax.cache.integration.CacheLoaderException) ResultSet(java.sql.ResultSet) RowCallbackHandler(org.springframework.jdbc.core.RowCallbackHandler) Person(org.apache.ignite.examples.model.Person)

Example 5 with Person

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

the class CacheSpringPersonStore 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 + ']');
    int updated = jdbcTemplate.update("update PERSON set first_name = ?, last_name = ? where id = ?", val.firstName, val.lastName, val.id);
    if (updated == 0) {
        jdbcTemplate.update("insert into PERSON (id, first_name, last_name) values (?, ?, ?)", val.id, val.firstName, val.lastName);
    }
}
Also used : Person(org.apache.ignite.examples.model.Person)

Aggregations

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