# ignite-329 Fixed store examples.
Project: http://git-wip-us.apache.org/repos/asf/incubator-ignite/repo Commit: http://git-wip-us.apache.org/repos/asf/incubator-ignite/commit/4c78ee58 Tree: http://git-wip-us.apache.org/repos/asf/incubator-ignite/tree/4c78ee58 Diff: http://git-wip-us.apache.org/repos/asf/incubator-ignite/diff/4c78ee58 Branch: refs/heads/ignite-443 Commit: 4c78ee58ebb15e471f79780c5c5f7730969766cd Parents: a4a59eb Author: anovikov <anovi...@gridgain.com> Authored: Tue Mar 10 14:15:39 2015 +0700 Committer: anovikov <anovi...@gridgain.com> Committed: Tue Mar 10 14:36:44 2015 +0700 ---------------------------------------------------------------------- .../CacheJdbcPojoStoreLoadDataExample.java | 174 ------------------- .../store/CacheNodeWithStoreStartup.java | 4 +- .../store/jdbc/CacheJdbcPojoPersonStore.java | 18 +- .../datagrid/store/model/Organization.java | 155 ----------------- .../datagrid/store/model/OrganizationKey.java | 97 ----------- .../store/jdbc/CacheAbstractJdbcStore.java | 10 +- .../cache/store/jdbc/CacheJdbcPojoStore.java | 2 - modules/schema-load/readme.txt | 54 +++--- 8 files changed, 57 insertions(+), 457 deletions(-) ---------------------------------------------------------------------- http://git-wip-us.apache.org/repos/asf/incubator-ignite/blob/4c78ee58/examples/src/main/java/org/apache/ignite/examples/datagrid/store/CacheJdbcPojoStoreLoadDataExample.java ---------------------------------------------------------------------- diff --git a/examples/src/main/java/org/apache/ignite/examples/datagrid/store/CacheJdbcPojoStoreLoadDataExample.java b/examples/src/main/java/org/apache/ignite/examples/datagrid/store/CacheJdbcPojoStoreLoadDataExample.java deleted file mode 100644 index ae51ede..0000000 --- a/examples/src/main/java/org/apache/ignite/examples/datagrid/store/CacheJdbcPojoStoreLoadDataExample.java +++ /dev/null @@ -1,174 +0,0 @@ -/* - * Licensed to the Apache Software Foundation (ASF) under one or more - * contributor license agreements. See the NOTICE file distributed with - * this work for additional information regarding copyright ownership. - * The ASF licenses this file to You under the Apache License, Version 2.0 - * (the "License"); you may not use this file except in compliance with - * the License. You may obtain a copy of the License at - * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, software - * distributed under the License is distributed on an "AS IS" BASIS, - * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. - * See the License for the specific language governing permissions and - * limitations under the License. - */ - -package org.apache.ignite.examples.datagrid.store; - -import org.apache.ignite.*; -import org.apache.ignite.examples.datagrid.store.model.*; -import org.apache.ignite.internal.util.typedef.internal.*; -import org.h2.tools.*; - -import java.sql.*; - -/** - * This examples demonstrates loading data into cache from underlying JDBC store. - * <p> - * Remote nodes should always be started with special configuration file: - * {@code 'ignite.{sh|bat} examples/config/store/example-jdbc-pojo-store.xml'}. - */ -public class CacheJdbcPojoStoreLoadDataExample { - /** DB connection URL. */ - private static final String CONN_URL = "jdbc:h2:mem:ExampleDb;DB_CLOSE_DELAY=-1"; - - /** Cache name. */ - private static final String CACHE_NAME = "partitioned"; - - /** Number of generated organizations. */ - private static final int ORGANIZATION_CNT = 5; - - /** Number of generated persons. */ - private static final int PERSON_CNT = 100; - - /** - * Executes example. - * - * @param args Command line arguments, none required. - * @throws Exception If example execution failed. - */ - public static void main(String[] args) throws Exception { - Server srv = null; - - // Start node and load cache from database. - try (Ignite ignite = Ignition.start("examples/config/store/example-jdbc-pojo-store.xml")) { - System.out.println(); - System.out.println(">>> Cache auto-loading data example started."); - - prepareDb(); - - // Start H2 database TCP server in order to access sample in-memory database from other processes. - srv = Server.createTcpServer().start(); - - IgniteCache<Object, Object> cache = ignite.jcache(CACHE_NAME); - - // Clean up caches on all nodes before run. - cache.clear(); - - System.out.println(); - System.out.println(">>> Load whole DB into cache."); - - cache.loadCache(null); - - System.out.println(); - System.out.println(">>> Print loaded content."); - - System.out.println("Organizations:"); - for (int i = 0; i < ORGANIZATION_CNT; i++) { - OrganizationKey orgKey = new OrganizationKey(i); - - System.out.println(" " + cache.get(orgKey)); - } - - System.out.println("Persons:"); - for (int i = 0; i < PERSON_CNT; i++) { - PersonKey prnKey = new PersonKey(i); - - System.out.println(" " + cache.get(prnKey)); - } - - System.out.println(">>> Clear cache for next demo."); - - cache.clear(); - - System.out.println(">>> Cache size = " + cache.size()); - - System.out.println(">>> Load cache by custom SQL."); - - // JDBC cache store accept pairs of "full key class name -> SQL statement" - cache.loadCache(null, - "org.apache.ignite.examples.datagrid.store.model.OrganizationKey", - "SELECT * FROM Organization WHERE id = 2", - "org.apache.ignite.examples.datagrid.store.model.PersonKey", - "SELECT * FROM Person WHERE id = 5"); - - System.out.println(">>> Check custom SQL."); - System.out.println(">>> Organization: " + cache.get(new OrganizationKey(2))); - System.out.println(">>> Person: " + cache.get(new PersonKey(5))); - } - finally { - // Stop H2 TCP server. - if (srv != null) - srv.stop(); - } - - System.exit(0); - } - - /** - * Create example DB and populate it with sample data. - * - * @throws Exception If failed to create database and populate it with sample data. - */ - private static void prepareDb() throws Exception { - Connection conn = DriverManager.getConnection(CONN_URL, "sa", ""); - - Statement stmt = conn.createStatement(); - - stmt.executeUpdate("CREATE TABLE IF NOT EXISTS Organization" + - "(id integer not null, name varchar(50), city varchar(50), PRIMARY KEY(id))"); - - stmt.executeUpdate("CREATE TABLE IF NOT EXISTS Person" + - "(id integer not null, first_name varchar(50), last_name varchar(50), PRIMARY KEY(id))"); - - U.closeQuiet(stmt); - - conn.commit(); - - PreparedStatement st = conn.prepareStatement("INSERT INTO Organization(id, name, city) VALUES (?, ?, ?)"); - - for (int i = 0; i < ORGANIZATION_CNT; i++) { - st.setInt(1, i); - st.setString(2, "name-" + i); - st.setString(3, "city-" + i); - - st.addBatch(); - } - - st.executeBatch(); - - U.closeQuiet(st); - - conn.commit(); - - st = conn.prepareStatement("INSERT INTO Person(id, first_name, last_name) VALUES (?, ?, ?)"); - - for (int i = 0; i < PERSON_CNT; i++) { - st.setInt(1, i); - st.setString(3, "firstName-" + i); - st.setString(3, "lastName-" + i); - - st.addBatch(); - } - - st.executeBatch(); - - U.closeQuiet(st); - - conn.commit(); - - U.closeQuiet(conn); - } -} http://git-wip-us.apache.org/repos/asf/incubator-ignite/blob/4c78ee58/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 568435e..64f9370 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 @@ -85,8 +85,8 @@ public class CacheNodeWithStoreStartup { // store = new CacheHibernatePersonStore(); // Uncomment two lines for try CacheJdbcPojoStore. -// store = new CacheJdbcPojoPersonStore(); -// cacheCfg.setTypeMetadata(typeMetadata()); + store = new CacheJdbcPojoPersonStore(); + cacheCfg.setTypeMetadata(typeMetadata()); cacheCfg.setCacheStoreFactory(new FactoryBuilder.SingletonFactory<>(store)); cacheCfg.setReadThrough(true); http://git-wip-us.apache.org/repos/asf/incubator-ignite/blob/4c78ee58/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 778e005..dd15184 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 @@ -34,6 +34,9 @@ import java.sql.*; * transaction with cache transactions and maps {@link Long} to {@link Person}. */ public class CacheJdbcPojoPersonStore extends CacheJdbcPojoStore<Long, Person> { + /** H2 database TCP server. */ + private Server srv; + /** * Constructor. * @@ -47,8 +50,8 @@ public class CacheJdbcPojoPersonStore extends CacheJdbcPojoStore<Long, Person> { } /** - * Prepares database for example execution. This method will create a - * table called "PERSONS" so it can be used by store implementation. + * Prepares database for example execution. This method will create a table called "PERSONS" + * so it can be used by store implementation. * * @throws IgniteException If failed. */ @@ -61,6 +64,9 @@ public class CacheJdbcPojoPersonStore extends CacheJdbcPojoStore<Long, Person> { try { RunScript.execute(dataSrc.getConnection(), new FileReader(script)); + + // Start H2 database TCP server in order to access sample in-memory database from other processes. + srv = Server.createTcpServer().start(); } catch (SQLException e) { throw new IgniteException("Failed to initialize database", e); @@ -80,4 +86,12 @@ public class CacheJdbcPojoPersonStore extends CacheJdbcPojoStore<Long, Person> { super.loadCache(clo, "java.lang.Long", "select * from PERSONS limit " + entryCnt); } + + /** {@inheritDoc} */ + @Override public void stop() throws IgniteException { + if (srv != null) + srv.stop(); + + super.stop(); + } } http://git-wip-us.apache.org/repos/asf/incubator-ignite/blob/4c78ee58/examples/src/main/java/org/apache/ignite/examples/datagrid/store/model/Organization.java ---------------------------------------------------------------------- diff --git a/examples/src/main/java/org/apache/ignite/examples/datagrid/store/model/Organization.java b/examples/src/main/java/org/apache/ignite/examples/datagrid/store/model/Organization.java deleted file mode 100644 index 98e62eb..0000000 --- a/examples/src/main/java/org/apache/ignite/examples/datagrid/store/model/Organization.java +++ /dev/null @@ -1,155 +0,0 @@ -/* - * Licensed to the Apache Software Foundation (ASF) under one or more - * contributor license agreements. See the NOTICE file distributed with - * this work for additional information regarding copyright ownership. - * The ASF licenses this file to You under the Apache License, Version 2.0 - * (the "License"); you may not use this file except in compliance with - * the License. You may obtain a copy of the License at - * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, software - * distributed under the License is distributed on an "AS IS" BASIS, - * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. - * See the License for the specific language governing permissions and - * limitations under the License. - */ - -package org.apache.ignite.examples.datagrid.store.model; - -import java.io.*; - -/** - * Organization definition. - * - * Code generated by Apache Ignite Schema Load utility: 02/24/2015. - */ -public class Organization implements Serializable { - /** */ - private static final long serialVersionUID = 0L; - - /** Value for id. */ - private int id; - - /** Value for name. */ - private String name; - - /** Value for city. */ - private String city; - - /** - * Empty constructor. - */ - public Organization() { - // No-op. - } - - /** - * Full constructor. - */ - public Organization( - int id, - String name, - String city - ) { - this.id = id; - this.name = name; - this.city = city; - } - - /** - * Gets id. - * - * @return Value for id. - */ - public int getId() { - return id; - } - - /** - * Sets id. - * - * @param id New value for id. - */ - public void setId(int id) { - this.id = id; - } - - /** - * Gets name. - * - * @return Value for name. - */ - public String getName() { - return name; - } - - /** - * Sets name. - * - * @param name New value for name. - */ - public void setName(String name) { - this.name = name; - } - - /** - * Gets city. - * - * @return Value for city. - */ - public String getCity() { - return city; - } - - /** - * Sets city. - * - * @param city New value for city. - */ - public void setCity(String city) { - this.city = city; - } - - /** {@inheritDoc} */ - @Override public boolean equals(Object o) { - if (this == o) - return true; - - if (!(o instanceof Organization)) - return false; - - Organization that = (Organization)o; - - if (id != that.id) - return false; - - if (name != null ? !name.equals(that.name) : that.name != null) - return false; - - if (city != null ? !city.equals(that.city) : that.city != null) - return false; - - return true; - } - - /** {@inheritDoc} */ - @Override public int hashCode() { - int res = id; - - res = 31 * res + (name != null ? name.hashCode() : 0); - - res = 31 * res + (city != null ? city.hashCode() : 0); - - return res; - } - - /** {@inheritDoc} */ - @Override public String toString() { - return "Organization [id=" + id + - ", name=" + name + - ", city=" + city + - "]"; - } -} - http://git-wip-us.apache.org/repos/asf/incubator-ignite/blob/4c78ee58/examples/src/main/java/org/apache/ignite/examples/datagrid/store/model/OrganizationKey.java ---------------------------------------------------------------------- diff --git a/examples/src/main/java/org/apache/ignite/examples/datagrid/store/model/OrganizationKey.java b/examples/src/main/java/org/apache/ignite/examples/datagrid/store/model/OrganizationKey.java deleted file mode 100644 index f142754..0000000 --- a/examples/src/main/java/org/apache/ignite/examples/datagrid/store/model/OrganizationKey.java +++ /dev/null @@ -1,97 +0,0 @@ -/* - * Licensed to the Apache Software Foundation (ASF) under one or more - * contributor license agreements. See the NOTICE file distributed with - * this work for additional information regarding copyright ownership. - * The ASF licenses this file to You under the Apache License, Version 2.0 - * (the "License"); you may not use this file except in compliance with - * the License. You may obtain a copy of the License at - * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, software - * distributed under the License is distributed on an "AS IS" BASIS, - * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. - * See the License for the specific language governing permissions and - * limitations under the License. - */ - -package org.apache.ignite.examples.datagrid.store.model; - -import java.io.*; - -/** - * OrganizationKey definition. - * - * Code generated by Apache Ignite Schema Load utility: 02/24/2015. - */ -public class OrganizationKey implements Serializable { - /** */ - private static final long serialVersionUID = 0L; - - /** Value for id. */ - private int id; - - /** - * Empty constructor. - */ - public OrganizationKey() { - // No-op. - } - - /** - * Full constructor. - */ - public OrganizationKey( - int id - ) { - this.id = id; - } - - /** - * Gets id. - * - * @return Value for id. - */ - public int getId() { - return id; - } - - /** - * Sets id. - * - * @param id New value for id. - */ - public void setId(int id) { - this.id = id; - } - - /** {@inheritDoc} */ - @Override public boolean equals(Object o) { - if (this == o) - return true; - - if (!(o instanceof OrganizationKey)) - return false; - - OrganizationKey that = (OrganizationKey)o; - - if (id != that.id) - return false; - - return true; - } - - /** {@inheritDoc} */ - @Override public int hashCode() { - int res = id; - - return res; - } - - /** {@inheritDoc} */ - @Override public String toString() { - return "OrganizationKey [id=" + id + - "]"; - } -} - http://git-wip-us.apache.org/repos/asf/incubator-ignite/blob/4c78ee58/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 866f374..80a22c0 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 @@ -22,7 +22,6 @@ import org.apache.ignite.cache.*; import org.apache.ignite.cache.store.*; import org.apache.ignite.cache.store.jdbc.dialect.*; import org.apache.ignite.configuration.*; -import org.apache.ignite.internal.*; import org.apache.ignite.internal.util.tostring.*; import org.apache.ignite.internal.util.typedef.*; import org.apache.ignite.internal.util.typedef.internal.*; @@ -485,8 +484,15 @@ public abstract class CacheAbstractJdbcStore<K, V> implements CacheStore<K, V>, entryMappings = U.newHashMap(types.size()); - for (CacheTypeMetadata type : types) + for (CacheTypeMetadata type : types) { + Object keyTypeId = keyTypeId(type.getKeyType()); + + if (entryMappings.containsKey(keyTypeId)) + throw new CacheException("Key type must be unique in type metadata [cache name=" + cacheName + + ", key type=" + type.getKeyType() + "]"); + entryMappings.put(keyTypeId(type.getKeyType()), new EntryMapping(cacheName, dialect, type)); + } Map<String, Map<Object, EntryMapping>> mappings = new HashMap<>(cacheMappings); http://git-wip-us.apache.org/repos/asf/incubator-ignite/blob/4c78ee58/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 620c396..601a237 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 @@ -132,8 +132,6 @@ public class CacheJdbcPojoStore<K, V> extends CacheAbstractJdbcStore<K, V> { throws CacheException { Map<String, PojoMethodsCache> typeMethods = U.newHashMap(types.size() * 2); - // TODO Check for diff type of key - for (CacheTypeMetadata type : types) { String keyType = type.getKeyType(); typeMethods.put(keyType, new PojoMethodsCache(keyType, type.getKeyFields())); http://git-wip-us.apache.org/repos/asf/incubator-ignite/blob/4c78ee58/modules/schema-load/readme.txt ---------------------------------------------------------------------- diff --git a/modules/schema-load/readme.txt b/modules/schema-load/readme.txt index 676956e..48049fb 100644 --- a/modules/schema-load/readme.txt +++ b/modules/schema-load/readme.txt @@ -4,7 +4,7 @@ Apache Ignite Schema-loading Module Apache Ignite Schema-loading module provides a simple utility that automatically reads the database schema, creates required type mapping description, and optionally generates the domain model in Java. -For running utility use 'ignite-schema-load.{sh|bat}' script. For connection with RDBMS system from utility +For running utility use 'ignite-schema-import.{sh|bat}' script. For connection with RDBMS system from utility you need to provide: connection url and jdbc driver. @@ -15,9 +15,16 @@ 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 Person(id integer not null, firstName varchar(50), lastName varchar(50), PRIMARY KEY(id)); +create table PERSONS(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, +insert into PERSONS(id, first_name, last_name) values(1, 'Johannes', 'Kepler'); +insert into PERSONS(id, first_name, last_name) values(2, 'Galileo', 'Galilei'); +insert into PERSONS(id, first_name, last_name) values(3, 'Henry', 'More'); +insert into PERSONS(id, first_name, last_name) values(4, 'Polish', 'Brethren'); +insert into PERSONS(id, first_name, last_name) values(5, 'Robert', 'Boyle'); +insert into PERSONS(id, first_name, last_name) values(6, 'Isaac', 'Newton'); + +You need place compiled domain model classes, jdbc driver (used for connect to you RDBMS system) in Ignite node classpath, for example place in 'libs' folder. Append type mapping description generated by Schema-loading utility to your cache configuration and setup DataSource @@ -123,29 +130,30 @@ ccfg.setCacheStoreFactory(new FactoryBuilder.SingletonFactory<>(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); +// PERSONS type mapping. +CacheTypeMetadata tm = new CacheTypeMetadata(); + +tm.setDatabaseTable("PERSONS"); + +tm.setKeyType("java.lang.Long"); +tm.setValueType("org.apache.ignite.examples.datagrid.store.model.Person"); + +// Key fields for PERSONS. +tm.setKeyFields(F.asList(new CacheTypeFieldMetadata("ID", Types.BIGINT, "id", Long.class))); + +// Value fields for PERSONS. +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) +)); +... +ccfg.setTypeMetadata(tm); + +cfg.setCacheConfiguration(ccfg); ... // Start Ignite node. Ignition.start(cfg);