use of org.apache.ignite.cache.store.jdbc.JdbcTypeField 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.cache.store.jdbc.JdbcTypeField in project ignite by apache.
the class ExternalStorage method cacheJdbcPojoStoreExample.
public static void cacheJdbcPojoStoreExample() {
// tag::pojo[]
IgniteConfiguration igniteCfg = new IgniteConfiguration();
CacheConfiguration<Integer, Person> personCacheCfg = new CacheConfiguration<>();
personCacheCfg.setName("PersonCache");
personCacheCfg.setCacheMode(CacheMode.PARTITIONED);
personCacheCfg.setAtomicityMode(CacheAtomicityMode.ATOMIC);
personCacheCfg.setReadThrough(true);
personCacheCfg.setWriteThrough(true);
CacheJdbcPojoStoreFactory<Integer, Person> factory = new CacheJdbcPojoStoreFactory<>();
factory.setDialect(new MySQLDialect());
factory.setDataSourceFactory((Factory<DataSource>) () -> {
MysqlDataSource mysqlDataSrc = new MysqlDataSource();
mysqlDataSrc.setURL("jdbc:mysql://[host]:[port]/[database]");
mysqlDataSrc.setUser("YOUR_USER_NAME");
mysqlDataSrc.setPassword("YOUR_PASSWORD");
return mysqlDataSrc;
});
JdbcType personType = new JdbcType();
personType.setCacheName("PersonCache");
personType.setKeyType(Integer.class);
personType.setValueType(Person.class);
// Specify the schema if applicable
// personType.setDatabaseSchema("MY_DB_SCHEMA");
personType.setDatabaseTable("PERSON");
personType.setKeyFields(new JdbcTypeField(java.sql.Types.INTEGER, "id", Integer.class, "id"));
personType.setValueFields(new JdbcTypeField(java.sql.Types.INTEGER, "id", Integer.class, "id"));
personType.setValueFields(new JdbcTypeField(java.sql.Types.VARCHAR, "name", String.class, "name"));
factory.setTypes(personType);
personCacheCfg.setCacheStoreFactory(factory);
QueryEntity qryEntity = new QueryEntity();
qryEntity.setKeyType(Integer.class.getName());
qryEntity.setValueType(Person.class.getName());
qryEntity.setKeyFieldName("id");
Set<String> keyFields = new HashSet<>();
keyFields.add("id");
qryEntity.setKeyFields(keyFields);
LinkedHashMap<String, String> fields = new LinkedHashMap<>();
fields.put("id", "java.lang.Integer");
fields.put("name", "java.lang.String");
qryEntity.setFields(fields);
personCacheCfg.setQueryEntities(Collections.singletonList(qryEntity));
igniteCfg.setCacheConfiguration(personCacheCfg);
// end::pojo[]
}
use of org.apache.ignite.cache.store.jdbc.JdbcTypeField in project ignite by apache.
the class CacheAutoStoreExample method cacheConfiguration.
/**
* Configure cache with store.
*/
private static CacheConfiguration<Long, Person> cacheConfiguration() {
CacheConfiguration<Long, Person> cfg = new CacheConfiguration<>(CACHE_NAME);
CacheJdbcPojoStoreExampleFactory storeFactory = new CacheJdbcPojoStoreExampleFactory();
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);
cfg.setCacheStoreFactory(storeFactory);
// Set atomicity as transaction, since we are showing transactions in the example.
cfg.setAtomicityMode(TRANSACTIONAL);
cfg.setReadThrough(true);
cfg.setWriteThrough(true);
return cfg;
}
Aggregations