# ignite-329 Changed example.
Project: http://git-wip-us.apache.org/repos/asf/incubator-ignite/repo Commit: http://git-wip-us.apache.org/repos/asf/incubator-ignite/commit/7cd3cb4e Tree: http://git-wip-us.apache.org/repos/asf/incubator-ignite/tree/7cd3cb4e Diff: http://git-wip-us.apache.org/repos/asf/incubator-ignite/diff/7cd3cb4e Branch: refs/heads/ignite-443 Commit: 7cd3cb4ec793f321917750de50fa77935315c5c4 Parents: ae71c78 Author: anovikov <anovi...@gridgain.com> Authored: Thu Mar 5 08:09:32 2015 +0700 Committer: anovikov <anovi...@gridgain.com> Committed: Thu Mar 5 08:09:32 2015 +0700 ---------------------------------------------------------------------- .../config/store/example-jdbc-pojo-store.xml | 61 ++++++-------------- examples/config/store/initdb.script | 1 + .../store/CacheNodeWithStoreStartup.java | 20 +++---- .../store/jdbc/CacheJdbcPojoPersonStore.java | 33 +++++++++++ .../store/jdbc/CacheAbstractJdbcStore.java | 8 +-- .../cache/store/jdbc/CacheJdbcPojoStore.java | 2 +- modules/schema-load/readme.txt | 50 +++++++++++++++- 7 files changed, 114 insertions(+), 61 deletions(-) ---------------------------------------------------------------------- http://git-wip-us.apache.org/repos/asf/incubator-ignite/blob/7cd3cb4e/examples/config/store/example-jdbc-pojo-store.xml ---------------------------------------------------------------------- diff --git a/examples/config/store/example-jdbc-pojo-store.xml b/examples/config/store/example-jdbc-pojo-store.xml index 22dea8f..8eebbe4 100644 --- a/examples/config/store/example-jdbc-pojo-store.xml +++ b/examples/config/store/example-jdbc-pojo-store.xml @@ -27,10 +27,13 @@ Ignition.start("examples/config/example-jdbc-pojo-store.xml"); --> <beans xmlns="http://www.springframework.org/schema/beans" + xmlns:util="http://www.springframework.org/schema/util" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation=" http://www.springframework.org/schema/beans - http://www.springframework.org/schema/beans/spring-beans.xsd"> + http://www.springframework.org/schema/beans/spring-beans.xsd + http://www.springframework.org/schema/util + http://www.springframework.org/schema/util/spring-util.xsd"> <bean id="ignite.cfg" class="org.apache.ignite.configuration.IgniteConfiguration"> <!-- Set to true to enable distributed class loading for examples, default is false. --> <property name="peerClassLoadingEnabled" value="true"/> @@ -76,53 +79,17 @@ <property name="typeMetadata"> <list> <bean class="org.apache.ignite.cache.CacheTypeMetadata"> - <property name="databaseTable" value="ORGANIZATION"/> - <property name="keyType" value="org.apache.ignite.examples.datagrid.store.model.OrganizationKey"/> - <property name="valueType" value="org.apache.ignite.examples.datagrid.store.model.Organization"/> - <property name="keyFields"> - <list> - <bean class="org.apache.ignite.cache.CacheTypeFieldMetadata"> - <property name="databaseName" value="ID"/> - <property name="databaseType" value="4"/> - <property name="javaName" value="id"/> - <property name="javaType" value="int"/> - </bean> - </list> - </property> - <property name="valueFields"> - <list> - <bean class="org.apache.ignite.cache.CacheTypeFieldMetadata"> - <property name="databaseName" value="ID"/> - <property name="databaseType" value="4"/> - <property name="javaName" value="id"/> - <property name="javaType" value="int"/> - </bean> - <bean class="org.apache.ignite.cache.CacheTypeFieldMetadata"> - <property name="databaseName" value="NAME"/> - <property name="databaseType" value="12"/> - <property name="javaName" value="name"/> - <property name="javaType" value="java.lang.String"/> - </bean> - <bean class="org.apache.ignite.cache.CacheTypeFieldMetadata"> - <property name="databaseName" value="CITY"/> - <property name="databaseType" value="12"/> - <property name="javaName" value="city"/> - <property name="javaType" value="java.lang.String"/> - </bean> - </list> - </property> - </bean> - <bean class="org.apache.ignite.cache.CacheTypeMetadata"> <property name="databaseTable" value="PERSON"/> - <property name="keyType" value="org.apache.ignite.examples.datagrid.store.model.PersonKey"/> + <property name="keyType" value="java.lang.Long"/> <property name="valueType" value="org.apache.ignite.examples.datagrid.store.model.Person"/> <property name="keyFields"> <list> <bean class="org.apache.ignite.cache.CacheTypeFieldMetadata"> <property name="databaseName" value="ID"/> - <property name="databaseType" value="4"/> + <property name="databaseType"> + <util:constant static-field="java.sql.Types.BIGINT"/> + </property> <property name="javaName" value="id"/> - <property name="javaType" value="long"/> </bean> </list> </property> @@ -130,19 +97,25 @@ <list> <bean class="org.apache.ignite.cache.CacheTypeFieldMetadata"> <property name="databaseName" value="ID"/> - <property name="databaseType" value="4"/> + <property name="databaseType"> + <util:constant static-field="java.sql.Types.BIGINT"/> + </property> <property name="javaName" value="id"/> <property name="javaType" value="long"/> </bean> <bean class="org.apache.ignite.cache.CacheTypeFieldMetadata"> <property name="databaseName" value="FIRST_NAME"/> - <property name="databaseType" value="12"/> + <property name="databaseType"> + <util:constant static-field="java.sql.Types.VARCHAR"/> + </property> <property name="javaName" value="firstName"/> <property name="javaType" value="java.lang.String"/> </bean> <bean class="org.apache.ignite.cache.CacheTypeFieldMetadata"> <property name="databaseName" value="LAST_NAME"/> - <property name="databaseType" value="12"/> + <property name="databaseType"> + <util:constant static-field="java.sql.Types.VARCHAR"/> + </property> <property name="javaName" value="lastName"/> <property name="javaType" value="java.lang.String"/> </bean> http://git-wip-us.apache.org/repos/asf/incubator-ignite/blob/7cd3cb4e/examples/config/store/initdb.script ---------------------------------------------------------------------- diff --git a/examples/config/store/initdb.script b/examples/config/store/initdb.script new file mode 100644 index 0000000..8d768ba --- /dev/null +++ b/examples/config/store/initdb.script @@ -0,0 +1 @@ +CREATE TABLE Person(id bigint not null, first_name varchar(50), last_name varchar(50), PRIMARY KEY(id)); http://git-wip-us.apache.org/repos/asf/incubator-ignite/blob/7cd3cb4e/examples/src/main/java/org/apache/ignite/examples/datagrid/store/CacheNodeWithStoreStartup.java ---------------------------------------------------------------------- diff --git a/examples/src/main/java/org/apache/ignite/examples/datagrid/store/CacheNodeWithStoreStartup.java b/examples/src/main/java/org/apache/ignite/examples/datagrid/store/CacheNodeWithStoreStartup.java index 1915c7f..4d39e86 100644 --- a/examples/src/main/java/org/apache/ignite/examples/datagrid/store/CacheNodeWithStoreStartup.java +++ b/examples/src/main/java/org/apache/ignite/examples/datagrid/store/CacheNodeWithStoreStartup.java @@ -22,6 +22,7 @@ import org.apache.ignite.cache.*; import org.apache.ignite.cache.store.*; import org.apache.ignite.configuration.*; import org.apache.ignite.examples.datagrid.store.dummy.*; +import org.apache.ignite.examples.datagrid.store.jdbc.*; import org.apache.ignite.examples.datagrid.store.model.*; import org.apache.ignite.internal.util.typedef.*; import org.apache.ignite.spi.discovery.tcp.*; @@ -76,13 +77,13 @@ public class CacheNodeWithStoreStartup { CacheStore<Long, Person> store; // Uncomment other cache stores to try them. - store = new CacheDummyPersonStore(); +// store = new CacheDummyPersonStore(); // store = new CacheJdbcPersonStore(); // store = new CacheHibernatePersonStore(); // Uncomment two lines for try . - // store = new CacheJdbcPojoPersonStore(); - // cacheCfg.setTypeMetadata(typeMetadata()); + store = new CacheJdbcPojoPersonStore(); + cacheCfg.setTypeMetadata(typeMetadata()); cacheCfg.setCacheStoreFactory(new FactoryBuilder.SingletonFactory<>(store)); cacheCfg.setReadThrough(true); @@ -99,16 +100,15 @@ public class CacheNodeWithStoreStartup { tm.setDatabaseTable("PERSON"); - tm.setKeyType("org.apache.ignite.examples.datagrid.store.model.PersonKey"); + tm.setKeyType("java.lang.Long"); tm.setValueType("org.apache.ignite.examples.datagrid.store.model.Person"); - CacheTypeFieldMetadata keyFld = new CacheTypeFieldMetadata("id", long.class, "ID", Types.BIGINT); + tm.setKeyFields(F.asList(new CacheTypeFieldMetadata("ID", Types.BIGINT, "id", Long.class))); - tm.setKeyFields(F.asList(keyFld)); - - tm.setValueFields(F.asList(keyFld, - new CacheTypeFieldMetadata("firstName", String.class, "FIRST_NAME", Types.VARCHAR), - new CacheTypeFieldMetadata("lastName", String.class, "LAST_NAME", Types.VARCHAR) + tm.setValueFields(F.asList( + new CacheTypeFieldMetadata("ID", Types.BIGINT, "id", long.class), + new CacheTypeFieldMetadata("FIRST_NAME", Types.VARCHAR, "firstName", String.class), + new CacheTypeFieldMetadata("LAST_NAME", Types.VARCHAR, "lastName", String.class) )); return F.asList(tm); http://git-wip-us.apache.org/repos/asf/incubator-ignite/blob/7cd3cb4e/examples/src/main/java/org/apache/ignite/examples/datagrid/store/jdbc/CacheJdbcPojoPersonStore.java ---------------------------------------------------------------------- diff --git a/examples/src/main/java/org/apache/ignite/examples/datagrid/store/jdbc/CacheJdbcPojoPersonStore.java b/examples/src/main/java/org/apache/ignite/examples/datagrid/store/jdbc/CacheJdbcPojoPersonStore.java index 535db10..1996a4d 100644 --- a/examples/src/main/java/org/apache/ignite/examples/datagrid/store/jdbc/CacheJdbcPojoPersonStore.java +++ b/examples/src/main/java/org/apache/ignite/examples/datagrid/store/jdbc/CacheJdbcPojoPersonStore.java @@ -17,11 +17,44 @@ package org.apache.ignite.examples.datagrid.store.jdbc; +import org.apache.ignite.*; import org.apache.ignite.cache.store.jdbc.*; +import org.apache.ignite.internal.util.typedef.internal.*; +import org.apache.ignite.lang.*; +import org.h2.tools.*; +import org.jetbrains.annotations.*; + +import javax.cache.integration.*; +import java.io.*; /** * TODO: Add class description. */ public class CacheJdbcPojoPersonStore<K, V> extends CacheJdbcPojoStore { + public CacheJdbcPojoPersonStore() throws IgniteException { + dataSrc = org.h2.jdbcx.JdbcConnectionPool.create("jdbc:h2:mem:ExampleDb;DB_CLOSE_DELAY=-1", "sa", ""); + + File script = U.resolveIgnitePath("examples/config/store/initdb.script"); + + if (script == null) + throw new IgniteException("Failed to find initial database script: " + "examples/config/store/initdb.script"); + + try { + RunScript.execute(dataSrc.getConnection(), new FileReader(script)); + } + catch (Exception e) { + throw new IgniteException("Failed to initialize database", e); + } + } + + /** {@inheritDoc} */ + @Override public void loadCache(IgniteBiInClosure<Object, Object> clo, @Nullable Object... args) + throws CacheLoaderException { + 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]; + super.loadCache(clo, "java.lang.Long", "select * from PERSON limit " + entryCnt); + } } http://git-wip-us.apache.org/repos/asf/incubator-ignite/blob/7cd3cb4e/modules/core/src/main/java/org/apache/ignite/cache/store/jdbc/CacheAbstractJdbcStore.java ---------------------------------------------------------------------- diff --git a/modules/core/src/main/java/org/apache/ignite/cache/store/jdbc/CacheAbstractJdbcStore.java b/modules/core/src/main/java/org/apache/ignite/cache/store/jdbc/CacheAbstractJdbcStore.java index d652c0f..866f374 100644 --- a/modules/core/src/main/java/org/apache/ignite/cache/store/jdbc/CacheAbstractJdbcStore.java +++ b/modules/core/src/main/java/org/apache/ignite/cache/store/jdbc/CacheAbstractJdbcStore.java @@ -1322,13 +1322,15 @@ public abstract class CacheAbstractJdbcStore<K, V> implements CacheStore<K, V>, this.typeMeta = typeMeta; - final Collection<CacheTypeFieldMetadata> keyFields = typeMeta.getKeyFields(); + Collection<CacheTypeFieldMetadata> keyFields = typeMeta.getKeyFields(); Collection<CacheTypeFieldMetadata> valFields = typeMeta.getValueFields(); + keyCols = databaseColumns(keyFields); + uniqValFields = F.view(valFields, new IgnitePredicate<CacheTypeFieldMetadata>() { @Override public boolean apply(CacheTypeFieldMetadata col) { - return !keyFields.contains(col); + return !keyCols.contains(col.getDatabaseName()); } }); @@ -1338,8 +1340,6 @@ public abstract class CacheAbstractJdbcStore<K, V> implements CacheStore<K, V>, fullTblName = F.isEmpty(schema) ? tblName : schema + "." + tblName; - keyCols = databaseColumns(keyFields); - Collection<String> uniqValCols = databaseColumns(uniqValFields); cols = F.concat(false, keyCols, uniqValCols); http://git-wip-us.apache.org/repos/asf/incubator-ignite/blob/7cd3cb4e/modules/core/src/main/java/org/apache/ignite/cache/store/jdbc/CacheJdbcPojoStore.java ---------------------------------------------------------------------- diff --git a/modules/core/src/main/java/org/apache/ignite/cache/store/jdbc/CacheJdbcPojoStore.java b/modules/core/src/main/java/org/apache/ignite/cache/store/jdbc/CacheJdbcPojoStore.java index 68bc965..162f7de 100644 --- a/modules/core/src/main/java/org/apache/ignite/cache/store/jdbc/CacheJdbcPojoStore.java +++ b/modules/core/src/main/java/org/apache/ignite/cache/store/jdbc/CacheJdbcPojoStore.java @@ -64,7 +64,7 @@ public class CacheJdbcPojoStore extends CacheAbstractJdbcStore<Object, Object> { try { cls = Class.forName(clsName); - if (simpleKey = cls.isAssignableFrom(Number.class) || cls.isInstance(String.class)) + if (simpleKey = (Number.class.isAssignableFrom(cls) || cls.isInstance(String.class))) return; ctor = cls.getDeclaredConstructor(); http://git-wip-us.apache.org/repos/asf/incubator-ignite/blob/7cd3cb4e/modules/schema-load/readme.txt ---------------------------------------------------------------------- diff --git a/modules/schema-load/readme.txt b/modules/schema-load/readme.txt index f3969da..676956e 100644 --- a/modules/schema-load/readme.txt +++ b/modules/schema-load/readme.txt @@ -15,7 +15,6 @@ Use Schema-loading utility for generation of type mapping and domain model in Ja For example you may use the following script for create sample type in your RDBMS system: -CREATE TABLE Organization(id bigint not null, name varchar(50), city varchar(50), PRIMARY KEY(id)); CREATE TABLE Person(id integer not null, firstName varchar(50), lastName varchar(50), PRIMARY KEY(id)); You need place domain model classes, jdbc driver (used for connect to you RDBMS system) in Ignite node classpath, @@ -24,6 +23,8 @@ for example place in 'libs' folder. Append type mapping description generated by Schema-loading utility to your cache configuration and setup DataSource to your RDBMS system for cache store. +Example of spring configuration: + <bean class="org.apache.ignite.configuration.IgniteConfiguration"> ... <!-- Cache configuration. --> @@ -38,7 +39,7 @@ to your RDBMS system for cache store. <constructor-arg> <bean class="org.apache.ignite.cache.store.jdbc.CacheJdbcPojoStore"> <property name="dataSource"> - <!-- Need specify connection pooling DataSource to your RDBMS system. --> + <!-- TODO: Need specify connection pooling DataSource to your RDBMS system. --> ... </property> </bean> @@ -103,3 +104,48 @@ to your RDBMS system for cache store. ... </bean> +Example of java code configuration: + +IgniteConfiguration cfg = new IgniteConfiguration(); +... +CacheConfiguration ccfg = new CacheConfiguration<>(); + +DataSource dataSource = null; // TODO: Need specify connection pooling DataSource to your RDBMS system. + +// Create store. +CacheJdbcPojoStore store = new CacheJdbcPojoStore(); +store.setDataSource(dataSource); + +// Create store factory. +ccfg.setCacheStoreFactory(new FactoryBuilder.SingletonFactory<>(store)); + +// Configure cache to use store. +ccfg.setReadThrough(true); +ccfg.setWriteThrough(true); + +cfg.setCacheConfiguration(ccfg); + +// Configure cache types. +Collection<CacheTypeMetadata> meta = new ArrayList<>(); + +// PERSON. +CacheTypeMetadata type = new CacheTypeMetadata(); +type.setDatabaseSchema("PUBLIC"); +type.setDatabaseTable("PERSON"); +type.setKeyType(long.class.getName()); +type.setValueType("org.apache.ignite.examples.datagrid.store.model.Person"); + +// Key fields for PERSON. +Collection<CacheTypeFieldMetadata> keys = new ArrayList<>(); +keys.add(new CacheTypeFieldMetadata("ID", java.sql.Types.BIGINT,"id", long.class)); +type.setKeyFields(keys); + +// Value fields for PERSON. +Collection<CacheTypeFieldMetadata> vals = new ArrayList<>(); +vals.add(new CacheTypeFieldMetadata("ID", java.sql.Types.BIGINT,"id", long.class)); +vals.add(new CacheTypeFieldMetadata("ORG_ID", java.sql.Types.INTEGER,"orgId", Integer.class)); +vals.add(new CacheTypeFieldMetadata("NAME", java.sql.Types.VARCHAR,"name", String.class)); +type.setValueFields(vals); +... +// Start Ignite node. +Ignition.start(cfg);