├── .idea ├── copyright │ └── profiles_settings.xml ├── vcs.xml ├── modules.xml ├── misc.xml ├── compiler.xml └── Ignite-SQL-Getting-Started.iml ├── README.md ├── JDBC ├── JdbcExampleDelete.java ├── JdbcExampleUpdate.java ├── JdbcExampleCreateIndexes.java ├── JdbcExampleSelect.java ├── JdbcExampleCreateTables.java └── JdbcExampleInsert.java ├── sql.script ├── config ├── example-ignite.xml └── example-default.xml ├── pom.xml └── java └── main └── org └── apache └── ignite └── gettingstarted └── JavaSample.java /.idea/copyright/profiles_settings.xml: -------------------------------------------------------------------------------- 1 | 2 | 3 | -------------------------------------------------------------------------------- /.idea/vcs.xml: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | 6 | -------------------------------------------------------------------------------- /.idea/modules.xml: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | 6 | 7 | 8 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | # Ignite-SQL-Getting-Started 2 | SQL code examples used in Apache Ignite Getting Started Guide: 3 | https://apacheignite.readme.io/docs/getting-started-sql 4 | 5 | * JDBC folder - comprises JDBC examples. 6 | * java folder - include Java API examples. 7 | * sql.script - contains all the SQL statements used (can be executed in an SQL tool). 8 | -------------------------------------------------------------------------------- /JDBC/JdbcExampleDelete.java: -------------------------------------------------------------------------------- 1 | import java.sql.Connection; 2 | import java.sql.DriverManager; 3 | import java.sql.Statement; 4 | 5 | public class JdbcExampleDelete { 6 | 7 | public static void main(String[] args) throws Exception { 8 | System.out.println("JDBC example started."); 9 | 10 | // Register JDBC driver 11 | Class.forName("org.apache.ignite.IgniteJdbcDriver"); 12 | 13 | // Open JDBC connection 14 | Connection conn = DriverManager.getConnection( 15 | "jdbc:ignite:thin://127.0.0.1/"); 16 | 17 | // Delete 18 | try (Statement stmt = conn.createStatement()) { 19 | 20 | // Delete person 21 | stmt.executeUpdate("DELETE FROM person WHERE name = 'John Doe'"); 22 | } 23 | 24 | System.out.println("Deleted data."); 25 | } 26 | } 27 | -------------------------------------------------------------------------------- /JDBC/JdbcExampleUpdate.java: -------------------------------------------------------------------------------- 1 | import java.sql.Connection; 2 | import java.sql.DriverManager; 3 | import java.sql.Statement; 4 | 5 | public class JdbcExampleUpdate { 6 | 7 | public static void main(String[] args) throws Exception { 8 | System.out.println("JDBC example started."); 9 | 10 | // Register JDBC driver 11 | Class.forName("org.apache.ignite.IgniteJdbcDriver"); 12 | 13 | // Open JDBC connection 14 | Connection conn = DriverManager.getConnection( 15 | "jdbc:ignite:thin://127.0.0.1/"); 16 | 17 | // Update 18 | try (Statement stmt = conn.createStatement()) { 19 | 20 | // Update city 21 | stmt.executeUpdate("UPDATE city SET name = 'Foster City' WHERE id = 2"); 22 | } 23 | 24 | System.out.println("Updated data."); 25 | } 26 | } 27 | -------------------------------------------------------------------------------- /JDBC/JdbcExampleCreateIndexes.java: -------------------------------------------------------------------------------- 1 | 2 | import java.sql.Connection; 3 | import java.sql.DriverManager; 4 | import java.sql.Statement; 5 | 6 | public class JdbcExampleCreateIndexes { 7 | 8 | public static void main(String[] args) throws Exception { 9 | System.out.println("JDBC example started."); 10 | 11 | // Register JDBC driver 12 | Class.forName("org.apache.ignite.IgniteJdbcDriver"); 13 | 14 | // Open JDBC connection 15 | Connection conn = DriverManager.getConnection( 16 | "jdbc:ignite:thin://127.0.0.1/"); 17 | 18 | // Create indexes 19 | try (Statement stmt = conn.createStatement()) { 20 | 21 | // Create an index on the city table 22 | stmt.executeUpdate("CREATE INDEX idx_city_name ON city (name)"); 23 | 24 | // Create an index on the person table 25 | stmt.executeUpdate("CREATE INDEX idx_person_name ON person (name)"); 26 | } 27 | 28 | System.out.println("Created database indexes."); 29 | } 30 | } 31 | -------------------------------------------------------------------------------- /.idea/misc.xml: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | 6 | 7 | 12 | 13 | 14 | 15 | 16 | 17 | 18 | 19 | 20 | 21 | 22 | 23 | 24 | -------------------------------------------------------------------------------- /sql.script: -------------------------------------------------------------------------------- 1 | CREATE TABLE City ( 2 | id LONG PRIMARY KEY, name VARCHAR) 3 | WITH "template=replicated"; 4 | 5 | CREATE TABLE Person ( 6 | id LONG, name VARCHAR, city_id LONG, PRIMARY KEY (id, city_id)) 7 | WITH "backups=1, affinityKey=city_id"; 8 | 9 | CREATE INDEX idx_city_name ON City (name); 10 | 11 | CREATE INDEX idx_person_name ON Person (name); 12 | 13 | INSERT INTO City (id, name) VALUES (1, 'Forest Hill'); 14 | INSERT INTO City (id, name) VALUES (2, 'Denver'); 15 | INSERT INTO City (id, name) VALUES (3, 'St. Petersburg'); 16 | 17 | INSERT INTO Person (id, name, city_id) VALUES (1, 'John Doe', 3); 18 | INSERT INTO Person (id, name, city_id) VALUES (2, 'Jane Roe', 2); 19 | INSERT INTO Person (id, name, city_id) VALUES (3, 'Mary Major', 1); 20 | INSERT INTO Person (id, name, city_id) VALUES (4, 'Richard Miles', 2); 21 | 22 | SELECT * FROM City; 23 | 24 | SELECT name FROM City WHERE id = 1; 25 | 26 | SELECT p.name, c.name 27 | FROM Person p, City c 28 | WHERE p.city_id = c.id; 29 | 30 | UPDATE City 31 | SET name = 'Foster City' 32 | WHERE id = 2; 33 | 34 | DELETE FROM Person 35 | WHERE name = 'John Doe'; -------------------------------------------------------------------------------- /JDBC/JdbcExampleSelect.java: -------------------------------------------------------------------------------- 1 | import java.sql.Connection; 2 | import java.sql.DriverManager; 3 | import java.sql.ResultSet; 4 | import java.sql.Statement; 5 | 6 | public class JdbcExampleSelect { 7 | 8 | public static void main(String[] args) throws Exception { 9 | System.out.println("JDBC example started."); 10 | 11 | // Register JDBC driver 12 | Class.forName("org.apache.ignite.IgniteJdbcDriver"); 13 | 14 | // Open JDBC connection 15 | Connection conn = DriverManager.getConnection( 16 | "jdbc:ignite:thin://127.0.0.1/"); 17 | 18 | // Get data 19 | try (Statement stmt = conn.createStatement()) { 20 | try (ResultSet rs = 21 | stmt.executeQuery("SELECT p.name, c.name " + 22 | " FROM person p, city c " + 23 | " WHERE p.city_id = c.id")) { 24 | 25 | System.out.println("Query results:"); 26 | 27 | while (rs.next()) 28 | System.out.println(">>> " + 29 | rs.getString(1) + 30 | ", " + 31 | rs.getString(2)); 32 | } 33 | } 34 | } 35 | } 36 | -------------------------------------------------------------------------------- /.idea/compiler.xml: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | 6 | 7 | 8 | 9 | 10 | 11 | 12 | 13 | 14 | 15 | 16 | 17 | 18 | 19 | 20 | 21 | 22 | 23 | 24 | 25 | 26 | 27 | 28 | 29 | 30 | 31 | 32 | -------------------------------------------------------------------------------- /JDBC/JdbcExampleCreateTables.java: -------------------------------------------------------------------------------- 1 | import java.sql.Connection; 2 | import java.sql.DriverManager; 3 | import java.sql.Statement; 4 | 5 | public class JdbcExampleCreateTables { 6 | 7 | public static void main(String[] args) throws Exception { 8 | System.out.println("JDBC example started."); 9 | 10 | // Register JDBC driver 11 | Class.forName("org.apache.ignite.IgniteJdbcDriver"); 12 | 13 | // Open JDBC connection 14 | Connection conn = DriverManager.getConnection( 15 | "jdbc:ignite:thin://127.0.0.1/"); 16 | 17 | // Create database objects 18 | try (Statement stmt = conn.createStatement()) { 19 | 20 | // Create reference City table based on REPLICATED template 21 | stmt.executeUpdate("CREATE TABLE city (" + 22 | " id LONG PRIMARY KEY, name VARCHAR) " + 23 | " WITH \"template=replicated\""); 24 | 25 | // Create table based on PARTITIONED template with one backup 26 | stmt.executeUpdate("CREATE TABLE person (" + 27 | " id LONG, name VARCHAR, city_id LONG, " + 28 | " PRIMARY KEY (id, city_id)) " + 29 | " WITH \"backups=1, affinityKey=city_id\""); 30 | } 31 | 32 | System.out.println("Created database tables."); 33 | } 34 | } 35 | -------------------------------------------------------------------------------- /config/example-ignite.xml: -------------------------------------------------------------------------------- 1 | 2 | 3 | 19 | 20 | 23 | 27 | 28 | 29 | 30 | 31 | 32 | -------------------------------------------------------------------------------- /JDBC/JdbcExampleInsert.java: -------------------------------------------------------------------------------- 1 | import java.sql.Connection; 2 | import java.sql.DriverManager; 3 | import java.sql.PreparedStatement; 4 | 5 | public class JdbcExampleInsert { 6 | 7 | public static void main(String[] args) throws Exception { 8 | System.out.println("JDBC example started."); 9 | 10 | // Register JDBC driver 11 | Class.forName("org.apache.ignite.IgniteJdbcDriver"); 12 | 13 | // Open JDBC connection 14 | Connection conn = DriverManager.getConnection( 15 | "jdbc:ignite:thin://127.0.0.1/"); 16 | 17 | // Populate city table 18 | try (PreparedStatement stmt = 19 | conn.prepareStatement("INSERT INTO city (id, name) VALUES (?, ?)")) { 20 | 21 | stmt.setLong(1, 1L); 22 | stmt.setString(2, "Forest Hill"); 23 | stmt.executeUpdate(); 24 | 25 | stmt.setLong(1, 2L); 26 | stmt.setString(2, "Denver"); 27 | stmt.executeUpdate(); 28 | 29 | stmt.setLong(1, 3L); 30 | stmt.setString(2, "St. Petersburg"); 31 | stmt.executeUpdate(); 32 | } 33 | 34 | // Populate person table 35 | try (PreparedStatement stmt = 36 | conn.prepareStatement("INSERT INTO person (id, name, city_id) values (?, ?, ?)")) { 37 | 38 | stmt.setLong(1, 1L); 39 | stmt.setString(2, "John Doe"); 40 | stmt.setLong(3, 3L); 41 | stmt.executeUpdate(); 42 | 43 | stmt.setLong(1, 2L); 44 | stmt.setString(2, "Jane Roe"); 45 | stmt.setLong(3, 2L); 46 | stmt.executeUpdate(); 47 | 48 | stmt.setLong(1, 3L); 49 | stmt.setString(2, "Mary Major"); 50 | stmt.setLong(3, 1L); 51 | stmt.executeUpdate(); 52 | 53 | stmt.setLong(1, 4L); 54 | stmt.setString(2, "Richard Miles"); 55 | stmt.setLong(3, 2L); 56 | stmt.executeUpdate(); 57 | } 58 | 59 | System.out.println("Populated data."); 60 | } 61 | } 62 | -------------------------------------------------------------------------------- /pom.xml: -------------------------------------------------------------------------------- 1 | 2 | 3 | 19 | 20 | 23 | 27 | 4.0.0 28 | 29 | org.apache.ignite 30 | ignite-gettings-started 31 | 1.0.0 32 | 33 | 34 | 35 | javax.cache 36 | cache-api 37 | 1.0.0 38 | 39 | 40 | 41 | org.apache.ignite 42 | ignite-core 43 | 2.1.0 44 | 45 | 46 | 47 | org.apache.ignite 48 | ignite-spring 49 | 2.1.0 50 | 51 | 52 | 53 | org.apache.ignite 54 | ignite-indexing 55 | 2.1.0 56 | 57 | 58 | 59 | 60 | 61 | -------------------------------------------------------------------------------- /.idea/Ignite-SQL-Getting-Started.iml: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | 6 | 7 | 8 | 9 | 10 | 11 | 12 | 13 | 14 | 15 | 16 | 17 | 18 | 19 | 20 | 21 | 22 | 23 | 24 | 25 | 26 | 27 | 28 | 29 | 30 | 31 | 32 | 33 | 34 | 35 | -------------------------------------------------------------------------------- /config/example-default.xml: -------------------------------------------------------------------------------- 1 | 2 | 3 | 19 | 20 | 23 | 31 | 32 | 33 | 34 | 35 | 36 | 37 | 38 | 39 | 40 | 41 | 42 | 43 | 44 | 45 | 46 | 47 | 48 | 49 | 50 | 51 | 52 | 53 | 54 | 55 | 56 | 57 | 62 | 63 | 64 | 65 | 66 | 67 | 68 | 127.0.0.1:47500..47509 69 | 70 | 71 | 72 | 73 | 74 | 75 | 76 | 77 | -------------------------------------------------------------------------------- /java/main/org/apache/ignite/gettingstarted/JavaSample.java: -------------------------------------------------------------------------------- 1 | package org.apache.ignite.gettingstarted; 2 | 3 | import java.util.Iterator; 4 | import java.util.List; 5 | import org.apache.ignite.Ignite; 6 | import org.apache.ignite.IgniteCache; 7 | import org.apache.ignite.Ignition; 8 | import org.apache.ignite.cache.CachePeekMode; 9 | import org.apache.ignite.cache.affinity.AffinityKeyMapped; 10 | import org.apache.ignite.cache.query.FieldsQueryCursor; 11 | import org.apache.ignite.cache.query.SqlFieldsQuery; 12 | 13 | /** 14 | * Do the following prior running this example: 15 | * 16 | *
  • 17 | * Start apache ignite cluster node using {apache_ignite_version}/bin/ignite.sh (bat) file. 18 | *
  • 19 | *
  • 20 | * Create SQL schema and indexes executing {@link JdbcExampleCreateTables} and {@link JdbcExampleCreateIndexes} 21 | * sources. 22 | *
  • 23 | *
    24 | * 25 | * Run this example after that to see how to execute DML statements from Java side. 26 | */ 27 | public class JavaSample { 28 | /** 29 | * Executes the example. 30 | * @param args 31 | */ 32 | public static void main(String[] args) { 33 | Ignition.setClientMode(true); 34 | 35 | try (Ignite ignite = Ignition.start()) { 36 | 37 | System.out.println(); 38 | System.out.println(">>> Java Sample is started."); 39 | 40 | // Getting a reference to City and Person caches created by 41 | // {@link JdbcExampleCreateTables} and {@link JdbcExampleCreateIndexes} 42 | IgniteCache cityCache = ignite.cache("SQL_PUBLIC_CITY"); 43 | 44 | IgniteCache personCache = ignite.cache("SQL_PUBLIC_PERSON"); 45 | 46 | insertData(cityCache, personCache); 47 | 48 | queryData(cityCache); 49 | 50 | updateData(cityCache); 51 | 52 | removeData(personCache); 53 | 54 | queryData(cityCache); 55 | } 56 | } 57 | 58 | /** 59 | * Putting data into the cluster. 60 | * 61 | * @param cityCache City cache. 62 | * @param personCache Person cache. 63 | */ 64 | private static void insertData(IgniteCache cityCache, IgniteCache personCache) { 65 | // Make sure the cache is empty. 66 | cityCache.clear(); 67 | 68 | // Inserting entries into City. 69 | SqlFieldsQuery query = new SqlFieldsQuery("INSERT INTO City (id, name) VALUES (?, ?)"); 70 | 71 | cityCache.query(query.setArgs(1, "Forest Hill")).getAll(); 72 | cityCache.query(query.setArgs(2, "Denver")).getAll(); 73 | cityCache.query(query.setArgs(3, "St. Petersburg")).getAll(); 74 | 75 | System.out.println(">>> Inserted entries into City:" + cityCache.size(CachePeekMode.PRIMARY)); 76 | 77 | // Make sure the cache is empty. 78 | personCache.clear(); 79 | 80 | // Inserting entries into Person. 81 | query = new SqlFieldsQuery("INSERT INTO Person (id, name, city_id) VALUES (?, ?, ?)"); 82 | 83 | personCache.query(query.setArgs(1, "John Doe", 3)).getAll(); 84 | personCache.query(query.setArgs(2, "Jane Roe", 2)).getAll(); 85 | personCache.query(query.setArgs(3, "Mary Major", 1)).getAll(); 86 | personCache.query(query.setArgs(4, "Richard Miles", 2)).getAll(); 87 | 88 | System.out.println(">>> Inserted entries into Person:" + personCache.size(CachePeekMode.PRIMARY)); 89 | } 90 | 91 | /** 92 | * Query data from the cluster. 93 | * 94 | * @param cityCache City cache. 95 | */ 96 | private static void queryData(IgniteCache cityCache) { 97 | // Querying data from the cluster using a distributed JOIN. 98 | SqlFieldsQuery query = new SqlFieldsQuery("SELECT p.name, c.name " + 99 | " FROM Person p, City c WHERE p.city_id = c.id"); 100 | 101 | FieldsQueryCursor> cursor = cityCache.query(query); 102 | 103 | Iterator> iterator = cursor.iterator(); 104 | 105 | System.out.println("Query result:"); 106 | 107 | while (iterator.hasNext()) { 108 | List row = iterator.next(); 109 | 110 | System.out.println(">>> " + row.get(0) + ", " + row.get(1)); 111 | } 112 | } 113 | 114 | /** 115 | * Updating data in the cluster. 116 | * 117 | * @param cityCache City cache. 118 | */ 119 | private static void updateData(IgniteCache cityCache) { 120 | // Updating a city entry. 121 | SqlFieldsQuery query = new SqlFieldsQuery("UPDATE City SET name = 'Foster City' WHERE id = 2"); 122 | 123 | cityCache.query(query).getAll(); 124 | } 125 | 126 | /** 127 | * Remove data from the cluster. 128 | * 129 | * @param personCache Person cache. 130 | */ 131 | private static void removeData(IgniteCache personCache) { 132 | // Removing a person. 133 | SqlFieldsQuery query = new SqlFieldsQuery("DELETE FROM Person WHERE name = 'John Doe'"); 134 | 135 | personCache.query(query).getAll(); 136 | } 137 | } 138 | --------------------------------------------------------------------------------