Search in sources :

Example 1 with JdbcTypeField

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;
}
Also used : CacheJdbcPojoStoreFactory(org.apache.ignite.cache.store.jdbc.CacheJdbcPojoStoreFactory) H2Dialect(org.apache.ignite.cache.store.jdbc.dialect.H2Dialect) JdbcType(org.apache.ignite.cache.store.jdbc.JdbcType) JdbcTypeField(org.apache.ignite.cache.store.jdbc.JdbcTypeField) Person(org.apache.ignite.examples.model.Person) CacheConfiguration(org.apache.ignite.configuration.CacheConfiguration)

Example 2 with JdbcTypeField

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[]
}
Also used : JdbcType(org.apache.ignite.cache.store.jdbc.JdbcType) QueryEntity(org.apache.ignite.cache.QueryEntity) MysqlDataSource(com.mysql.cj.jdbc.MysqlDataSource) DataSource(javax.sql.DataSource) LinkedHashMap(java.util.LinkedHashMap) MySQLDialect(org.apache.ignite.cache.store.jdbc.dialect.MySQLDialect) CacheJdbcPojoStoreFactory(org.apache.ignite.cache.store.jdbc.CacheJdbcPojoStoreFactory) IgniteConfiguration(org.apache.ignite.configuration.IgniteConfiguration) MysqlDataSource(com.mysql.cj.jdbc.MysqlDataSource) JdbcTypeField(org.apache.ignite.cache.store.jdbc.JdbcTypeField) CacheConfiguration(org.apache.ignite.configuration.CacheConfiguration) HashSet(java.util.HashSet)

Example 3 with JdbcTypeField

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;
}
Also used : H2Dialect(org.apache.ignite.cache.store.jdbc.dialect.H2Dialect) JdbcType(org.apache.ignite.cache.store.jdbc.JdbcType) JdbcTypeField(org.apache.ignite.cache.store.jdbc.JdbcTypeField) Person(org.apache.ignite.examples.model.Person) CacheConfiguration(org.apache.ignite.configuration.CacheConfiguration)

Aggregations

JdbcType (org.apache.ignite.cache.store.jdbc.JdbcType)3 JdbcTypeField (org.apache.ignite.cache.store.jdbc.JdbcTypeField)3 CacheConfiguration (org.apache.ignite.configuration.CacheConfiguration)3 CacheJdbcPojoStoreFactory (org.apache.ignite.cache.store.jdbc.CacheJdbcPojoStoreFactory)2 H2Dialect (org.apache.ignite.cache.store.jdbc.dialect.H2Dialect)2 Person (org.apache.ignite.examples.model.Person)2 MysqlDataSource (com.mysql.cj.jdbc.MysqlDataSource)1 HashSet (java.util.HashSet)1 LinkedHashMap (java.util.LinkedHashMap)1 DataSource (javax.sql.DataSource)1 QueryEntity (org.apache.ignite.cache.QueryEntity)1 MySQLDialect (org.apache.ignite.cache.store.jdbc.dialect.MySQLDialect)1 IgniteConfiguration (org.apache.ignite.configuration.IgniteConfiguration)1