Skip to content

Commit

Permalink
IGNITE-11334: SQL: Deprecated SqlQuery in Java. This closes #6151.
Browse files Browse the repository at this point in the history
  • Loading branch information
tledkov authored and devozerov committed Apr 4, 2019
1 parent 770e2ba commit e1ab893
Show file tree
Hide file tree
Showing 10 changed files with 78 additions and 129 deletions.
Expand Up @@ -19,38 +19,37 @@


import java.sql.Timestamp; import java.sql.Timestamp;
import java.util.Arrays; import java.util.Arrays;
import java.util.LinkedHashMap; import java.util.Collections;
import java.util.List; import java.util.List;
import javax.cache.Cache; import javax.cache.Cache;
import org.apache.ignite.Ignite; import org.apache.ignite.Ignite;
import org.apache.ignite.IgniteCache; import org.apache.ignite.IgniteCache;
import org.apache.ignite.Ignition; import org.apache.ignite.Ignition;
import org.apache.ignite.binary.BinaryObject;
import org.apache.ignite.cache.CacheKeyConfiguration; import org.apache.ignite.cache.CacheKeyConfiguration;
import org.apache.ignite.cache.CacheMode; import org.apache.ignite.cache.CacheMode;
import org.apache.ignite.cache.QueryEntity; import org.apache.ignite.cache.QueryEntity;
import org.apache.ignite.cache.QueryIndex; import org.apache.ignite.cache.QueryIndex;
import org.apache.ignite.cache.QueryIndexType; import org.apache.ignite.cache.QueryIndexType;
import org.apache.ignite.cache.query.QueryCursor; import org.apache.ignite.cache.query.QueryCursor;
import org.apache.ignite.cache.query.SqlFieldsQuery; 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.query.TextQuery;
import org.apache.ignite.configuration.CacheConfiguration; import org.apache.ignite.configuration.CacheConfiguration;
import org.apache.ignite.examples.model.Address; import org.apache.ignite.examples.model.Address;
import org.apache.ignite.examples.model.Employee; import org.apache.ignite.examples.model.Employee;
import org.apache.ignite.examples.model.EmployeeKey; import org.apache.ignite.examples.model.EmployeeKey;
import org.apache.ignite.examples.model.Organization; import org.apache.ignite.examples.model.Organization;
import org.apache.ignite.examples.model.OrganizationType; import org.apache.ignite.examples.model.OrganizationType;
import org.apache.ignite.binary.BinaryObject;


/** /**
* This example demonstrates use of binary objects with cache queries. * This example demonstrates use of binary objects with cache queries. The example populates cache with sample data and
* The example populates cache with sample data and runs several SQL and full text queries over this data. * runs several SQL and full text queries over this data.
* <p> * <p>
* Remote nodes should always be started with the following command: * Remote nodes should always be started with the following command: {@code 'ignite.{sh|bat}
* {@code 'ignite.{sh|bat} examples/config/example-ignite.xml'} * examples/config/example-ignite.xml'}
* <p> * <p>
* Alternatively you can run {@link org.apache.ignite.examples.ExampleNodeStartup} in another JVM which will * Alternatively you can run {@link org.apache.ignite.examples.ExampleNodeStartup} in another JVM which will start a
* start a node with {@code examples/config/example-ignite.xml} configuration. * node with {@code examples/config/example-ignite.xml} configuration.
*/ */
public class CacheClientBinaryQueryExample { public class CacheClientBinaryQueryExample {
/** Organization cache name. */ /** Organization cache name. */
Expand Down Expand Up @@ -106,15 +105,12 @@ public static void main(String[] args) {
// Get cache that will work with binary objects. // Get cache that will work with binary objects.
IgniteCache<BinaryObject, BinaryObject> binaryCache = employeeCache.withKeepBinary(); IgniteCache<BinaryObject, BinaryObject> binaryCache = employeeCache.withKeepBinary();


// Run SQL query example. // Run SQL fields query example.
sqlQuery(binaryCache); sqlFieldsQuery(binaryCache);


// Run SQL query with join example. // Run SQL query with join example.
sqlJoinQuery(binaryCache); sqlJoinQuery(binaryCache);


// Run SQL fields query example.
sqlFieldsQuery(binaryCache);

// Run full text query example. // Run full text query example.
textQuery(binaryCache); textQuery(binaryCache);


Expand All @@ -134,30 +130,21 @@ public static void main(String[] args) {
* @return Cache type metadata. * @return Cache type metadata.
*/ */
private static QueryEntity createEmployeeQueryEntity() { private static QueryEntity createEmployeeQueryEntity() {
QueryEntity employeeEntity = new QueryEntity(); return new QueryEntity()

.setValueType(Employee.class.getName())
employeeEntity.setValueType(Employee.class.getName()); .setKeyType(EmployeeKey.class.getName())
employeeEntity.setKeyType(EmployeeKey.class.getName()); .addQueryField("organizationId", Integer.class.getName(), null)

.addQueryField("name", String.class.getName(), null)
LinkedHashMap<String, String> fields = new LinkedHashMap<>(); .addQueryField("salary", Long.class.getName(), null)

.addQueryField("addr.zip", Integer.class.getName(), null)
fields.put("name", String.class.getName()); .addQueryField("addr.street", String.class.getName(), null)
fields.put("salary", Long.class.getName()); .setKeyFields(Collections.singleton("organizationId"))
fields.put("addr.zip", Integer.class.getName()); .setIndexes(Arrays.asList(
fields.put("organizationId", Integer.class.getName()); new QueryIndex("name"),
fields.put("addr.street", Integer.class.getName()); new QueryIndex("salary"),

new QueryIndex("addr.zip"),
employeeEntity.setFields(fields); new QueryIndex("organizationId"),

new QueryIndex("addr.street", QueryIndexType.FULLTEXT)));
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;
} }


/** /**
Expand All @@ -166,42 +153,32 @@ private static QueryEntity createEmployeeQueryEntity() {
* @return Cache type metadata. * @return Cache type metadata.
*/ */
private static QueryEntity createOrganizationQueryEntity() { private static QueryEntity createOrganizationQueryEntity() {
QueryEntity organizationEntity = new QueryEntity(); return new QueryEntity()

.setValueType(Organization.class.getName())
organizationEntity.setValueType(Organization.class.getName()); .setKeyType(Integer.class.getName())
organizationEntity.setKeyType(Integer.class.getName()); .addQueryField("keyId", Integer.class.getName(), null)

.addQueryField("name", String.class.getName(), null)
LinkedHashMap<String, String> fields = new LinkedHashMap<>(); .addQueryField("address.street", String.class.getName(), null)

.setKeyFieldName("keyId")
fields.put("name", String.class.getName()); .setIndexes(Arrays.asList(
fields.put("address.street", String.class.getName()); new QueryIndex("name")));

organizationEntity.setFields(fields);

organizationEntity.setIndexes(Arrays.asList(
new QueryIndex("name")
));

return organizationEntity;
} }


/** /**
* Queries employees that have provided ZIP code in address. * Queries names and salaries for all employees.
* *
* @param cache Ignite cache. * @param cache Ignite cache.
*/ */
private static void sqlQuery(IgniteCache<BinaryObject, BinaryObject> cache) { private static void sqlFieldsQuery(IgniteCache<BinaryObject, BinaryObject> cache) {
SqlQuery<BinaryObject, BinaryObject> query = new SqlQuery<>(Employee.class, "zip = ?"); SqlFieldsQuery qry = new SqlFieldsQuery("select name, salary from Employee");

int zip = 94109;


QueryCursor<Cache.Entry<BinaryObject, BinaryObject>> employees = cache.query(query.setArgs(zip)); QueryCursor<List<?>> employees = cache.query(qry);


System.out.println(); System.out.println();
System.out.println(">>> Employees with zip " + zip + ':'); System.out.println(">>> Employee names and their salaries:");


for (Cache.Entry<BinaryObject, BinaryObject> e : employees.getAll()) for (List<?> row : employees.getAll())
System.out.println(">>> " + e.getValue().deserialize()); System.out.println(">>> [Name=" + row.get(0) + ", salary=" + row.get(1) + ']');
} }


/** /**
Expand All @@ -210,37 +187,19 @@ private static void sqlQuery(IgniteCache<BinaryObject, BinaryObject> cache) {
* @param cache Ignite cache. * @param cache Ignite cache.
*/ */
private static void sqlJoinQuery(IgniteCache<BinaryObject, BinaryObject> cache) { private static void sqlJoinQuery(IgniteCache<BinaryObject, BinaryObject> cache) {
SqlQuery<BinaryObject, BinaryObject> qry = new SqlQuery<>(Employee.class, SqlFieldsQuery qry = new SqlFieldsQuery(
"from Employee, \"" + ORGANIZATION_CACHE_NAME + "\".Organization as org " + "select e.* from Employee e, \"" + ORGANIZATION_CACHE_NAME + "\".Organization as org " +
"where Employee.organizationId = org._key and org.name = ?"); "where e.organizationId = org.keyId and org.name = ?");


String organizationName = "GridGain"; String organizationName = "GridGain";


QueryCursor<Cache.Entry<BinaryObject, BinaryObject>> employees = QueryCursor<List<?>> employees = cache.query(qry.setArgs(organizationName));
cache.query(qry.setArgs(organizationName));


System.out.println(); System.out.println();
System.out.println(">>> Employees working for " + organizationName + ':'); System.out.println(">>> Employees working for " + organizationName + ':');


for (Cache.Entry<BinaryObject, BinaryObject> e : employees.getAll())
System.out.println(">>> " + e.getValue());
}

/**
* Queries names and salaries for all employees.
*
* @param cache Ignite cache.
*/
private static void sqlFieldsQuery(IgniteCache<BinaryObject, BinaryObject> cache) {
SqlFieldsQuery qry = new SqlFieldsQuery("select name, salary from Employee");

QueryCursor<List<?>> employees = cache.query(qry);

System.out.println();
System.out.println(">>> Employee names and their salaries:");

for (List<?> row : employees.getAll()) for (List<?> row : employees.getAll())
System.out.println(">>> [Name=" + row.get(0) + ", salary=" + row.get(1) + ']'); System.out.println(">>> " + row);
} }


/** /**
Expand Down
Expand Up @@ -27,7 +27,6 @@
import org.apache.ignite.cache.query.QueryCursor; import org.apache.ignite.cache.query.QueryCursor;
import org.apache.ignite.cache.query.ScanQuery; import org.apache.ignite.cache.query.ScanQuery;
import org.apache.ignite.cache.query.SqlFieldsQuery; 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.query.TextQuery;
import org.apache.ignite.configuration.CacheConfiguration; import org.apache.ignite.configuration.CacheConfiguration;
import org.apache.ignite.examples.ExampleNodeStartup; import org.apache.ignite.examples.ExampleNodeStartup;
Expand All @@ -49,7 +48,7 @@
* collocated mode. Refer to {@link AffinityKey} javadoc for more details. * collocated mode. Refer to {@link AffinityKey} javadoc for more details.
* <p> * <p>
* To use distributed joins it is necessary to set query 'distributedJoin' flag using * 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)}.
* </li> * </li>
* <li> * <li>
* Note that if you created query on to replicated cache, all data will * Note that if you created query on to replicated cache, all data will
Expand Down
Expand Up @@ -20,6 +20,7 @@
import java.util.Collection; import java.util.Collection;
import java.util.ConcurrentModificationException; import java.util.ConcurrentModificationException;
import java.util.HashMap; import java.util.HashMap;
import java.util.List;
import java.util.Map; import java.util.Map;
import java.util.concurrent.ThreadLocalRandom; import java.util.concurrent.ThreadLocalRandom;
import javax.cache.Cache; import javax.cache.Cache;
Expand All @@ -29,7 +30,7 @@
import org.apache.ignite.Ignition; import org.apache.ignite.Ignition;
import org.apache.ignite.cache.CacheMode; import org.apache.ignite.cache.CacheMode;
import org.apache.ignite.cache.query.QueryCursor; 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.configuration.CacheConfiguration;
import org.apache.ignite.examples.ExampleNodeStartup; import org.apache.ignite.examples.ExampleNodeStartup;


Expand Down Expand Up @@ -176,10 +177,9 @@ private static void queryStorePurchases() {
// ======================== // ========================


// Create cross cache query to get all purchases made at store1. // Create cross cache query to get all purchases made at store1.
QueryCursor<Cache.Entry<Integer, FactPurchase>> storePurchases = factCache.query(new SqlQuery( QueryCursor<List<?>> storePurchases = factCache.query(new SqlFieldsQuery(
FactPurchase.class, "select fp.* from \"" + DIM_STORE_CACHE_NAME + "\".DimStore, \"" + FACT_CACHE_NAME + "\".FactPurchase as fp "
"from \"" + DIM_STORE_CACHE_NAME + "\".DimStore, \"" + FACT_CACHE_NAME + "\".FactPurchase " + "where DimStore.id=fp.storeId and DimStore.name=?").setArgs("Store1"));
+ "where DimStore.id=FactPurchase.storeId and DimStore.name=?").setArgs("Store1"));


printQueryResults("All purchases made at store1:", storePurchases.getAll()); printQueryResults("All purchases made at store1:", storePurchases.getAll());
} }
Expand All @@ -206,11 +206,10 @@ private static void queryProductPurchases() {


// Create cross cache query to get all purchases made at store2 // Create cross cache query to get all purchases made at store2
// for specified products. // for specified products.
QueryCursor<Cache.Entry<Integer, FactPurchase>> prodPurchases = factCache.query(new SqlQuery( QueryCursor<List<?>> prodPurchases = factCache.query(new SqlFieldsQuery(
FactPurchase.class, "select fp.* from \"" + DIM_STORE_CACHE_NAME + "\".DimStore, \"" + DIM_PROD_CACHE_NAME + "\".DimProduct, " +
"from \"" + DIM_STORE_CACHE_NAME + "\".DimStore, \"" + DIM_PROD_CACHE_NAME + "\".DimProduct, " + "\"" + FACT_CACHE_NAME + "\".FactPurchase as fp "
"\"" + FACT_CACHE_NAME + "\".FactPurchase " + "where DimStore.id=fp.storeId and DimProduct.id=fp.productId "
+ "where DimStore.id=FactPurchase.storeId and DimProduct.id=FactPurchase.productId "
+ "and DimStore.name=? and DimProduct.id in(?, ?, ?)") + "and DimStore.name=? and DimProduct.id in(?, ?, ?)")
.setArgs("Store2", p1.getId(), p2.getId(), p3.getId())); .setArgs("Store2", p1.getId(), p2.getId(), p3.getId()));


Expand All @@ -223,11 +222,11 @@ private static void queryProductPurchases() {
* @param msg Initial message. * @param msg Initial message.
* @param res Results to print. * @param res Results to print.
*/ */
private static <V> void printQueryResults(String msg, Iterable<Cache.Entry<Integer, V>> res) { private static void printQueryResults(String msg, Iterable<List<?>> res) {
System.out.println(msg); System.out.println(msg);


for (Cache.Entry<?, ?> e : res) for (List<?> row : res)
System.out.println(" " + e.getValue().toString()); System.out.println(" " + row.toString());
} }


/** /**
Expand Down
Expand Up @@ -25,7 +25,6 @@
import org.apache.ignite.cache.affinity.AffinityKey; import org.apache.ignite.cache.affinity.AffinityKey;
import org.apache.ignite.cache.query.QueryCursor; import org.apache.ignite.cache.query.QueryCursor;
import org.apache.ignite.cache.query.SqlFieldsQuery; import org.apache.ignite.cache.query.SqlFieldsQuery;
import org.apache.ignite.cache.query.SqlQuery;
import org.apache.ignite.configuration.CacheConfiguration; import org.apache.ignite.configuration.CacheConfiguration;
import org.apache.ignite.examples.ExampleNodeStartup; import org.apache.ignite.examples.ExampleNodeStartup;
import org.apache.ignite.examples.model.Organization; import org.apache.ignite.examples.model.Organization;
Expand All @@ -44,7 +43,7 @@
* collocated mode. Refer to {@link AffinityKey} javadoc for more details. * collocated mode. Refer to {@link AffinityKey} javadoc for more details.
* <p> * <p>
* To use distributed joins it is necessary to set query 'distributedJoin' flag using * 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)}.
* </li> * </li>
* <li> * <li>
* Note that if you created query on to replicated cache, all data will * Note that if you created query on to replicated cache, all data will
Expand Down Expand Up @@ -144,16 +143,15 @@ private static void sqlQuery() {
IgniteCache<Long, Person> cache = Ignition.ignite().cache(PERSON_CACHE); IgniteCache<Long, Person> cache = Ignition.ignite().cache(PERSON_CACHE);


// SQL clause which selects salaries based on range. // 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. // Execute queries for salary ranges.
print("People with salaries between 0 and 1000 (queried with SQL query): ", print("People with salaries between 0 and 1000 (queried with SQL query): ",
cache.query(new SqlQuery<AffinityKey<Long>, Person>(Person.class, sql). cache.query(new SqlFieldsQuery(sql).setArgs(0, 1000)).getAll());
setArgs(0, 1000)).getAll());


print("People with salaries between 1000 and 2000 (queried with SQL query): ", print("People with salaries between 1000 and 2000 (queried with SQL query): ",
cache.query(new SqlQuery<AffinityKey<Long>, Person>(Person.class, sql). cache.query(new SqlFieldsQuery(sql).setArgs(1000, 2000)).getAll());
setArgs(1000, 2000)).getAll());
} }


/** /**
Expand All @@ -164,18 +162,16 @@ private static void sqlQueryWithJoin() {


// SQL clause query which joins on 2 types to select people for a specific organization. // SQL clause query which joins on 2 types to select people for a specific organization.
String joinSql = String joinSql =
"from Person, \"" + ORG_CACHE + "\".Organization as org " + "select pers.* from Person as pers, \"" + ORG_CACHE + "\".Organization as org " +
"where Person.orgId = org.id " + "where pers.orgId = org.id " +
"and lower(org.name) = lower(?)"; "and lower(org.name) = lower(?)";


// Execute queries for find employees for different organizations. // Execute queries for find employees for different organizations.
print("Following people are 'ApacheIgnite' employees: ", print("Following people are 'ApacheIgnite' employees: ",
cache.query(new SqlQuery<AffinityKey<Long>, Person>(Person.class, joinSql). cache.query(new SqlFieldsQuery(joinSql).setArgs("ApacheIgnite")).getAll());
setArgs("ApacheIgnite")).getAll());


print("Following people are 'Other' employees: ", print("Following people are 'Other' employees: ",
cache.query(new SqlQuery<AffinityKey<Long>, Person>(Person.class, joinSql). cache.query(new SqlFieldsQuery(joinSql).setArgs("Other")).getAll());
setArgs("Other")).getAll());
} }


/** /**
Expand All @@ -187,12 +183,11 @@ private static void sqlQueryWithDistributedJoin() {


// SQL clause query which joins on 2 types to select people for a specific organization. // SQL clause query which joins on 2 types to select people for a specific organization.
String joinSql = String joinSql =
"from Person, \"" + ORG_CACHE + "\".Organization as org " + "select pers.* from Person as pers, \"" + ORG_CACHE + "\".Organization as org " +
"where Person.orgId = org.id " + "where pers.orgId = org.id " +
"and lower(org.name) = lower(?)"; "and lower(org.name) = lower(?)";


SqlQuery qry = new SqlQuery<Long, Person>(Person.class, joinSql). SqlFieldsQuery qry = new SqlFieldsQuery(joinSql).setArgs("ApacheIgnite");
setArgs("ApacheIgnite");


// Enable distributed joins for query. // Enable distributed joins for query.
qry.setDistributedJoins(true); qry.setDistributedJoins(true);
Expand Down
2 changes: 0 additions & 2 deletions modules/core/src/main/java/org/apache/ignite/IgniteCache.java
Expand Up @@ -51,7 +51,6 @@
import org.apache.ignite.cache.query.ScanQuery; import org.apache.ignite.cache.query.ScanQuery;
import org.apache.ignite.cache.query.SpiQuery; import org.apache.ignite.cache.query.SpiQuery;
import org.apache.ignite.cache.query.SqlFieldsQuery; 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.query.TextQuery;
import org.apache.ignite.cache.store.CacheStore; import org.apache.ignite.cache.store.CacheStore;
import org.apache.ignite.cluster.ClusterGroup; import org.apache.ignite.cluster.ClusterGroup;
Expand Down Expand Up @@ -366,7 +365,6 @@ public IgniteFuture<Void> localLoadCacheAsync(@Nullable IgniteBiPredicate<K, V>
* @param qry Query. * @param qry Query.
* @return Cursor. * @return Cursor.
* @see ScanQuery * @see ScanQuery
* @see SqlQuery
* @see SqlFieldsQuery * @see SqlFieldsQuery
* @see TextQuery * @see TextQuery
* @see SpiQuery * @see SpiQuery
Expand Down
Expand Up @@ -31,8 +31,6 @@
* <ul> * <ul>
* <li><b>SQL Fields query.</b>Provides SQL way with full syntax to access cache data. * <li><b>SQL Fields query.</b>Provides SQL way with full syntax to access cache data.
* See {@link SqlFieldsQuery} for details.</li> * See {@link SqlFieldsQuery} for details.</li>
* <li><b>SQL query.</b> Provides SQL way with simplified syntax to access cache data.
* See {@link SqlQuery} for details.</li>
* <li><b>Full-text query.</b> Uses full-text search engine based on Apache Lucene engine. * <li><b>Full-text query.</b> Uses full-text search engine based on Apache Lucene engine.
* See {@link TextQuery} for details.</li> * See {@link TextQuery} for details.</li>
* <li><b>Scan query.</b> Provides effective and flexible way to full cache\partition scan. * <li><b>Scan query.</b> Provides effective and flexible way to full cache\partition scan.
Expand Down

0 comments on commit e1ab893

Please sign in to comment.