From f9038471553d5b32d76443acc1c9104d85016e5a Mon Sep 17 00:00:00 2001 From: AKuznetsov Date: Tue, 24 Feb 2015 15:15:27 +0700 Subject: [PATCH] # IGNITE-329 Example of CacheTypeMetadata and CacheJdbcPojoStore usage. --- examples/config/store/example-jdbc-pojo-store.xml | 183 +++++++++++++++++++++ .../datagrid/CacheJdbcPojoStoreExample.java | 167 +++++++++++++++++++ .../datagrid/store/model/Organization.java | 155 +++++++++++++++++ .../datagrid/store/model/OrganizationKey.java | 97 +++++++++++ .../examples/datagrid/store/model/Person.java | 155 +++++++++++++++++ .../examples/datagrid/store/model/PersonKey.java | 97 +++++++++++ 6 files changed, 854 insertions(+) create mode 100644 examples/config/store/example-jdbc-pojo-store.xml create mode 100644 examples/src/main/java/org/apache/ignite/examples/datagrid/CacheJdbcPojoStoreExample.java create mode 100644 examples/src/main/java/org/apache/ignite/examples/datagrid/store/model/Organization.java create mode 100644 examples/src/main/java/org/apache/ignite/examples/datagrid/store/model/OrganizationKey.java create mode 100644 examples/src/main/java/org/apache/ignite/examples/datagrid/store/model/Person.java create mode 100644 examples/src/main/java/org/apache/ignite/examples/datagrid/store/model/PersonKey.java diff --git a/examples/config/store/example-jdbc-pojo-store.xml b/examples/config/store/example-jdbc-pojo-store.xml new file mode 100644 index 0000000..608d9c1 --- /dev/null +++ b/examples/config/store/example-jdbc-pojo-store.xml @@ -0,0 +1,183 @@ + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + 127.0.0.1:47500..47501 + + + + + + + + diff --git a/examples/src/main/java/org/apache/ignite/examples/datagrid/CacheJdbcPojoStoreExample.java b/examples/src/main/java/org/apache/ignite/examples/datagrid/CacheJdbcPojoStoreExample.java new file mode 100644 index 0000000..f76afc8 --- /dev/null +++ b/examples/src/main/java/org/apache/ignite/examples/datagrid/CacheJdbcPojoStoreExample.java @@ -0,0 +1,167 @@ +/* + * 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; + +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.*; +import java.util.*; + +/** + * This examples demonstrates loading data into cache from underlying JDBC store. + */ +public class CacheJdbcPojoStoreExample { + /** 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 { + System.out.println("Populate database with sample data."); + + createDb(); + + // Start H2 database TCP server in order to access sample in-memory database from other processes. + // This is H2 specific only. + Server srv = Server.createTcpServer().start(); + + // Start node and load cache from database. + try (Ignite ignite = Ignition.start("examples/config/store/example-jdbc-pojo-store.xml")) { + IgniteCache cache = ignite.jcache(CACHE_NAME); + + System.out.println("Load whole DB into cache."); + + cache.loadCache(null); + + 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))); + } + + // Stop H2 TCP server. H2 specific only. + srv.stop(); + + System.exit(0); + } + + /** + * Create example DB and populate it with sample data. + * + * @throws Exception If failed to create databse and populate it with sample data. + */ + private static void createDb() throws Exception { + Class.forName("org.h2.Driver"); + + 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, org_id integer, name varchar(50), PRIMARY KEY(id))"); + + U.closeQuiet(stmt); + + conn.commit(); + + PreparedStatement orgStmt = conn.prepareStatement("INSERT INTO Organization(id, name, city) VALUES (?, ?, ?)"); + + for (int i = 0; i < ORGANIZATION_CNT; i++) { + orgStmt.setInt(1, i); + orgStmt.setString(2, "org-name-" + i); + orgStmt.setString(3, "city-" + i); + + orgStmt.addBatch(); + } + + orgStmt.executeBatch(); + + U.closeQuiet(orgStmt); + + conn.commit(); + + PreparedStatement prnStmt = conn.prepareStatement("INSERT INTO Person(id, org_id, name) VALUES (?, ?, ?)"); + + Random rnd = new Random(); + + for (int i = 0; i < PERSON_CNT; i++) { + prnStmt.setInt(1, i); + prnStmt.setInt(2, rnd.nextInt(ORGANIZATION_CNT)); + prnStmt.setString(3, "person-name-" + i); + + prnStmt.addBatch(); + } + + prnStmt.executeBatch(); + + U.closeQuiet(prnStmt); + + conn.commit(); + + U.closeQuiet(conn); + } +} 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 new file mode 100644 index 0000000..98e62eb --- /dev/null +++ b/examples/src/main/java/org/apache/ignite/examples/datagrid/store/model/Organization.java @@ -0,0 +1,155 @@ +/* + * 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 + + "]"; + } +} + 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 new file mode 100644 index 0000000..f142754 --- /dev/null +++ b/examples/src/main/java/org/apache/ignite/examples/datagrid/store/model/OrganizationKey.java @@ -0,0 +1,97 @@ +/* + * 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 + + "]"; + } +} + diff --git a/examples/src/main/java/org/apache/ignite/examples/datagrid/store/model/Person.java b/examples/src/main/java/org/apache/ignite/examples/datagrid/store/model/Person.java new file mode 100644 index 0000000..ad56534 --- /dev/null +++ b/examples/src/main/java/org/apache/ignite/examples/datagrid/store/model/Person.java @@ -0,0 +1,155 @@ +/* + * 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.*; + +/** + * Person definition. + * + * Code generated by Apache Ignite Schema Load utility: 02/24/2015. + */ +public class Person implements Serializable { + /** */ + private static final long serialVersionUID = 0L; + + /** Value for id. */ + private int id; + + /** Value for orgId. */ + private Integer orgId; + + /** Value for name. */ + private String name; + + /** + * Empty constructor. + */ + public Person() { + // No-op. + } + + /** + * Full constructor. + */ + public Person( + int id, + Integer orgId, + String name + ) { + this.id = id; + this.orgId = orgId; + this.name = name; + } + + /** + * 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 orgId. + * + * @return Value for orgId. + */ + public Integer getOrgId() { + return orgId; + } + + /** + * Sets orgId. + * + * @param orgId New value for orgId. + */ + public void setOrgId(Integer orgId) { + this.orgId = orgId; + } + + /** + * 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; + } + + /** {@inheritDoc} */ + @Override public boolean equals(Object o) { + if (this == o) + return true; + + if (!(o instanceof Person)) + return false; + + Person that = (Person)o; + + if (id != that.id) + return false; + + if (orgId != null ? !orgId.equals(that.orgId) : that.orgId != null) + return false; + + if (name != null ? !name.equals(that.name) : that.name != null) + return false; + + return true; + } + + /** {@inheritDoc} */ + @Override public int hashCode() { + int res = id; + + res = 31 * res + (orgId != null ? orgId.hashCode() : 0); + + res = 31 * res + (name != null ? name.hashCode() : 0); + + return res; + } + + /** {@inheritDoc} */ + @Override public String toString() { + return "Person [id=" + id + + ", orgId=" + orgId + + ", name=" + name + + "]"; + } +} + diff --git a/examples/src/main/java/org/apache/ignite/examples/datagrid/store/model/PersonKey.java b/examples/src/main/java/org/apache/ignite/examples/datagrid/store/model/PersonKey.java new file mode 100644 index 0000000..ec517d5 --- /dev/null +++ b/examples/src/main/java/org/apache/ignite/examples/datagrid/store/model/PersonKey.java @@ -0,0 +1,97 @@ +/* + * 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.*; + +/** + * PersonKey definition. + * + * Code generated by Apache Ignite Schema Load utility: 02/24/2015. + */ +public class PersonKey implements Serializable { + /** */ + private static final long serialVersionUID = 0L; + + /** Value for id. */ + private int id; + + /** + * Empty constructor. + */ + public PersonKey() { + // No-op. + } + + /** + * Full constructor. + */ + public PersonKey( + 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 PersonKey)) + return false; + + PersonKey that = (PersonKey)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 "PersonKey [id=" + id + + "]"; + } +} + -- 1.9.4.msysgit.0