Before you begin, you can take a look at this section, which summarizes the different query methods and shows how to index fields to speed up query based operations. If you're already familiar with the SQLQuery API, you can jump ahead to Writing the code. All code is documented inside each example as well.
public class Person{
privateInteger age;
privateInteger height;
privateString name;
.
.
Constructors/Setters/Getters
}
SQLQuery is the primary API when accessing the space.
SQL Query
Using a SQL query in the code:
// Create SQLQuery that returns all the person objects that have age field equals 5 and height field less then 180
SQLQuery<Person> query = new SQLQuery<Person>(Person.class,"age = 5 and height < 180");
// Get all results that match the query
Person[] result = gigaSpace.readMultiple(query /* the query */ ,Integer.MAX_VALUE /* maximum objects to read */);
Plain, Non-Parameterized SQL Query using rlike
Regular expression pattern matching should be used with extreme care. Matching regular expressions in read operations is done in a serial manner and cannot take advantage of the space indexing mechanisms. Therefore, with large result sets, it can take a considerable amount of time to process and consume a lot of CPU resources.
Using regular expression pattern matching for notify templates is less dangerous since matching the objects is done once at a time, when the operation that the template was registered for (read, write, update, take, etc.) is invoked. In this case, the client application is not directly affected by this as the template matching is done in the background and does not affect client performance directly.
Using regular expressions for pattern matching on POJO properties in the query is done using the rlike option, as part of the SQL query:
// Create SQLQuery with 'rlike' that return all the objects that have a name field that starts with a or c
SQLQuery<Person> query = new SQLQuery<Person>(Person.class,"name rlike '(a|c).*'");
// Get all results that match the query
MyObject[] result = gigaSpace.readMultiple(query /* the query */ ,Integer.MAX_VALUE /* maximum objects to read */);
You can use a single SQLQuery instance multiple times by binding different values to it every time. The query should include the ? placeholder instead of the actual value. When executing the query, the condition that includes the ? placeholder is replaced with corresponding field values taken from the value defined in the setParameters method, or in the constructor.
Performing dynamic queries using parameters allows you to use the same field several times as part of the SQL statement, where the template value is used to fill in the parameter values.
You can perform dynamic queries using parameters, or using templates:
It is not recommended to perform both types of dynamic queries (using parameters and using templates) in one query. If you perform these together, only the values you define in the dynamic query using parameters is taken into account.
Set the parameters using the setParameters() method:
SQLQuery query<Person> = new SQLQuery<Person>(Person.class,"age > ? or height > ? and name=?");
query.setParameters(22,178,"USA");
Or set each parameter seperetly using the setParameter() method:
SQLQuery<Person> query = new SQLQuery<Person>(Person.class,"age > ? or height > ? and name=?",22,178,"USA");
You can create one SQLQuery object and assign different queries and templates to it. The values in the template are used to construct the query. The query should include the ? placeholder instead of the actual value. When executing the query, the condition that includes the ? placeholder is replaced with corresponding field values taken from the relevant template field.
When performing dynamic queries using templates, you can not use the same field several times as part of the SQL statement. To do this, use the setParameters() method in the previous tab (Parameterized query using JDBC like API).
Set the the Person template attributes:
Person template = new Person();
template.setAge(22);
template.setHeight(178);
template.setName("USA");
Set the query template using the setTemplate method:
SQLQuery<Person> query;
query.setTemplate(template);
query.setQuery("age > ? and height < ? and name=?");
Or pass the template and clause in the constructor:
SQLQuery<Person> query = new SQLQuery<Person>(template,"age > ? and salary < ? and userName=?");
This polling container (defined in a pu.xml configuration file) registers for notifications on every Person object that is written to the space and matches the query template. Upon registration, the simpleListener is invoked:
Namespace:
<os-events:notify-container id="eventContainer" giga-space="gigaSpace"><os-core:sql-query where="age>22 and height>178 and name='USA'" class="mypackage.Person"/>
<os-events:listener><os-events:annotation-adapter><os-events:delegate ref="simpleListener"/></os-events:annotation-adapter></os-events:listener></os-events:notify-container>