Search in sources :

Example 6 with Person

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

the class CacheSpringStoreExample 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 {
    ExamplesUtils.checkMinMemory(MIN_MEMORY);
    // 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(">>> Cache store example started.");
        CacheConfiguration<Long, Person> cacheCfg = new CacheConfiguration<>(CACHE_NAME);
        // Set atomicity as transaction, since we are showing transactions in example.
        cacheCfg.setAtomicityMode(TRANSACTIONAL);
        // Configure Spring store.
        cacheCfg.setCacheStoreFactory(FactoryBuilder.factoryOf(CacheSpringPersonStore.class));
        // Configure Spring session listener.
        cacheCfg.setCacheStoreSessionListenerFactories(new Factory<CacheStoreSessionListener>() {

            @Override
            public CacheStoreSessionListener create() {
                CacheSpringStoreSessionListener lsnr = new CacheSpringStoreSessionListener();
                lsnr.setDataSource(CacheSpringPersonStore.DATA_SRC);
                return lsnr;
            }
        });
        cacheCfg.setReadThrough(true);
        cacheCfg.setWriteThrough(true);
        // Auto-close cache at the end of the example.
        try (IgniteCache<Long, Person> cache = ignite.getOrCreateCache(cacheCfg)) {
            // Make initial cache loading from persistent store. This is a
            // distributed operation and will call CacheStore.loadCache(...)
            // method on all nodes in topology.
            loadCache(cache);
            // Start transaction and execute several cache operations with
            // read/write-through to persistent store.
            executeTransaction(cache);
        } finally {
            // Distributed cache could be removed from cluster only by #destroyCache() call.
            ignite.destroyCache(CACHE_NAME);
        }
    }
}
Also used : CacheSpringStoreSessionListener(org.apache.ignite.cache.store.spring.CacheSpringStoreSessionListener) Ignite(org.apache.ignite.Ignite) Person(org.apache.ignite.examples.model.Person) CacheStoreSessionListener(org.apache.ignite.cache.store.CacheStoreSessionListener) CacheConfiguration(org.apache.ignite.configuration.CacheConfiguration)

Example 7 with Person

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

the class CacheHibernateStoreExample 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));
}
Also used : Transaction(org.apache.ignite.transactions.Transaction) Person(org.apache.ignite.examples.model.Person)

Example 8 with Person

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

the class CacheHibernateStoreExample 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 {
    ExamplesUtils.checkMinMemory(MIN_MEMORY);
    // 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(">>> Cache store example started.");
        CacheConfiguration<Long, Person> cacheCfg = new CacheConfiguration<>(CACHE_NAME);
        // Set atomicity as transaction, since we are showing transactions in example.
        cacheCfg.setAtomicityMode(TRANSACTIONAL);
        // Configure Hibernate store.
        cacheCfg.setCacheStoreFactory(FactoryBuilder.factoryOf(CacheHibernatePersonStore.class));
        // Configure Hibernate session listener.
        cacheCfg.setCacheStoreSessionListenerFactories(new Factory<CacheStoreSessionListener>() {

            @Override
            public CacheStoreSessionListener create() {
                CacheHibernateStoreSessionListener lsnr = new CacheHibernateStoreSessionListener();
                lsnr.setHibernateConfigurationPath(HIBERNATE_CFG);
                return lsnr;
            }
        });
        cacheCfg.setReadThrough(true);
        cacheCfg.setWriteThrough(true);
        // Auto-close cache at the end of the example.
        try (IgniteCache<Long, Person> cache = ignite.getOrCreateCache(cacheCfg)) {
            // Make initial cache loading from persistent store. This is a
            // distributed operation and will call CacheStore.loadCache(...)
            // method on all nodes in topology.
            loadCache(cache);
            // Start transaction and execute several cache operations with
            // read/write-through to persistent store.
            executeTransaction(cache);
        } finally {
            // Distributed cache could be removed from cluster only by #destroyCache() call.
            ignite.destroyCache(CACHE_NAME);
        }
    }
}
Also used : CacheHibernateStoreSessionListener(org.apache.ignite.cache.store.hibernate.CacheHibernateStoreSessionListener) Ignite(org.apache.ignite.Ignite) Person(org.apache.ignite.examples.model.Person) CacheStoreSessionListener(org.apache.ignite.cache.store.CacheStoreSessionListener) CacheConfiguration(org.apache.ignite.configuration.CacheConfiguration)

Example 9 with Person

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

the class SpringDataExample method populateRepository.

/**
     * Fills the repository in with sample data.
     */
private static void populateRepository() {
    TreeMap<Long, Person> persons = new TreeMap<>();
    persons.put(1L, new Person(1L, 2000L, "John", "Smith", 15000, "Worked for Apple"));
    persons.put(2L, new Person(2L, 2000L, "Brad", "Pitt", 16000, "Worked for Oracle"));
    persons.put(3L, new Person(3L, 1000L, "Mark", "Tomson", 10000, "Worked for Sun"));
    persons.put(4L, new Person(4L, 2000L, "Erick", "Smith", 13000, "Worked for Apple"));
    persons.put(5L, new Person(5L, 1000L, "John", "Rozenberg", 25000, "Worked for RedHat"));
    persons.put(6L, new Person(6L, 2000L, "Denis", "Won", 35000, "Worked for CBS"));
    persons.put(7L, new Person(7L, 1000L, "Abdula", "Adis", 45000, "Worked for NBC"));
    persons.put(8L, new Person(8L, 2000L, "Roman", "Ive", 15000, "Worked for Sun"));
    // Adding data into the repository.
    repo.save(persons);
    System.out.println("\n>>> Added " + repo.count() + " Persons into the repository.");
}
Also used : TreeMap(java.util.TreeMap) Person(org.apache.ignite.examples.model.Person)

Example 10 with Person

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

the class SpringDataExample method findPersons.

/**
     * Gets a list of Persons using standard read operations.
     */
private static void findPersons() {
    // Getting Person with specific ID.
    Person person = repo.findOne(2L);
    System.out.println("\n>>> Found Person [id=" + 2L + ", val=" + person + "]");
    // Getting a list of Persons.
    ArrayList<Long> ids = new ArrayList<>();
    for (long i = 0; i < 5; i++) ids.add(i);
    Iterator<Person> persons = repo.findAll(ids).iterator();
    System.out.println("\n>>> Persons list for specific ids: ");
    while (persons.hasNext()) System.out.println("   >>>   " + persons.next());
}
Also used : ArrayList(java.util.ArrayList) 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