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