LATEST VERSION: 8.2.6 - CHANGELOG
Pivotal GemFire® v8.2

Writing and Executing a Query in GemFire

Writing and Executing a Query in GemFire

The 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.

Querying a Local Cache

To query the application's local cache or to query other members, use com.gemstone.gemfire.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. */

Querying a Server Cache from a Client

To perform a client to server query, use com.gemstone.gemfire.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:
Note: You can also perform queries using the gfsh query command. See query.