From 3ae3ed10e5199a9c755ed5b7c46387ceccfce127 Mon Sep 17 00:00:00 2001 From: nikolay_tikhonov Date: Fri, 6 Feb 2015 11:08:33 +0300 Subject: [PATCH] IGNITE-190 Changed query examples. --- examples/config/example-cache.xml | 2 +- .../examples/datagrid/CacheQueryExample.java | 180 ++++++------------ .../starschema/CacheStarSchemaExample.java | 43 ++--- 3 files changed, 76 insertions(+), 149 deletions(-) diff --git a/examples/config/example-cache.xml b/examples/config/example-cache.xml index 38ea60d36fb48..54f926a620ff7 100644 --- a/examples/config/example-cache.xml +++ b/examples/config/example-cache.xml @@ -141,7 +141,7 @@ - + diff --git a/examples/src/main/java/org/apache/ignite/examples/datagrid/CacheQueryExample.java b/examples/src/main/java/org/apache/ignite/examples/datagrid/CacheQueryExample.java index fe5bf7fd0dbe9..72a20ff946fb8 100644 --- a/examples/src/main/java/org/apache/ignite/examples/datagrid/CacheQueryExample.java +++ b/examples/src/main/java/org/apache/ignite/examples/datagrid/CacheQueryExample.java @@ -18,12 +18,11 @@ package org.apache.ignite.examples.datagrid; import org.apache.ignite.*; -import org.apache.ignite.cache.*; import org.apache.ignite.cache.affinity.*; +import org.apache.ignite.cache.query.*; import org.apache.ignite.cache.query.annotations.*; -import org.apache.ignite.internal.processors.cache.query.*; -import org.apache.ignite.lang.*; +import javax.cache.*; import java.io.*; import java.util.*; @@ -81,7 +80,7 @@ public static void main(String[] args) throws Exception { System.out.println(">>> Cache query example started."); // Clean up caches on all nodes before run. - ignite.jcache(CACHE_NAME).clear(); + ignite.jcache(CACHE_NAME).removeAll(); // Populate cache. initialize(); @@ -95,13 +94,8 @@ public static void main(String[] args) throws Exception { // Example for TEXT-based querying for a given string in peoples resumes. textQuery(); - // Example for SQL-based querying with custom remote and local reducers - // to calculate average salary among all employees within a company. - sqlQueryWithReducers(); - - // Example for SQL-based querying with custom remote transformer to make sure - // that only required data without any overhead is returned to caller. - sqlQueryWithTransformer(); + // Example for SQL-based querying to calculate average salary among all employees within a company. + sqlQueryWithAggregation(); // Example for SQL-based fields queries that return only required // fields instead of whole key-value pairs. @@ -120,18 +114,20 @@ public static void main(String[] args) throws Exception { * @throws IgniteCheckedException In case of error. */ private static void sqlQuery() throws IgniteCheckedException { - GridCache, Person> cache = Ignition.ignite().cache(CACHE_NAME); + IgniteCache, Person> cache = Ignition.ignite().jcache(CACHE_NAME); - // Create query which selects salaries based on range. - CacheQuery, Person>> qry = - cache.queries().createSqlQuery(Person.class, "salary > ? and salary <= ?"); + // SQL clause which selects salaries based on range. + String sql = "salary > ? and salary <= ?"; // Execute queries for salary ranges. - print("People with salaries between 0 and 1000: ", qry.execute(0, 1000).get()); + print("People with salaries between 0 and 1000: ", + cache.query(new QuerySqlPredicate(Person.class, sql, 0, 1000)).getAll()); - print("People with salaries between 1000 and 2000: ", qry.execute(1000, 2000).get()); + print("People with salaries between 1000 and 2000: ", + cache.query(new QuerySqlPredicate(Person.class, sql, 1000, 2000)).getAll()); - print("People with salaries greater than 2000: ", qry.execute(2000, Integer.MAX_VALUE).get()); + print("People with salaries greater than 2000: ", + cache.query(new QuerySqlPredicate(Person.class, sql, 2000, Integer.MAX_VALUE)).getAll()); } /** @@ -140,17 +136,19 @@ private static void sqlQuery() throws IgniteCheckedException { * @throws IgniteCheckedException In case of error. */ private static void sqlQueryWithJoin() throws IgniteCheckedException { - GridCache, Person> cache = Ignition.ignite().cache(CACHE_NAME); + IgniteCache, Person> cache = Ignition.ignite().jcache(CACHE_NAME); - // Create query which joins on 2 types to select people for a specific organization. - CacheQuery, Person>> qry = - cache.queries().createSqlQuery(Person.class, "from Person, Organization " + - "where Person.orgId = Organization.id " + - "and lower(Organization.name) = lower(?)"); + // SQL clause query which joins on 2 types to select people for a specific organization. + String joinSql = + "from Person, Organization " + + "where Person.orgId = Organization.id " + + "and lower(Organization.name) = lower(?)"; // Execute queries for find employees for different organizations. - print("Following people are 'Ignite' employees: ", qry.execute("Ignite").get()); - print("Following people are 'Other' employees: ", qry.execute("Other").get()); + print("Following people are 'Ignite' employees: ", + cache.query(new QuerySqlPredicate(Person.class, joinSql, "Ignite")).getAll()); + print("Following people are 'Other' employees: ", + cache.query(new QuerySqlPredicate(Person.class, joinSql, "Other")).getAll()); } /** @@ -159,96 +157,35 @@ private static void sqlQueryWithJoin() throws IgniteCheckedException { * @throws IgniteCheckedException In case of error. */ private static void textQuery() throws IgniteCheckedException { - GridCache, Person> cache = Ignition.ignite().cache(CACHE_NAME); + IgniteCache, Person> cache = Ignition.ignite().jcache(CACHE_NAME); // Query for all people with "Master Degree" in their resumes. - CacheQuery, Person>> masters = - cache.queries().createFullTextQuery(Person.class, "Master"); + QueryCursor, Person>> masters = + cache.query(new QueryTextPredicate(Person.class, "Master")); - // Query for all people with "Bachelor Degree"in their resumes. - CacheQuery, Person>> bachelors = - cache.queries().createFullTextQuery(Person.class, "Bachelor"); + // Query for all people with "Bachelor Degree" in their resumes. + QueryCursor, Person>> bachelors = + cache.query(new QueryTextPredicate(Person.class, "Bachelor")); - print("Following people have 'Master Degree' in their resumes: ", masters.execute().get()); - print("Following people have 'Bachelor Degree' in their resumes: ", bachelors.execute().get()); + print("Following people have 'Master Degree' in their resumes: ", masters.getAll()); + print("Following people have 'Bachelor Degree' in their resumes: ", bachelors.getAll()); } /** - * Example for SQL queries with custom remote and local reducers to calculate - * average salary for a specific organization. + * Example for SQL queries to calculate average salary for a specific organization. * * @throws IgniteCheckedException In case of error. */ - private static void sqlQueryWithReducers() throws IgniteCheckedException { - CacheProjection, Person> cache = Ignition.ignite().cache(CACHE_NAME); + private static void sqlQueryWithAggregation() throws IgniteCheckedException { + IgniteCache, Person> cache = Ignition.ignite().jcache(CACHE_NAME); // Calculate average of salary of all persons in Ignite. - CacheQuery, Person>> qry = cache.queries().createSqlQuery( - Person.class, - "from Person, Organization where Person.orgId = Organization.id and " + - "lower(Organization.name) = lower(?)"); - - Collection> res = qry.execute( - new IgniteReducer, Person>, IgniteBiTuple>() { - private double sum; - - private int cnt; - - @Override public boolean collect(Map.Entry, Person> e) { - sum += e.getValue().salary; - - cnt++; - - // Continue collecting. - return true; - } - - @Override public IgniteBiTuple reduce() { - return new IgniteBiTuple<>(sum, cnt); - } - }, "Ignite").get(); - - double sum = 0.0d; - int cnt = 0; - - for (IgniteBiTuple t : res) { - sum += t.get1(); - cnt += t.get2(); - } - - double avg = sum / cnt; + QueryCursor> qry = cache.queryFields(new QuerySqlPredicate( + "select avg(salary) from Person, Organization where Person.orgId = Organization.id and " + + "lower(Organization.name) = lower(?)", "Ignite")); // Calculate average salary for a specific organization. - print("Average salary for 'Ignite' employees: " + avg); - } - - /** - * Example for SQL queries with custom transformer to allow passing - * only the required set of fields back to caller. - * - * @throws IgniteCheckedException In case of error. - */ - private static void sqlQueryWithTransformer() throws IgniteCheckedException { - GridCache, Person> cache = Ignition.ignite().cache(CACHE_NAME); - - // Create query to get names of all employees working for some company. - CacheQuery, Person>> qry = - cache.queries().createSqlQuery(Person.class, - "from Person, Organization " + - "where Person.orgId = Organization.id and lower(Organization.name) = lower(?)"); - - // Transformer to convert Person objects to String. - // Since caller only needs employee names, we only - // send names back. - IgniteClosure, Person>, String> trans = - new IgniteClosure, Person>, String>() { - @Override public String apply(Map.Entry, Person> e) { - return e.getValue().lastName; - } - }; - - // Query all nodes for names of all Ignite employees. - print("Names of all 'Ignite' employees: " + qry.execute(trans, "Ignite").get()); + print("Average salary for 'Ignite' employees: " + qry.getAll()); } /** @@ -258,15 +195,15 @@ private static void sqlQueryWithTransformer() throws IgniteCheckedException { * @throws IgniteCheckedException In case of error. */ private static void sqlFieldsQuery() throws IgniteCheckedException { - GridCache cache = Ignition.ignite().cache(CACHE_NAME); + IgniteCache cache = Ignition.ignite().jcache(CACHE_NAME); // Create query to get names of all employees. - CacheQuery> qry1 = cache.queries().createSqlFieldsQuery( - "select concat(firstName, ' ', lastName) from Person"); + QueryCursor> qry = cache.queryFields( + new QuerySqlPredicate("select concat(firstName, ' ', lastName) from Person")); // Execute query to get collection of rows. In this particular // case each row will have one element with full name of an employees. - Collection> res = qry1.execute().get(); + Collection> res = qry.getAll(); // Print names. print("Names of all employees:", res); @@ -279,16 +216,16 @@ private static void sqlFieldsQuery() throws IgniteCheckedException { * @throws IgniteCheckedException In case of error. */ private static void sqlFieldsQueryWithJoin() throws IgniteCheckedException { - GridCache cache = Ignition.ignite().cache(CACHE_NAME); + IgniteCache cache = Ignition.ignite().jcache(CACHE_NAME); // Create query to get names of all employees. - CacheQuery> qry1 = cache.queries().createSqlFieldsQuery( - "select concat(firstName, ' ', lastName), Organization.name from Person, Organization where " + - "Person.orgId = Organization.id"); + QueryCursor> qry = cache.queryFields(new QuerySqlPredicate("select concat(firstName, ' ', lastName), " + + "Organization.name from Person, Organization where " + + "Person.orgId = Organization.id")); // Execute query to get collection of rows. In this particular // case each row will have one element with full name of an employees. - Collection> res = qry1.execute().get(); + Collection> res = qry.getAll(); // Print persons' names and organizations' names. print("Names of all employees and organizations they belong to:", res); @@ -301,14 +238,7 @@ private static void sqlFieldsQueryWithJoin() throws IgniteCheckedException { * @throws InterruptedException In case of error. */ private static void initialize() throws IgniteCheckedException, InterruptedException { - GridCache cache = Ignition.ignite().cache(CACHE_NAME); - - // Organization projection. - CacheProjection orgCache = cache.projection(UUID.class, Organization.class); - - // Person projection. - CacheProjection, Person> personCache = - cache.projection(CacheAffinityKey.class, Person.class); + IgniteCache cache = Ignition.ignite().jcache(CACHE_NAME); // Organizations. Organization org1 = new Organization("Ignite"); @@ -320,15 +250,15 @@ private static void initialize() throws IgniteCheckedException, InterruptedExcep Person p3 = new Person(org2, "John", "Smith", 1000, "John Smith has Bachelor Degree."); Person p4 = new Person(org2, "Jane", "Smith", 2000, "Jane Smith has Master Degree."); - orgCache.put(org1.id, org1); - orgCache.put(org2.id, org2); + cache.put(org1.id, org1); + cache.put(org2.id, org2); // Note that in this example we use custom affinity key for Person objects // to ensure that all persons are collocated with their organizations. - personCache.put(p1.key(), p1); - personCache.put(p2.key(), p2); - personCache.put(p3.key(), p3); - personCache.put(p4.key(), p4); + cache.put(p1.key(), p1); + cache.put(p2.key(), p2); + cache.put(p3.key(), p3); + cache.put(p4.key(), p4); // Wait 1 second to be sure that all nodes processed put requests. Thread.sleep(1000); diff --git a/examples/src/main/java/org/apache/ignite/examples/datagrid/starschema/CacheStarSchemaExample.java b/examples/src/main/java/org/apache/ignite/examples/datagrid/starschema/CacheStarSchemaExample.java index a5212d2cdbeb1..bedbe34d20d0f 100644 --- a/examples/src/main/java/org/apache/ignite/examples/datagrid/starschema/CacheStarSchemaExample.java +++ b/examples/src/main/java/org/apache/ignite/examples/datagrid/starschema/CacheStarSchemaExample.java @@ -19,9 +19,10 @@ import org.apache.ignite.*; import org.apache.ignite.cache.*; +import org.apache.ignite.cache.query.*; import org.apache.ignite.examples.datagrid.*; -import org.apache.ignite.internal.processors.cache.query.*; +import javax.cache.*; import java.util.*; import java.util.concurrent.*; @@ -148,19 +149,19 @@ private static void populateFacts() throws IgniteException { * @throws IgniteException If failed. */ private static void queryStorePurchases() throws IgniteCheckedException { - GridCache factCache = Ignition.ignite().cache(PARTITIONED_CACHE_NAME); + IgniteCache factCache = Ignition.ignite().jcache(PARTITIONED_CACHE_NAME); // All purchases for store1. // ======================== // Create cross cache query to get all purchases made at store1. - CacheQuery> storePurchases = factCache.queries().createSqlQuery( - FactPurchase.class, - "from \"replicated\".DimStore, \"partitioned\".FactPurchase " + - "where DimStore.id=FactPurchase.storeId and DimStore.name=?"); + QueryCursor> storePurchases = factCache.query(new QuerySqlPredicate( + FactPurchase.class, + "from \"replicated\".DimStore, \"partitioned\".FactPurchase " + + "where DimStore.id=FactPurchase.storeId and DimStore.name=?", + "Store1")); - printQueryResults("All purchases made at store1:", - storePurchases.execute("Store1").get()); + printQueryResults("All purchases made at store1:", storePurchases.getAll()); } /** @@ -172,30 +173,26 @@ private static void queryStorePurchases() throws IgniteCheckedException { * @throws IgniteException If failed. */ private static void queryProductPurchases() throws IgniteCheckedException { - GridCache dimCache = Ignition.ignite().cache(REPLICATED_CACHE_NAME); - GridCache factCache = Ignition.ignite().cache(PARTITIONED_CACHE_NAME); - - CacheProjection prods = dimCache.projection(Integer.class, DimProduct.class); + IgniteCache factCache = Ignition.ignite().jcache(PARTITIONED_CACHE_NAME); // All purchases for certain product made at store2. // ================================================= - DimProduct p1 = rand(prods.values()); - DimProduct p2 = rand(prods.values()); - DimProduct p3 = rand(prods.values()); + DimProduct p1 = rand(dataProduct.values()); + DimProduct p2 = rand(dataProduct.values()); + DimProduct p3 = rand(dataProduct.values()); System.out.println("IDs of products [p1=" + p1.getId() + ", p2=" + p2.getId() + ", p3=" + p3.getId() + ']'); // Create cross cache query to get all purchases made at store2 // for specified products. - CacheQuery> prodPurchases = factCache.queries().createSqlQuery( + QueryCursor> prodPurchases = factCache.query(new QuerySqlPredicate( FactPurchase.class, - "from \"replicated\".DimStore, \"replicated\".DimProduct, \"partitioned\".FactPurchase " + - "where DimStore.id=FactPurchase.storeId and DimProduct.id=FactPurchase.productId " + - "and DimStore.name=? and DimProduct.id in(?, ?, ?)"); + "from \"replicated\".DimStore, \"replicated\".DimProduct, \"partitioned\".FactPurchase " + + "where DimStore.id=FactPurchase.storeId and DimProduct.id=FactPurchase.productId " + + "and DimStore.name=? and DimProduct.id in(?, ?, ?)", "Store2", p1.getId(), p2.getId(), p3.getId())); - printQueryResults("All purchases made at store2 for 3 specific products:", - prodPurchases.execute("Store2", p1.getId(), p2.getId(), p3.getId()).get()); + printQueryResults("All purchases made at store2 for 3 specific products:", prodPurchases.getAll()); } /** @@ -204,10 +201,10 @@ private static void queryProductPurchases() throws IgniteCheckedException { * @param msg Initial message. * @param res Results to print. */ - private static void printQueryResults(String msg, Iterable> res) { + private static void printQueryResults(String msg, Iterable> res) { System.out.println(msg); - for (Map.Entry e : res) + for (Cache.Entry e : res) System.out.println(" " + e.getValue().toString()); }