From e1ab893540e069e25c59d2c909a33f4fd0cee1cb Mon Sep 17 00:00:00 2001 From: tledkov Date: Thu, 4 Apr 2019 11:28:56 +0300 Subject: [PATCH] IGNITE-11334: SQL: Deprecated SqlQuery in Java. This closes #6151. --- .../CacheClientBinaryQueryExample.java | 133 ++++++------------ .../examples/datagrid/CacheQueryExample.java | 3 +- .../starschema/CacheStarSchemaExample.java | 25 ++-- .../examples/sql/SqlQueriesExample.java | 29 ++-- .../java/org/apache/ignite/IgniteCache.java | 2 - .../org/apache/ignite/cache/query/Query.java | 2 - .../ignite/cache/query/SqlFieldsQuery.java | 3 +- .../apache/ignite/cache/query/SqlQuery.java | 3 + .../org/apache/ignite/client/ClientCache.java | 3 +- .../internal/jdbc/thin/JdbcThinStatement.java | 4 +- 10 files changed, 78 insertions(+), 129 deletions(-) diff --git a/examples/src/main/java/org/apache/ignite/examples/binary/datagrid/CacheClientBinaryQueryExample.java b/examples/src/main/java/org/apache/ignite/examples/binary/datagrid/CacheClientBinaryQueryExample.java index a3c99962811a9..0c9becc32a16d 100644 --- a/examples/src/main/java/org/apache/ignite/examples/binary/datagrid/CacheClientBinaryQueryExample.java +++ b/examples/src/main/java/org/apache/ignite/examples/binary/datagrid/CacheClientBinaryQueryExample.java @@ -19,12 +19,13 @@ import java.sql.Timestamp; import java.util.Arrays; -import java.util.LinkedHashMap; +import java.util.Collections; import java.util.List; import javax.cache.Cache; import org.apache.ignite.Ignite; import org.apache.ignite.IgniteCache; import org.apache.ignite.Ignition; +import org.apache.ignite.binary.BinaryObject; import org.apache.ignite.cache.CacheKeyConfiguration; import org.apache.ignite.cache.CacheMode; import org.apache.ignite.cache.QueryEntity; @@ -32,7 +33,6 @@ import org.apache.ignite.cache.QueryIndexType; import org.apache.ignite.cache.query.QueryCursor; import org.apache.ignite.cache.query.SqlFieldsQuery; -import org.apache.ignite.cache.query.SqlQuery; import org.apache.ignite.cache.query.TextQuery; import org.apache.ignite.configuration.CacheConfiguration; import org.apache.ignite.examples.model.Address; @@ -40,17 +40,16 @@ import org.apache.ignite.examples.model.EmployeeKey; import org.apache.ignite.examples.model.Organization; import org.apache.ignite.examples.model.OrganizationType; -import org.apache.ignite.binary.BinaryObject; /** - * This example demonstrates use of binary objects with cache queries. - * The example populates cache with sample data and runs several SQL and full text queries over this data. + * This example demonstrates use of binary objects with cache queries. The example populates cache with sample data and + * runs several SQL and full text queries over this data. *

- * Remote nodes should always be started with the following command: - * {@code 'ignite.{sh|bat} examples/config/example-ignite.xml'} + * Remote nodes should always be started with the following command: {@code 'ignite.{sh|bat} + * examples/config/example-ignite.xml'} *

- * Alternatively you can run {@link org.apache.ignite.examples.ExampleNodeStartup} in another JVM which will - * start a node with {@code examples/config/example-ignite.xml} configuration. + * Alternatively you can run {@link org.apache.ignite.examples.ExampleNodeStartup} in another JVM which will start a + * node with {@code examples/config/example-ignite.xml} configuration. */ public class CacheClientBinaryQueryExample { /** Organization cache name. */ @@ -106,15 +105,12 @@ public static void main(String[] args) { // Get cache that will work with binary objects. IgniteCache binaryCache = employeeCache.withKeepBinary(); - // Run SQL query example. - sqlQuery(binaryCache); + // Run SQL fields query example. + sqlFieldsQuery(binaryCache); // Run SQL query with join example. sqlJoinQuery(binaryCache); - // Run SQL fields query example. - sqlFieldsQuery(binaryCache); - // Run full text query example. textQuery(binaryCache); @@ -134,30 +130,21 @@ public static void main(String[] args) { * @return Cache type metadata. */ private static QueryEntity createEmployeeQueryEntity() { - QueryEntity employeeEntity = new QueryEntity(); - - employeeEntity.setValueType(Employee.class.getName()); - employeeEntity.setKeyType(EmployeeKey.class.getName()); - - LinkedHashMap fields = new LinkedHashMap<>(); - - fields.put("name", String.class.getName()); - fields.put("salary", Long.class.getName()); - fields.put("addr.zip", Integer.class.getName()); - fields.put("organizationId", Integer.class.getName()); - fields.put("addr.street", Integer.class.getName()); - - employeeEntity.setFields(fields); - - employeeEntity.setIndexes(Arrays.asList( - new QueryIndex("name"), - new QueryIndex("salary"), - new QueryIndex("addr.zip"), - new QueryIndex("organizationId"), - new QueryIndex("addr.street", QueryIndexType.FULLTEXT) - )); - - return employeeEntity; + return new QueryEntity() + .setValueType(Employee.class.getName()) + .setKeyType(EmployeeKey.class.getName()) + .addQueryField("organizationId", Integer.class.getName(), null) + .addQueryField("name", String.class.getName(), null) + .addQueryField("salary", Long.class.getName(), null) + .addQueryField("addr.zip", Integer.class.getName(), null) + .addQueryField("addr.street", String.class.getName(), null) + .setKeyFields(Collections.singleton("organizationId")) + .setIndexes(Arrays.asList( + new QueryIndex("name"), + new QueryIndex("salary"), + new QueryIndex("addr.zip"), + new QueryIndex("organizationId"), + new QueryIndex("addr.street", QueryIndexType.FULLTEXT))); } /** @@ -166,42 +153,32 @@ private static QueryEntity createEmployeeQueryEntity() { * @return Cache type metadata. */ private static QueryEntity createOrganizationQueryEntity() { - QueryEntity organizationEntity = new QueryEntity(); - - organizationEntity.setValueType(Organization.class.getName()); - organizationEntity.setKeyType(Integer.class.getName()); - - LinkedHashMap fields = new LinkedHashMap<>(); - - fields.put("name", String.class.getName()); - fields.put("address.street", String.class.getName()); - - organizationEntity.setFields(fields); - - organizationEntity.setIndexes(Arrays.asList( - new QueryIndex("name") - )); - - return organizationEntity; + return new QueryEntity() + .setValueType(Organization.class.getName()) + .setKeyType(Integer.class.getName()) + .addQueryField("keyId", Integer.class.getName(), null) + .addQueryField("name", String.class.getName(), null) + .addQueryField("address.street", String.class.getName(), null) + .setKeyFieldName("keyId") + .setIndexes(Arrays.asList( + new QueryIndex("name"))); } /** - * Queries employees that have provided ZIP code in address. + * Queries names and salaries for all employees. * * @param cache Ignite cache. */ - private static void sqlQuery(IgniteCache cache) { - SqlQuery query = new SqlQuery<>(Employee.class, "zip = ?"); - - int zip = 94109; + private static void sqlFieldsQuery(IgniteCache cache) { + SqlFieldsQuery qry = new SqlFieldsQuery("select name, salary from Employee"); - QueryCursor> employees = cache.query(query.setArgs(zip)); + QueryCursor> employees = cache.query(qry); System.out.println(); - System.out.println(">>> Employees with zip " + zip + ':'); + System.out.println(">>> Employee names and their salaries:"); - for (Cache.Entry e : employees.getAll()) - System.out.println(">>> " + e.getValue().deserialize()); + for (List row : employees.getAll()) + System.out.println(">>> [Name=" + row.get(0) + ", salary=" + row.get(1) + ']'); } /** @@ -210,37 +187,19 @@ private static void sqlQuery(IgniteCache cache) { * @param cache Ignite cache. */ private static void sqlJoinQuery(IgniteCache cache) { - SqlQuery qry = new SqlQuery<>(Employee.class, - "from Employee, \"" + ORGANIZATION_CACHE_NAME + "\".Organization as org " + - "where Employee.organizationId = org._key and org.name = ?"); + SqlFieldsQuery qry = new SqlFieldsQuery( + "select e.* from Employee e, \"" + ORGANIZATION_CACHE_NAME + "\".Organization as org " + + "where e.organizationId = org.keyId and org.name = ?"); String organizationName = "GridGain"; - QueryCursor> employees = - cache.query(qry.setArgs(organizationName)); + QueryCursor> employees = cache.query(qry.setArgs(organizationName)); System.out.println(); System.out.println(">>> Employees working for " + organizationName + ':'); - for (Cache.Entry e : employees.getAll()) - System.out.println(">>> " + e.getValue()); - } - - /** - * Queries names and salaries for all employees. - * - * @param cache Ignite cache. - */ - private static void sqlFieldsQuery(IgniteCache cache) { - SqlFieldsQuery qry = new SqlFieldsQuery("select name, salary from Employee"); - - QueryCursor> employees = cache.query(qry); - - System.out.println(); - System.out.println(">>> Employee names and their salaries:"); - for (List row : employees.getAll()) - System.out.println(">>> [Name=" + row.get(0) + ", salary=" + row.get(1) + ']'); + System.out.println(">>> " + row); } /** 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 7a1d25fcb62c7..86e866e0823a6 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 @@ -27,7 +27,6 @@ import org.apache.ignite.cache.query.QueryCursor; import org.apache.ignite.cache.query.ScanQuery; import org.apache.ignite.cache.query.SqlFieldsQuery; -import org.apache.ignite.cache.query.SqlQuery; import org.apache.ignite.cache.query.TextQuery; import org.apache.ignite.configuration.CacheConfiguration; import org.apache.ignite.examples.ExampleNodeStartup; @@ -49,7 +48,7 @@ * collocated mode. Refer to {@link AffinityKey} javadoc for more details. *

* To use distributed joins it is necessary to set query 'distributedJoin' flag using - * {@link SqlFieldsQuery#setDistributedJoins(boolean)} or {@link SqlQuery#setDistributedJoins(boolean)}. + * {@link SqlFieldsQuery#setDistributedJoins(boolean)}. * *

  • * Note that if you created query on to replicated cache, all data will 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 6a53b893e9c8f..379701724d3b2 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 @@ -20,6 +20,7 @@ import java.util.Collection; import java.util.ConcurrentModificationException; import java.util.HashMap; +import java.util.List; import java.util.Map; import java.util.concurrent.ThreadLocalRandom; import javax.cache.Cache; @@ -29,7 +30,7 @@ import org.apache.ignite.Ignition; import org.apache.ignite.cache.CacheMode; import org.apache.ignite.cache.query.QueryCursor; -import org.apache.ignite.cache.query.SqlQuery; +import org.apache.ignite.cache.query.SqlFieldsQuery; import org.apache.ignite.configuration.CacheConfiguration; import org.apache.ignite.examples.ExampleNodeStartup; @@ -176,10 +177,9 @@ private static void queryStorePurchases() { // ======================== // Create cross cache query to get all purchases made at store1. - QueryCursor> storePurchases = factCache.query(new SqlQuery( - FactPurchase.class, - "from \"" + DIM_STORE_CACHE_NAME + "\".DimStore, \"" + FACT_CACHE_NAME + "\".FactPurchase " - + "where DimStore.id=FactPurchase.storeId and DimStore.name=?").setArgs("Store1")); + QueryCursor> storePurchases = factCache.query(new SqlFieldsQuery( + "select fp.* from \"" + DIM_STORE_CACHE_NAME + "\".DimStore, \"" + FACT_CACHE_NAME + "\".FactPurchase as fp " + + "where DimStore.id=fp.storeId and DimStore.name=?").setArgs("Store1")); printQueryResults("All purchases made at store1:", storePurchases.getAll()); } @@ -206,11 +206,10 @@ private static void queryProductPurchases() { // Create cross cache query to get all purchases made at store2 // for specified products. - QueryCursor> prodPurchases = factCache.query(new SqlQuery( - FactPurchase.class, - "from \"" + DIM_STORE_CACHE_NAME + "\".DimStore, \"" + DIM_PROD_CACHE_NAME + "\".DimProduct, " + - "\"" + FACT_CACHE_NAME + "\".FactPurchase " - + "where DimStore.id=FactPurchase.storeId and DimProduct.id=FactPurchase.productId " + QueryCursor> prodPurchases = factCache.query(new SqlFieldsQuery( + "select fp.* from \"" + DIM_STORE_CACHE_NAME + "\".DimStore, \"" + DIM_PROD_CACHE_NAME + "\".DimProduct, " + + "\"" + FACT_CACHE_NAME + "\".FactPurchase as fp " + + "where DimStore.id=fp.storeId and DimProduct.id=fp.productId " + "and DimStore.name=? and DimProduct.id in(?, ?, ?)") .setArgs("Store2", p1.getId(), p2.getId(), p3.getId())); @@ -223,11 +222,11 @@ private static void queryProductPurchases() { * @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 (Cache.Entry e : res) - System.out.println(" " + e.getValue().toString()); + for (List row : res) + System.out.println(" " + row.toString()); } /** diff --git a/examples/src/main/java/org/apache/ignite/examples/sql/SqlQueriesExample.java b/examples/src/main/java/org/apache/ignite/examples/sql/SqlQueriesExample.java index 77d36294b2824..f3b99791e2d62 100644 --- a/examples/src/main/java/org/apache/ignite/examples/sql/SqlQueriesExample.java +++ b/examples/src/main/java/org/apache/ignite/examples/sql/SqlQueriesExample.java @@ -25,7 +25,6 @@ import org.apache.ignite.cache.affinity.AffinityKey; import org.apache.ignite.cache.query.QueryCursor; import org.apache.ignite.cache.query.SqlFieldsQuery; -import org.apache.ignite.cache.query.SqlQuery; import org.apache.ignite.configuration.CacheConfiguration; import org.apache.ignite.examples.ExampleNodeStartup; import org.apache.ignite.examples.model.Organization; @@ -44,7 +43,7 @@ * collocated mode. Refer to {@link AffinityKey} javadoc for more details. *

    * To use distributed joins it is necessary to set query 'distributedJoin' flag using - * {@link SqlFieldsQuery#setDistributedJoins(boolean)} or {@link SqlQuery#setDistributedJoins(boolean)}. + * {@link SqlFieldsQuery#setDistributedJoins(boolean)}. *

  • *
  • * Note that if you created query on to replicated cache, all data will @@ -144,16 +143,15 @@ private static void sqlQuery() { IgniteCache cache = Ignition.ignite().cache(PERSON_CACHE); // SQL clause which selects salaries based on range. - String sql = "salary > ? and salary <= ?"; + // Extract fields of the entry. + String sql = "select * from Person where salary > ? and salary <= ?"; // Execute queries for salary ranges. print("People with salaries between 0 and 1000 (queried with SQL query): ", - cache.query(new SqlQuery, Person>(Person.class, sql). - setArgs(0, 1000)).getAll()); + cache.query(new SqlFieldsQuery(sql).setArgs(0, 1000)).getAll()); print("People with salaries between 1000 and 2000 (queried with SQL query): ", - cache.query(new SqlQuery, Person>(Person.class, sql). - setArgs(1000, 2000)).getAll()); + cache.query(new SqlFieldsQuery(sql).setArgs(1000, 2000)).getAll()); } /** @@ -164,18 +162,16 @@ private static void sqlQueryWithJoin() { // SQL clause query which joins on 2 types to select people for a specific organization. String joinSql = - "from Person, \"" + ORG_CACHE + "\".Organization as org " + - "where Person.orgId = org.id " + + "select pers.* from Person as pers, \"" + ORG_CACHE + "\".Organization as org " + + "where pers.orgId = org.id " + "and lower(org.name) = lower(?)"; // Execute queries for find employees for different organizations. print("Following people are 'ApacheIgnite' employees: ", - cache.query(new SqlQuery, Person>(Person.class, joinSql). - setArgs("ApacheIgnite")).getAll()); + cache.query(new SqlFieldsQuery(joinSql).setArgs("ApacheIgnite")).getAll()); print("Following people are 'Other' employees: ", - cache.query(new SqlQuery, Person>(Person.class, joinSql). - setArgs("Other")).getAll()); + cache.query(new SqlFieldsQuery(joinSql).setArgs("Other")).getAll()); } /** @@ -187,12 +183,11 @@ private static void sqlQueryWithDistributedJoin() { // SQL clause query which joins on 2 types to select people for a specific organization. String joinSql = - "from Person, \"" + ORG_CACHE + "\".Organization as org " + - "where Person.orgId = org.id " + + "select pers.* from Person as pers, \"" + ORG_CACHE + "\".Organization as org " + + "where pers.orgId = org.id " + "and lower(org.name) = lower(?)"; - SqlQuery qry = new SqlQuery(Person.class, joinSql). - setArgs("ApacheIgnite"); + SqlFieldsQuery qry = new SqlFieldsQuery(joinSql).setArgs("ApacheIgnite"); // Enable distributed joins for query. qry.setDistributedJoins(true); diff --git a/modules/core/src/main/java/org/apache/ignite/IgniteCache.java b/modules/core/src/main/java/org/apache/ignite/IgniteCache.java index 12329db7a26d4..8e6918ae93c2c 100644 --- a/modules/core/src/main/java/org/apache/ignite/IgniteCache.java +++ b/modules/core/src/main/java/org/apache/ignite/IgniteCache.java @@ -51,7 +51,6 @@ import org.apache.ignite.cache.query.ScanQuery; import org.apache.ignite.cache.query.SpiQuery; import org.apache.ignite.cache.query.SqlFieldsQuery; -import org.apache.ignite.cache.query.SqlQuery; import org.apache.ignite.cache.query.TextQuery; import org.apache.ignite.cache.store.CacheStore; import org.apache.ignite.cluster.ClusterGroup; @@ -366,7 +365,6 @@ public IgniteFuture localLoadCacheAsync(@Nullable IgniteBiPredicate * @param qry Query. * @return Cursor. * @see ScanQuery - * @see SqlQuery * @see SqlFieldsQuery * @see TextQuery * @see SpiQuery diff --git a/modules/core/src/main/java/org/apache/ignite/cache/query/Query.java b/modules/core/src/main/java/org/apache/ignite/cache/query/Query.java index 3b3786f381191..6726c497b0e9a 100644 --- a/modules/core/src/main/java/org/apache/ignite/cache/query/Query.java +++ b/modules/core/src/main/java/org/apache/ignite/cache/query/Query.java @@ -31,8 +31,6 @@ *
      *
    • SQL Fields query.Provides SQL way with full syntax to access cache data. * See {@link SqlFieldsQuery} for details.
    • - *
    • SQL query. Provides SQL way with simplified syntax to access cache data. - * See {@link SqlQuery} for details.
    • *
    • Full-text query. Uses full-text search engine based on Apache Lucene engine. * See {@link TextQuery} for details.
    • *
    • Scan query. Provides effective and flexible way to full cache\partition scan. diff --git a/modules/core/src/main/java/org/apache/ignite/cache/query/SqlFieldsQuery.java b/modules/core/src/main/java/org/apache/ignite/cache/query/SqlFieldsQuery.java index aa66716ba5e22..d3bd1d6a77cdf 100644 --- a/modules/core/src/main/java/org/apache/ignite/cache/query/SqlFieldsQuery.java +++ b/modules/core/src/main/java/org/apache/ignite/cache/query/SqlFieldsQuery.java @@ -30,8 +30,7 @@ /** * SQL Fields query. This query can return specific fields of data based - * on SQL {@code 'select'} clause, as opposed to {@link SqlQuery}, which always returns - * the whole key and value objects back. + * on SQL {@code 'select'} clause. *

      Collocated Flag

      * Collocation flag is used for optimization purposes. Whenever Ignite executes * a distributed query, it sends sub-queries to individual cluster members. diff --git a/modules/core/src/main/java/org/apache/ignite/cache/query/SqlQuery.java b/modules/core/src/main/java/org/apache/ignite/cache/query/SqlQuery.java index 7cb97c8036040..778dc0a4c442f 100644 --- a/modules/core/src/main/java/org/apache/ignite/cache/query/SqlQuery.java +++ b/modules/core/src/main/java/org/apache/ignite/cache/query/SqlQuery.java @@ -32,7 +32,10 @@ * SQL Query. * * @see IgniteCache#query(Query) + * + * @deprecated Since 2.8, please use {@link SqlFieldsQuery} instead. */ +@Deprecated public final class SqlQuery extends Query> { /** */ private static final long serialVersionUID = 0L; diff --git a/modules/core/src/main/java/org/apache/ignite/client/ClientCache.java b/modules/core/src/main/java/org/apache/ignite/client/ClientCache.java index 168b30bb4f06a..2f0a0445d9f33 100644 --- a/modules/core/src/main/java/org/apache/ignite/client/ClientCache.java +++ b/modules/core/src/main/java/org/apache/ignite/client/ClientCache.java @@ -28,7 +28,6 @@ import org.apache.ignite.cache.query.QueryCursor; import org.apache.ignite.cache.query.ScanQuery; import org.apache.ignite.cache.query.SqlFieldsQuery; -import org.apache.ignite.cache.query.SqlQuery; /** * Thin client cache. @@ -338,7 +337,7 @@ public interface ClientCache { public ClientCache withKeepBinary(); /** - * Queries cache. Supports {@link ScanQuery}, {@link SqlQuery} and {@link SqlFieldsQuery}. + * Queries cache. Supports {@link ScanQuery} and {@link SqlFieldsQuery}. * * @param qry Query. * @return Cursor. diff --git a/modules/core/src/main/java/org/apache/ignite/internal/jdbc/thin/JdbcThinStatement.java b/modules/core/src/main/java/org/apache/ignite/internal/jdbc/thin/JdbcThinStatement.java index 565ea562b04ff..55d69f8ce8783 100644 --- a/modules/core/src/main/java/org/apache/ignite/internal/jdbc/thin/JdbcThinStatement.java +++ b/modules/core/src/main/java/org/apache/ignite/internal/jdbc/thin/JdbcThinStatement.java @@ -32,7 +32,7 @@ import java.util.Arrays; import java.util.Collections; import java.util.List; -import org.apache.ignite.cache.query.SqlQuery; +import org.apache.ignite.cache.query.Query; import org.apache.ignite.internal.processors.cache.query.IgniteQueryErrorCode; import org.apache.ignite.internal.processors.odbc.ClientListenerResponse; import org.apache.ignite.internal.processors.odbc.SqlStateCode; @@ -65,7 +65,7 @@ */ public class JdbcThinStatement implements Statement { /** Default queryPage size. */ - private static final int DFLT_PAGE_SIZE = SqlQuery.DFLT_PAGE_SIZE; + private static final int DFLT_PAGE_SIZE = Query.DFLT_PAGE_SIZE; /** JDBC Connection implementation. */ protected final JdbcThinConnection conn;