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);
}
}
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);
}
}
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());
}
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);
}
Aggregations