Search in sources :

Example 31 with Person

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

the class CacheJdbcPersonStore method load.

/**
 * {@inheritDoc}
 */
@Override
public Person load(Long key) {
    System.out.println(">>> Store load [key=" + key + ']');
    Connection conn = ses.attachment();
    try (PreparedStatement st = conn.prepareStatement("select * from PERSON where id = ?")) {
        st.setString(1, key.toString());
        ResultSet rs = st.executeQuery();
        return rs.next() ? new Person(rs.getLong(1), rs.getString(2), rs.getString(3)) : null;
    } catch (SQLException e) {
        throw new CacheLoaderException("Failed to load object [key=" + key + ']', e);
    }
}
Also used : SQLException(java.sql.SQLException) CacheLoaderException(javax.cache.integration.CacheLoaderException) Connection(java.sql.Connection) ResultSet(java.sql.ResultSet) PreparedStatement(java.sql.PreparedStatement) Person(org.apache.ignite.examples.model.Person)

Example 32 with Person

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

the class CacheJdbcPersonStore method loadCache.

/**
 * {@inheritDoc}
 */
@Override
public void loadCache(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.");
    final int entryCnt = (Integer) args[0];
    Connection conn = ses.attachment();
    try (PreparedStatement stmt = conn.prepareStatement("select * from PERSON limit ?")) {
        stmt.setInt(1, entryCnt);
        ResultSet rs = stmt.executeQuery();
        int cnt = 0;
        while (rs.next()) {
            Person person = new Person(rs.getLong(1), rs.getString(2), rs.getString(3));
            clo.apply(person.id, person);
            cnt++;
        }
        System.out.println(">>> Loaded " + cnt + " values into cache.");
    } catch (SQLException e) {
        throw new CacheLoaderException("Failed to load values from cache store.", e);
    }
}
Also used : SQLException(java.sql.SQLException) CacheLoaderException(javax.cache.integration.CacheLoaderException) Connection(java.sql.Connection) ResultSet(java.sql.ResultSet) PreparedStatement(java.sql.PreparedStatement) Person(org.apache.ignite.examples.model.Person)

Example 33 with Person

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

the class SqlQueriesExample method sqlQueryWithDistributedJoin.

/**
 * Example for SQL queries based on all employees working
 * for a specific organization (query uses distributed join).
 */
private static void sqlQueryWithDistributedJoin() {
    IgniteCache<Long, Person> cache = Ignition.ignite().cache(PERSON_CACHE);
    // SQL clause query which joins on 2 types to select people for a specific organization.
    String joinSql = "from Person, \"" + ORG_CACHE + "\".Organization as org " + "where Person.orgId = org.id " + "and lower(org.name) = lower(?)";
    SqlQuery qry = new SqlQuery<Long, Person>(Person.class, joinSql).setArgs("ApacheIgnite");
    // Enable distributed joins for query.
    qry.setDistributedJoins(true);
    // Execute queries for find employees for different organizations.
    print("Following people are 'ApacheIgnite' employees (distributed join): ", cache.query(qry).getAll());
    qry.setArgs("Other");
    print("Following people are 'Other' employees (distributed join): ", cache.query(qry).getAll());
}
Also used : SqlQuery(org.apache.ignite.cache.query.SqlQuery) Person(org.apache.ignite.examples.model.Person)

Example 34 with Person

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

the class SqlQueriesExample method sqlFieldsQueryWithJoin.

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

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