This topic explains how to write and execute a query using the VMware Tanzu GemFire QueryService.
The Tanzu GemFire QueryService provides methods to create the query object. You can then use the query object to perform query-related operations.
The QueryService instance you should use depends on whether you are querying the local cache of an application or if you want your application to query the server cache.
To query the application’s local cache or to query other members, use org.apache.geode.cache.Cache.getQueryService
.
Sample Code
// Identify your query string.
String queryString = "SELECT DISTINCT * FROM /exampleRegion";
// Get QueryService from Cache.
QueryService queryService = cache.getQueryService();
// Create the query object.
Query query = queryService.newQuery(queryString);
// Execute Query locally. Returns results set.
SelectResults results = (SelectResults)query.execute();
// Find the Size of the ResultSet.
int size = results.size();
// Iterate through your ResultSet.
Portfolio p = (Portfolio)results.iterator().next(); /* Region containing Portfolio object. */
To perform a client to server query, use org.apache.geode.cache.client.Pool.getQueryService
.
Sample Code
// Identify your query string.
String queryString = "SELECT DISTINCT * FROM /exampleRegion";
// Get QueryService from client pool.
QueryService queryService = pool.getQueryService();
// Create the query object.
Query query = queryService.newQuery(queryString);
// Execute Query locally. Returns results set.
SelectResults results = (SelectResults)query.execute();
// Find the Size of the ResultSet.
int size = results.size();
// Iterate through your ResultSet.
Portfolio p = (Portfolio)results.iterator().next(); /* Region containing Portfolio object. */
Refer to the following JavaDocs for specific APIs:
Query
packageQueryService
NoteYou can also perform queries using the gfsh
query
command. See query.
Building a Query String: To build a query string, combine supported keywords, expressions, and operators to create an expression that returns the information that you require.
OQL Syntax and Semantics: This section describes the querying language features.
Query Language Restrictions and Unsupported Features: This section describes some limitations to be aware of when composing queries in Object Query Language (OQL).