Querying Example

Error formatting macro: composition-setup: java.lang.NoClassDefFoundError: net/customware/confluence/plugin/composition/CompositionUtil

You can query your GemFire Enterprise data using OQL. OQL is similar to SQL but it works with objects. With OQL querying, you can combine data from a number of regions and drill down into nested region objects, extracting only the data points you need. You can also use indexing to optimize your query performance. Read more about querying.

Running the Example

This example creates an example region with data and then runs a handful of queries against it. The first query selects everything from the region so you can see all data the queries are run against.

To run this example, you must have terminal sessions configured for the QuickStart examples, as described in Setting Up the Environment.

In a single session, run the example:

$ java quickstart.Querying

Example Listings

Program and cache configuration file listings:

Error formatting macro: deck: java.lang.NoClassDefFoundError: net/customware/confluence/plugin/composition/CompositionUtil
Error formatting macro: card: java.lang.NoClassDefFoundError: net/customware/confluence/plugin/composition/CompositionUtil
<?xml version="1.0"?>
<!DOCTYPE cache PUBLIC
    "-//GemStone Systems, Inc.//GemFire Declarative Caching 6.5//EN"
    "http://www.gemstone.com/dtd/cache6_5.dtd">

<!--
  | Querying.xml
  |
  | Initializes a region with sample Portfolio data for querying. 
-->
<cache>
        <region name="exampleRegion">
                <region-attributes>
                        <value-constraint>quickstart.Portfolio</value-constraint>
                </region-attributes>
                <!-- Add entries to the region -->
                <entry>
                        <key>
                                <string>1</string>
                        </key>
                        <value>
                                <declarable>
                                        <class-name>quickstart.Portfolio</class-name>
                                        <parameter name="id">
                                                <string>1</string>
                                        </parameter>
                                        <parameter name="type">
                                                <string>type1</string>
                                        </parameter>
                                        <parameter name="status">
                                                <string>active</string>
                                        </parameter>
                                        <parameter name="position1">
                                                <declarable>
                                                        <class-name>quickstart.Position</class-name>
                                                        <parameter name="secId">
                                                                <string>SUN</string>
                                                        </parameter>
                                                        <parameter name="qty">
                                                                <string>34000</string>
                                                        </parameter>
                                                        <parameter name="mktValue">
                                                                <string>24.42</string>
                                                        </parameter>
                                                </declarable>
                                        </parameter>
                                        <parameter name="position2">
                                                <declarable>
                                                        <class-name>quickstart.Position</class-name>
                                                        <parameter name="secId">
                                                                <string>IBM</string>
                                                        </parameter>
                                                        <parameter name="qty">
                                                                <string>8765</string>
                                                        </parameter>
                                                        <parameter name="mktValue">
                                                                <string>34.29</string>
                                                        </parameter>
                                                </declarable>
                                        </parameter>
                                </declarable>
                        </value>
                </entry>

                <entry>
                        <key>
                                <string>2</string>
                        </key>
                        <value>
                                <declarable>
                                        <class-name>quickstart.Portfolio</class-name>
                                        <parameter name="id">
                                                <string>2</string>
                                        </parameter>
                                        <parameter name="type">
                                                <string>type2</string>
                                        </parameter>
                                        <parameter name="status">
                                                <string>inactive</string>
                                        </parameter>
                                        <parameter name="position1">
                                                <declarable>
                                                        <class-name>quickstart.Position</class-name>
                                                        <parameter name="secId">
                                                                <string>YHOO</string>
                                                        </parameter>
                                                        <parameter name="qty">
                                                                <string>9834</string>
                                                        </parameter>
                                                        <parameter name="mktValue">
                                                                <string>12.925</string>
                                                        </parameter>
                                                </declarable>
                                        </parameter>
                                        <parameter name="position2">
                                                <declarable>
                                                        <class-name>quickstart.Position</class-name>
                                                        <parameter name="secId">
                                                                <string>GOOG</string>
                                                        </parameter>
                                                        <parameter name="qty">
                                                                <string>12176</string>
                                                        </parameter>
                                                        <parameter name="mktValue">
                                                                <string>21.972</string>
                                                        </parameter>
                                                </declarable>
                                        </parameter>
                                </declarable>
                        </value>
                </entry>

                <entry>
                        <key>
                                <string>3</string>
                        </key>
                        <value>
                                <declarable>
                                        <class-name>quickstart.Portfolio</class-name>
                                        <parameter name="id">
                                                <string>3</string>
                                        </parameter>
                                        <parameter name="type">
                                                <string>type3</string>
                                        </parameter>
                                        <parameter name="status">
                                                <string>active</string>
                                        </parameter>
                                        <parameter name="position1">
                                                <declarable>
                                                        <class-name>quickstart.Position</class-name>
                                                        <parameter name="secId">
                                                                <string>MSFT</string>
                                                        </parameter>
                                                        <parameter name="qty">
                                                                <string>98327</string>
                                                        </parameter>
                                                        <parameter name="mktValue">
                                                                <string>23.32</string>
                                                        </parameter>
                                                </declarable>
                                        </parameter>
                                        <parameter name="position2">
                                                <declarable>
                                                        <class-name>quickstart.Position</class-name>
                                                        <parameter name="secId">
                                                                <string>AOL</string>
                                                        </parameter>
                                                        <parameter name="qty">
                                                                <string>978</string>
                                                        </parameter>
                                                        <parameter name="mktValue">
                                                                <string>40.373</string>
                                                        </parameter>
                                                </declarable>
                                        </parameter>
                                </declarable>
                        </value>
                </entry>

                <entry>
                        <key>
                                <string>4</string>
                        </key>
                        <value>
                                <declarable>
                                        <class-name>quickstart.Portfolio</class-name>
                                        <parameter name="id">
                                                <string>4</string>
                                        </parameter>
                                        <parameter name="type">
                                                <string>type1</string>
                                        </parameter>
                                        <parameter name="status">
                                                <string>inactive</string>
                                        </parameter>
                                        <parameter name="position1">
                                                <declarable>
                                                        <class-name>quickstart.Position</class-name>
                                                        <parameter name="secId">
                                                                <string>APPL</string>
                                                        </parameter>
                                                        <parameter name="qty">
                                                                <string>90</string>
                                                        </parameter>
                                                        <parameter name="mktValue">
                                                                <string>67.356572</string>
                                                        </parameter>
                                                </declarable>
                                        </parameter>
                                        <parameter name="position2">
                                                <declarable>
                                                        <class-name>quickstart.Position</class-name>
                                                        <parameter name="secId">
                                                                <string>ORCL</string>
                                                        </parameter>
                                                        <parameter name="qty">
                                                                <string>376</string>
                                                        </parameter>
                                                        <parameter name="mktValue">
                                                                <string>101.34</string>
                                                        </parameter>
                                                </declarable>
                                        </parameter>
                                </declarable>
                        </value>
                </entry>

                <entry>
                        <key>
                                <string>5</string>
                        </key>
                        <value>
                                <declarable>
                                        <class-name>quickstart.Portfolio</class-name>
                                        <parameter name="id">
                                                <string>5</string>
                                        </parameter>
                                        <parameter name="type">
                                                <string>type2</string>
                                        </parameter>
                                        <parameter name="status">
                                                <string>active</string>
                                        </parameter>
                                        <parameter name="position1">
                                                <declarable>
                                                        <class-name>quickstart.Position</class-name>
                                                        <parameter name="secId">
                                                                <string>SAP</string>
                                                        </parameter>
                                                        <parameter name="qty">
                                                                <string>90</string>
                                                        </parameter>
                                                        <parameter name="mktValue">
                                                                <string>67.356572</string>
                                                        </parameter>
                                                </declarable>
                                        </parameter>
                                        <parameter name="position2">
                                                <declarable>
                                                        <class-name>quickstart.Position</class-name>
                                                        <parameter name="secId">
                                                                <string>DELL</string>
                                                        </parameter>
                                                        <parameter name="qty">
                                                                <string>376</string>
                                                        </parameter>
                                                        <parameter name="mktValue">
                                                                <string>101.34</string>
                                                        </parameter>
                                                </declarable>
                                        </parameter>
                                </declarable>
                        </value>
                </entry>

                <entry>
                        <key>
                                <string>6</string>
                        </key>
                        <value>
                                <declarable>
                                        <class-name>quickstart.Portfolio</class-name>
                                        <parameter name="id">
                                                <string>6</string>
                                        </parameter>
                                        <parameter name="type">
                                                <string>type3</string>
                                        </parameter>
                                        <parameter name="status">
                                                <string>inactive</string>
                                        </parameter>
                                        <parameter name="position1">
                                                <declarable>
                                                        <class-name>quickstart.Position</class-name>
                                                        <parameter name="secId">
                                                                <string>RHAT</string>
                                                        </parameter>
                                                        <parameter name="qty">
                                                                <string>90</string>
                                                        </parameter>
                                                        <parameter name="mktValue">
                                                                <string>67.356572</string>
                                                        </parameter>
                                                </declarable>
                                        </parameter>
                                        <parameter name="position2">
                                                <declarable>
                                                        <class-name>quickstart.Position</class-name>
                                                        <parameter name="secId">
                                                                <string>NOVL</string>
                                                        </parameter>
                                                        <parameter name="qty">
                                                                <string>376</string>
                                                        </parameter>
                                                        <parameter name="mktValue">
                                                                <string>101.34</string>
                                                        </parameter>
                                                </declarable>
                                        </parameter>
                                </declarable>
                        </value>
                </entry>
        </region>
</cache>
Error formatting macro: card: java.lang.NoClassDefFoundError: net/customware/confluence/plugin/composition/CompositionUtil
package quickstart;

import java.io.BufferedReader;
import java.io.InputStreamReader;
import java.util.Collection;
import com.gemstone.gemfire.cache.Cache;
import com.gemstone.gemfire.cache.CacheFactory;
import com.gemstone.gemfire.cache.Region;
import com.gemstone.gemfire.cache.query.Query;
import com.gemstone.gemfire.cache.query.QueryService;
import com.gemstone.gemfire.cache.query.SelectResults;

/**
 * You can query on the data in your cached regions. Please refer to the 
 * quickstart guide for instructions on how to run this example. 
 * 
 * @author GemStone Systems, Inc.
 * @since 4.1.1
 */
public class Querying {

  public static void main(String[] args) throws Exception {
    System.out.println("\nThis example demonstrates querying on a set of data in a GemFire ");
    System.out.println("region. The data represents Portfolios containing Positions. ");

    System.out.println("\nConnecting to the distributed system and creating the cache.");
    
    // Create the cache which causes the cache-xml-file to be parsed
    Cache cache = new CacheFactory()
      .set("name", "Querying")
      .set("cache-xml-file", "xml/Querying.xml")
      .create();

    // Get the exampleRegion
    Region<?,?> exampleRegion = cache.getRegion("exampleRegion");
    System.out.println("Example region, " + exampleRegion.getFullPath() + ", created in cache. ");

    // Get the query service for the cache
    QueryService queryService = cache.getQueryService();
    Query query = null;
    Object result = null;
    
    // Query everything to show what's in the region
    query = queryService.newQuery("SELECT DISTINCT * FROM /exampleRegion");
    System.out.println("\nExecuting query:\n\t" + query.getQueryString()); 
    result = query.execute();
    System.out.println("Query result:\n\t" + formatQueryResult(result));
    
    System.out.println("\nPress Enter to continue to next query...");
    BufferedReader bufferedReader = new BufferedReader(new InputStreamReader(System.in));
    bufferedReader.readLine();
    
    // Execute a simple query on the exampleRegion 
    query = queryService.newQuery("/exampleRegion.isDestroyed");
    System.out.println("\nExecuting query:\n\t" + query.getQueryString()); 
    result = query.execute();
    System.out.println("Query result:\n\t" + formatQueryResult(result));
    
    System.out.println("\nPress Enter to continue to next query...");
    // bufferedReader = new BufferedReader(new InputStreamReader(System.in));
    bufferedReader.readLine();
    
    // Execute a query with SELECT, FROM and WHERE clauses
    query = queryService.newQuery(
      "SELECT DISTINCT * FROM /exampleRegion WHERE status = 'active'");
    System.out.println("Executing query:\n\t" + query.getQueryString()); 
    result = query.execute();
    System.out.println("Query result:\n\t" + formatQueryResult(result));
    
    System.out.println("Press Enter to continue to next query...");
    // bufferedReader = new BufferedReader(new InputStreamReader(System.in));
    bufferedReader.readLine();
    
    // Execute a query with a compound WHERE clause 
    query = queryService.newQuery(
      "SELECT DISTINCT * FROM /exampleRegion " +
      "WHERE status = 'active' AND \"type\" = 'type3'");
    System.out.println("Executing query:\n\t" + query.getQueryString()); 
    result = query.execute();
    System.out.println("Query result:\n\t" + formatQueryResult(result));

    System.out.println("Press Enter to continue to next query...");
    // bufferedReader = new BufferedReader(new InputStreamReader(System.in));
    bufferedReader.readLine();
    
    // Execute a more complex query
    query = queryService.newQuery(
      "IMPORT quickstart.Position; " +
      "SELECT DISTINCT posnVal " +
      "FROM /exampleRegion, positions.values posnVal TYPE Position " +
      "WHERE status = 'active' AND posnVal.mktValue >= 25.00");
    System.out.println("Executing query:\n\t" + query.getQueryString()); 
    result = query.execute();
    System.out.println("Query result:\n\t" + formatQueryResult(result));

    // Close the cache and disconnect from GemFire distributed system
    System.out.println("Closing the cache and disconnecting.");
    cache.close();
  }
  
  public static String formatQueryResult(Object result) {
    if (result == null) {
    	return "null";
    }
    else if (result == QueryService.UNDEFINED) {
      return "UNDEFINED";
    }
    if (result instanceof SelectResults) {
      Collection<?> collection = ((SelectResults<?>)result).asList();
      StringBuffer sb = new StringBuffer();
      for (Object e: collection) {
        sb.append(e + "\n\t");
      }
      return sb.toString();
    }
    else {
    	return result.toString();
    }
  }
}
Error formatting macro: card: java.lang.NoClassDefFoundError: net/customware/confluence/plugin/composition/CompositionUtil
package quickstart;

import java.io.Serializable;
import java.util.LinkedHashMap;
import java.util.Map;
import java.util.Properties;
import com.gemstone.gemfire.cache.Declarable;

/**
 * A stock portfolio that consists of multiple {@link Position}
 * objects that represent shares of stock (a "security").  Instances
 * of <code>Portfolio</code> can be stored in a GemFire
 * <code>Region</code> and their contents can be queried using the
 * GemFire query service.
 * <P>
 * This class is <code>Serializable</code> because we want it to be
 * distributed to multiple members of a distributed system.  Because
 * this class is <code>Declarable</code>, we can describe instances of
 * it in a GemFire <code>cache.xml</code> file.
 *
 * @author GemStone Systems, Inc.
 * @since 4.1.1
 */
public class Portfolio implements Declarable, Serializable {
  private static final long serialVersionUID = 9097335119586059309L;
  
  private int id;  /* id is used as the entry key and is stored in the entry */
  private String type;
  private Map<String,Position> positions = new LinkedHashMap<String,Position>();
  private String status;
  
  public void init(Properties props) {
    this.id = Integer.parseInt(props.getProperty("id"));
    this.type = props.getProperty("type", "type1");
    this.status = props.getProperty("status", "active");
    
    // get the positions. These are stored in the properties object
    // as Positions, not String, so use Hashtable protocol to get at them.
    // the keys are named "positionN", where N is an integer.
    for (Map.Entry<Object, Object> entry: props.entrySet()) {
      String key = (String)entry.getKey();
      if (key.startsWith("position")) {
        Position pos = (Position)entry.getValue();
        this.positions.put(pos.getSecId(), pos);
      }
    }
  }
  
  public String getStatus(){
    return status;
  }
  
  public int getId(){
    return this.id;
  }
  
  public Map<String,Position> getPositions(){
    return this.positions;
  }
  
  public String getType() {
    return this.type;
  }
  
  public boolean isActive(){
    return status.equals("active");
  }
  
  public String toString(){
    StringBuffer out = new StringBuffer();
    out.append("\n\tPortfolio [id=" + this.id + " status=" + this.status);
    out.append(" type=" + this.type);
    boolean firstTime = true;
    for (Map.Entry<String, Position> entry: positions.entrySet()) {
      if (!firstTime) {
        out.append(", ");
      }
      out.append("\n\t\t");
      out.append(entry.getKey() + ":" + entry.getValue());
      firstTime = false;
    }
    out.append("]");
    return out.toString();
  }
}

Related Javadocs

query package

Enter labels to add to this page:
Please wait 
Looking for a label? Just start typing.