# ignite-330 Fixed 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/f716692b Tree: http://git-wip-us.apache.org/repos/asf/incubator-ignite/tree/f716692b Diff: http://git-wip-us.apache.org/repos/asf/incubator-ignite/diff/f716692b Branch: refs/heads/ignite-45 Commit: f716692b944cdc3e54fec88e257fa836ba987e70 Parents: 6986cbf Author: anovikov <anovi...@gridgain.com> Authored: Mon Mar 23 12:14:43 2015 +0700 Committer: anovikov <anovi...@gridgain.com> Committed: Mon Mar 23 12:14:43 2015 +0700 ---------------------------------------------------------------------- .../datagrid/store/CacheStoreExample.java | 16 --- .../CacheStoreExampleCacheConfigurator.java | 36 ------ .../store/auto/CacheAutoStoreExample.java | 80 ++++++++++++ .../auto/CacheAutoStoreLoadDataExample.java | 123 +++++++++++++++++++ .../datagrid/store/auto/CacheConfig.java | 80 ++++++++++++ .../examples/datagrid/store/auto/H2Startup.java | 67 ++++++++++ .../store/jdbc/CacheJdbcPojoPersonStore.java | 82 ------------- 7 files changed, 350 insertions(+), 134 deletions(-) ---------------------------------------------------------------------- http://git-wip-us.apache.org/repos/asf/incubator-ignite/blob/f716692b/examples/src/main/java/org/apache/ignite/examples/datagrid/store/CacheStoreExample.java ---------------------------------------------------------------------- diff --git a/examples/src/main/java/org/apache/ignite/examples/datagrid/store/CacheStoreExample.java b/examples/src/main/java/org/apache/ignite/examples/datagrid/store/CacheStoreExample.java index 39d5452..b5b8098 100644 --- a/examples/src/main/java/org/apache/ignite/examples/datagrid/store/CacheStoreExample.java +++ b/examples/src/main/java/org/apache/ignite/examples/datagrid/store/CacheStoreExample.java @@ -72,22 +72,6 @@ public class CacheStoreExample { } System.out.println("Read value after commit: " + cache.get(id)); - - // If example run with CacheJdbcPojoStore. - // Example of CacheJdbcPojoStore special features. - if (STORE.equals(AUTO)) { - System.out.println(">>> Example of CacheJdbcPojoStore special feature: load from DB with custom SQL."); - - cache.clear(); - - System.out.println("Cache size: " + cache.size()); - - // Load values from DB into store with custom SQL. - cache.loadCache(null, "java.lang.Long", "select * from PERSON where id = 2"); - - System.out.println("Cache size: " + cache.size()); - System.out.println("Person: " + cache.get(2L)); - } } } } http://git-wip-us.apache.org/repos/asf/incubator-ignite/blob/f716692b/examples/src/main/java/org/apache/ignite/examples/datagrid/store/CacheStoreExampleCacheConfigurator.java ---------------------------------------------------------------------- diff --git a/examples/src/main/java/org/apache/ignite/examples/datagrid/store/CacheStoreExampleCacheConfigurator.java b/examples/src/main/java/org/apache/ignite/examples/datagrid/store/CacheStoreExampleCacheConfigurator.java index d44c2a1..249ea1f 100644 --- a/examples/src/main/java/org/apache/ignite/examples/datagrid/store/CacheStoreExampleCacheConfigurator.java +++ b/examples/src/main/java/org/apache/ignite/examples/datagrid/store/CacheStoreExampleCacheConfigurator.java @@ -18,17 +18,13 @@ package org.apache.ignite.examples.datagrid.store; import org.apache.ignite.*; -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.hibernate.*; import org.apache.ignite.examples.datagrid.store.jdbc.*; -import org.apache.ignite.internal.util.typedef.*; import javax.cache.configuration.*; -import java.sql.*; -import java.util.*; import static org.apache.ignite.cache.CacheAtomicityMode.*; @@ -45,9 +41,6 @@ public class CacheStoreExampleCacheConfigurator { /** Use org.apache.ignite.examples.datagrid.store.hibernate.CacheHibernatePersonStore to run example. */ public static final String HIBERNATE = "HIBERNATE"; - /** Use org.apache.ignite.examples.datagrid.store.jdbc.CacheJdbcPojoPersonStore to run example. */ - public static final String AUTO = "AUTO"; - /** Store to use. */ public static final String STORE = DUMMY; @@ -80,10 +73,6 @@ public class CacheStoreExampleCacheConfigurator { store = new CacheHibernatePersonStore(); break; - case AUTO: - store = new CacheJdbcPojoPersonStore(); - break; - default: throw new IllegalStateException("Unexpected store configured: " + STORE); } @@ -92,34 +81,9 @@ public class CacheStoreExampleCacheConfigurator { } }); - if (STORE.equals(AUTO)) - cacheCfg.setTypeMetadata(typeMetadata()); - cacheCfg.setReadThrough(true); cacheCfg.setWriteThrough(true); return cacheCfg; } - - /** - * @return Type mapping description. - */ - private static Collection<CacheTypeMetadata> typeMetadata() { - CacheTypeMetadata tm = new CacheTypeMetadata(); - - tm.setDatabaseTable("PERSON"); - - tm.setKeyType("java.lang.Long"); - tm.setValueType("org.apache.ignite.examples.datagrid.store.model.Person"); - - tm.setKeyFields(F.asList(new CacheTypeFieldMetadata("ID", Types.BIGINT, "id", Long.class))); - - 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/f716692b/examples/src/main/java/org/apache/ignite/examples/datagrid/store/auto/CacheAutoStoreExample.java ---------------------------------------------------------------------- diff --git a/examples/src/main/java/org/apache/ignite/examples/datagrid/store/auto/CacheAutoStoreExample.java b/examples/src/main/java/org/apache/ignite/examples/datagrid/store/auto/CacheAutoStoreExample.java new file mode 100644 index 0000000..36057e3 --- /dev/null +++ b/examples/src/main/java/org/apache/ignite/examples/datagrid/store/auto/CacheAutoStoreExample.java @@ -0,0 +1,80 @@ +/* + * 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.auto; + +import org.apache.ignite.*; +import org.apache.ignite.cache.store.jdbc.*; +import org.apache.ignite.configuration.*; +import org.apache.ignite.examples.*; +import org.apache.ignite.examples.datagrid.store.*; +import org.apache.ignite.transactions.*; + +import java.util.*; + +/** + * Example of {@link CacheJdbcPojoStore} implementation that uses JDBC + * transaction with cache transactions and maps {@link Long} to {@link Person}. + * <p> + * To run this example your should start {@link H2Startup} first. + * <p> + * Remote nodes should always be started with special configuration file which + * enables P2P class loading: {@code 'ignite.{sh|bat} examples/config/example-ignite.xml'}. + * <p> + * Alternatively you can run {@link ExampleNodeStartup} in another JVM which will + * start node with {@code examples/config/example-ignite.xml} configuration. + */ +public class CacheAutoStoreExample { + /** Global person ID to use across entire example. */ + private static final Long id = Math.abs(UUID.randomUUID().getLeastSignificantBits()); + + /** + * Executes example. + * + * @param args Command line arguments, none required. + * @throws IgniteException If example execution failed. + */ + public static void main(String[] args) throws IgniteException { + // To start ignite with desired configuration uncomment the appropriate line. + try (Ignite ignite = Ignition.start("examples/config/example-ignite.xml")) { + System.out.println(); + System.out.println(">>> Cache auto store example started."); + + CacheConfiguration<Long, Person> cacheCfg = CacheConfig.jdbcPojoStoreCache(); + + try (IgniteCache<Long, Person> cache = ignite.createCache(cacheCfg)) { + try (Transaction tx = ignite.transactions().txStart()) { + Person val = cache.get(id); + + System.out.println("Read value: " + val); + + val = cache.getAndPut(id, new Person(id, "Isaac", "Newton")); + + System.out.println("Overwrote old value: " + val); + + val = cache.get(id); + + System.out.println("Read value: " + val); + + tx.commit(); + } + + System.out.println("Read value after commit: " + cache.get(id)); + } + } + } +} http://git-wip-us.apache.org/repos/asf/incubator-ignite/blob/f716692b/examples/src/main/java/org/apache/ignite/examples/datagrid/store/auto/CacheAutoStoreLoadDataExample.java ---------------------------------------------------------------------- diff --git a/examples/src/main/java/org/apache/ignite/examples/datagrid/store/auto/CacheAutoStoreLoadDataExample.java b/examples/src/main/java/org/apache/ignite/examples/datagrid/store/auto/CacheAutoStoreLoadDataExample.java new file mode 100644 index 0000000..3158c4b --- /dev/null +++ b/examples/src/main/java/org/apache/ignite/examples/datagrid/store/auto/CacheAutoStoreLoadDataExample.java @@ -0,0 +1,123 @@ +/* + * 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.auto; + +import org.apache.ignite.*; +import org.apache.ignite.cache.store.jdbc.*; +import org.apache.ignite.configuration.*; +import org.apache.ignite.examples.*; +import org.apache.ignite.examples.datagrid.store.*; +import org.h2.jdbcx.*; +import org.h2.tools.*; + +import java.io.*; +import java.sql.*; + +/** + * Example of load data from database through {@link CacheJdbcPojoStore} implementation. + * This example shows how to load all data or with custom SQL. + * <p> + * To run this example your should start {@link H2Startup} first. + * <p> + * Remote nodes should always be started with special configuration file which + * enables P2P class loading: {@code 'ignite.{sh|bat} examples/config/example-ignite.xml'}. + * <p> + * Alternatively you can run {@link ExampleNodeStartup} in another JVM which will + * start node with {@code examples/config/example-ignite.xml} configuration. + */ +public class CacheAutoStoreLoadDataExample { + /** Heap size required to run this example. */ + public static final int MIN_MEMORY = 1024 * 1024 * 1024; + + /** */ + private static final String DB_SCRIPT = + "delete from PERSON;\n" + + "insert into PERSON(id, first_name, last_name) values(1, 'Johannes', 'Kepler');\n" + + "insert into PERSON(id, first_name, last_name) values(2, 'Galileo', 'Galilei');\n" + + "insert into PERSON(id, first_name, last_name) values(3, 'Henry', 'More');\n" + + "insert into PERSON(id, first_name, last_name) values(4, 'Polish', 'Brethren');\n" + + "insert into PERSON(id, first_name, last_name) values(5, 'Robert', 'Boyle');\n" + + "insert into PERSON(id, first_name, last_name) values(6, 'Isaac', 'Newton');"; + + /** + * Executes example. + * + * @param args Command line arguments, none required. + * @throws IgniteException If example execution failed. + */ + public static void main(String[] args) throws IgniteException { + ExamplesUtils.checkMinMemory(MIN_MEMORY); + + initializeDatabase(); + + try (Ignite ignite = Ignition.start("examples/config/example-ignite.xml")) { + System.out.println(); + System.out.println(">>> Cache auto store load data example started."); + + CacheConfiguration<Long, Person> cacheCfg = CacheConfig.jdbcPojoStoreCache(); + + try (IgniteCache<Long, Person> cache = ignite.createCache(cacheCfg)) { + System.out.println(">>> Load cache from database using custom script."); + + System.out.println(">>> Cache size: " + cache.size()); + + long start = System.currentTimeMillis(); + + // Start loading cache from persistent store on all caching nodes. + cache.loadCache(null, "java.lang.Long", "select * from PERSON where id <= 3"); + + long end = System.currentTimeMillis(); + + System.out.println(">>> Loaded " + cache.size() + " keys with backups in " + (end - start) + "ms."); + + System.out.println(">>> Load cache from database."); + + cache.clear(); + + System.out.println(">>> Cache size: " + cache.size()); + + start = System.currentTimeMillis(); + + // Start loading cache from persistent store on all caching nodes. + cache.loadCache(null); + + end = System.currentTimeMillis(); + + System.out.println(">>> Loaded " + cache.size() + " keys with backups in " + (end - start) + "ms."); + } + } + } + + /** + * Prepares database for load example execution. + * + * @throws IgniteException If failed. + */ + private static void initializeDatabase() throws IgniteException { + try { + // Try to connect to database server. + JdbcConnectionPool dataSrc = JdbcConnectionPool.create("jdbc:h2:tcp://localhost/mem:ExampleDb", "sa", ""); + + // Load sample data into database. + RunScript.execute(dataSrc.getConnection(), new StringReader(DB_SCRIPT)); + } + catch (SQLException e) { + throw new IgniteException("Failed to initialize database", e); + } + } +} http://git-wip-us.apache.org/repos/asf/incubator-ignite/blob/f716692b/examples/src/main/java/org/apache/ignite/examples/datagrid/store/auto/CacheConfig.java ---------------------------------------------------------------------- diff --git a/examples/src/main/java/org/apache/ignite/examples/datagrid/store/auto/CacheConfig.java b/examples/src/main/java/org/apache/ignite/examples/datagrid/store/auto/CacheConfig.java new file mode 100644 index 0000000..f4ad8f9 --- /dev/null +++ b/examples/src/main/java/org/apache/ignite/examples/datagrid/store/auto/CacheConfig.java @@ -0,0 +1,80 @@ +/* + * 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.auto; + +import org.apache.ignite.cache.*; +import org.apache.ignite.cache.store.*; +import org.apache.ignite.cache.store.jdbc.*; +import org.apache.ignite.configuration.*; +import org.apache.ignite.examples.datagrid.store.*; +import org.apache.ignite.internal.util.typedef.*; +import org.h2.jdbcx.*; + +import javax.cache.configuration.*; +import java.sql.*; + +import static org.apache.ignite.cache.CacheAtomicityMode.*; + +/** + * Predefined configuration for examples with {@link CacheJdbcPojoStore}. + */ +public class CacheConfig { + /** + * Configure cache with JDBC mappings. + */ + public static CacheConfiguration<Long, Person> jdbcPojoStoreCache() { + CacheConfiguration<Long, Person> cfg = new CacheConfiguration<>(); + + // Set atomicity as transaction, since we are showing transactions in example. + cfg.setAtomicityMode(TRANSACTIONAL); + + cfg.setCacheStoreFactory(new Factory<CacheStore<? super Long, ? super Person>>() { + @Override public CacheStore<? super Long, ? super Person> create() { + CacheJdbcPojoStore<Long, Person> store = new CacheJdbcPojoStore<>(); + + store.setDataSource(JdbcConnectionPool.create("jdbc:h2:tcp://localhost/mem:ExampleDb", "sa", "")); + + return store; + } + }); + + CacheTypeMetadata tm = new CacheTypeMetadata(); + + tm.setDatabaseTable("PERSON"); + + tm.setKeyType("java.lang.Long"); + tm.setValueType("org.apache.ignite.examples.datagrid.store.Person"); + + tm.setKeyFields(F.asList(new CacheTypeFieldMetadata("ID", Types.BIGINT, "id", Long.class))); + + 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) + )); + + cfg.setTypeMetadata(F.asList(tm)); + + cfg.setWriteBehindEnabled(true); + + cfg.setReadThrough(true); + cfg.setWriteThrough(true); + + return cfg; + } +} http://git-wip-us.apache.org/repos/asf/incubator-ignite/blob/f716692b/examples/src/main/java/org/apache/ignite/examples/datagrid/store/auto/H2Startup.java ---------------------------------------------------------------------- diff --git a/examples/src/main/java/org/apache/ignite/examples/datagrid/store/auto/H2Startup.java b/examples/src/main/java/org/apache/ignite/examples/datagrid/store/auto/H2Startup.java new file mode 100644 index 0000000..16aac7e --- /dev/null +++ b/examples/src/main/java/org/apache/ignite/examples/datagrid/store/auto/H2Startup.java @@ -0,0 +1,67 @@ +/* + * 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.auto; + +import org.apache.ignite.*; +import org.h2.jdbcx.*; +import org.h2.tools.*; + +import java.io.*; +import java.sql.*; + +/** + * Start H2 database TCP server in order to access sample in-memory database from other processes. + */ +public class H2Startup { + /** */ + private static final String DB_SCRIPT = + "create table PERSON(id bigint not null, first_name varchar(50), last_name varchar(50), PRIMARY KEY(id));"; + + /** + * Start H2 database TCP server. + * + * @param args Command line arguments, none required. + * @throws IgniteException If start H2 database TCP server failed. + */ + public static void main(String[] args) throws IgniteException { + try { + // Start H2 database TCP server in order to access sample in-memory database from other processes. + Server.createTcpServer("-tcpDaemon").start(); + + // Try to connect to database TCP server. + JdbcConnectionPool dataSrc = JdbcConnectionPool.create("jdbc:h2:tcp://localhost/mem:ExampleDb", "sa", ""); + + // Load sample data into database. + RunScript.execute(dataSrc.getConnection(), new StringReader(DB_SCRIPT)); + } + catch (SQLException e) { + throw new IgniteException("Failed to start database TCP server", e); + } + + try { + do { + System.out.println("Type 'q' and press 'Enter' to stop H2 TCP server..."); + } + while ('q' != System.in.read()); + } + catch (IOException ignored) { + // No-op. + } + } +} http://git-wip-us.apache.org/repos/asf/incubator-ignite/blob/f716692b/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 deleted file mode 100644 index e69ef7a..0000000 --- a/examples/src/main/java/org/apache/ignite/examples/datagrid/store/jdbc/CacheJdbcPojoPersonStore.java +++ /dev/null @@ -1,82 +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.jdbc; - -import org.apache.ignite.*; -import org.apache.ignite.cache.store.jdbc.*; -import org.apache.ignite.examples.datagrid.store.*; -import org.h2.tools.*; - -import javax.cache.*; -import java.io.*; -import java.sql.*; - -/** - * Example of {@link CacheJdbcPojoStore} implementation that uses JDBC - * transaction with cache transactions and maps {@link Long} to {@link Person}. - */ -public class CacheJdbcPojoPersonStore extends CacheJdbcPojoStore<Long, Person> { - /** - * Constructor. - * - * @throws IgniteException If failed. - */ - public CacheJdbcPojoPersonStore() throws IgniteException { - try { - // Try to connect to database server. - dataSrc = org.h2.jdbcx.JdbcConnectionPool.create("jdbc:h2:tcp://localhost/mem:ExampleDb", "sa", ""); - - resolveDialect(); - } - catch (CacheException ignore) { - // Construct example database in memory. - dataSrc = org.h2.jdbcx.JdbcConnectionPool.create("jdbc:h2:mem:ExampleDb;DB_CLOSE_DELAY=-1", "sa", ""); - - prepareDb(); - } - } - - /** */ - private static final String DB_SCRIPT = - "create table PERSON(id bigint not null, first_name varchar(50), last_name varchar(50), PRIMARY KEY(id));\n" + - "insert into PERSON(id, first_name, last_name) values(1, 'Johannes', 'Kepler');\n" + - "insert into PERSON(id, first_name, last_name) values(2, 'Galileo', 'Galilei');\n" + - "insert into PERSON(id, first_name, last_name) values(3, 'Henry', 'More');\n" + - "insert into PERSON(id, first_name, last_name) values(4, 'Polish', 'Brethren');\n" + - "insert into PERSON(id, first_name, last_name) values(5, 'Robert', 'Boyle');\n" + - "insert into PERSON(id, first_name, last_name) values(6, 'Isaac', 'Newton');"; - - /** - * 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. - */ - private void prepareDb() throws IgniteException { - try { - // Start H2 database TCP server in order to access sample in-memory database from other processes. - Server.createTcpServer("-tcpDaemon").start(); - - // Load sample data into database. - RunScript.execute(dataSrc.getConnection(), new StringReader(DB_SCRIPT)); - } - catch (SQLException e) { - throw new IgniteException("Failed to initialize database", e); - } - } -}