SQL API
Use GridGain's SQL API to configure queryable fields, run queries, execute DML and DDL, specify schemas, and cancel long-running queries.
In addition to using the JDBC driver, Java developers can use GridGain's SQL APIs to query and modify data stored in GridGain.
The SqlFieldsQuery class is an interface for executing SQL statements and navigating through the results. SqlFieldsQuery is executed through the IgniteCache.query(SqlFieldsQuery) method, which returns a query cursor.
Configuring Queryable Fields
If you want to query a cache using SQL statements, you need to define which fields of the value objects are queryable. Queryable fields are the fields of your data model that the SQL engine can "see" and query.
In Java, queryable fields can be configured in two ways:
using annotations
by defining query entities
@QuerySqlField Annotation
To make specific fields queryable, annotate the fields in the value class definition with the @QuerySqlField annotation and call CacheConfiguration.setIndexedTypes(...).
class Person implements Serializable {
/** Indexed field. Will be visible to the SQL engine. */
@QuerySqlField(index = true)
private long id;
/** Queryable field. Will be visible to the SQL engine. */
@QuerySqlField
private String name;
/** Will NOT be visible to the SQL engine. */
private int age;
/**
* Indexed field sorted in descending order. Will be visible to the SQL engine.
*/
@QuerySqlField(index = true, descending = true)
private float salary;
}
public static void main(String[] args) {
Ignite ignite = Ignition.start();
CacheConfiguration<Long, Person> personCacheCfg = new CacheConfiguration<Long, Person>();
personCacheCfg.setName("Person");
personCacheCfg.setIndexedTypes(Long.class, Person.class);
IgniteCache<Long, Person> cache = ignite.createCache(personCacheCfg);
}Make sure to call CacheConfiguration.setIndexedTypes(...) to let the SQL engine know about the annotated fields.
unsupported
Query Entities
You can define queryable fields using the QueryEntity class. Query entities can be configured via XML configuration.
unsupported
Querying
To execute a select query on a cache, simply create an object of SqlFieldsQuery providing the query string to the constructor and run cache.query(...). Note that in the following example, the Person cache must be configured to be visible to the SQL engine.
SqlFieldsQuery returns a cursor that iterates through the results that match the SQL query.
In .NET, this cursor implements IAsyncEnumerable<T>, so you can iterate the rows with await foreach, streaming each page as it arrives without blocking the calling thread:
Local Execution
To force local execution of a query, use SqlFieldsQuery.setLocal(true). In this case, the query is executed against the data stored on the node where the query is run. It means that the results of the query are almost always incomplete. Use the local mode only if you are confident you understand this limitation.
Subqueries in WHERE Clause
SELECT queries used in INSERT and MERGE statements as well as SELECT queries generated by UPDATE and DELETE operations are distributed and executed in either colocated or non-colocated distributed modes.
However, if there is a subquery that is executed as part of a WHERE clause, then it can be executed in the colocated mode only.
For instance, let's consider the following query:
The SQL engine generates the SELECT query in order to get a list of entries to be deleted. The query is distributed and executed across the cluster and looks like the one below:
However, the subquery from the IN clause (SELECT personId FROM Salary ...) is not distributed further and is executed over the local data set available on the node.
Inserting, Updating, Deleting, and Merging
With SqlFieldsQuery you can execute the other DML commands in order to modify the data:
When using SqlFieldsQuery to execute DDL statements, you must call getAll() on the cursor returned from the query(...) method.
Specifying the Schema
By default, any SELECT statement executed via SqlFieldsQuery is resolved against the PUBLIC schema. However, if the table you want to query is in a different schema, you can specify the schema by calling SqlFieldsQuery.setSchema(...). In this case, the statement is executed in the given schema.
Alternatively, you can define the schema in the statement:
Creating Tables
You can pass any supported DDL statement to SqlFieldsQuery and execute it on a cache as shown below.
In terms of SQL schema, the following tables are created as a result of executing the code:
Table "Person" in the "Person" schema (if it hasn't been created before).
Table "City" in the "Person" schema.
To query the "City" table, use statements like select * from Person.City or new SqlFieldsQuery("select * from City").setSchema("PERSON") (note the uppercase).
Cancelling Queries
There are two ways to cancel long running queries.
The first approach is to prevent run away queries by setting a query execution timeout.
unsupported
The second approach is to halt the query by using QueryCursor.close().
unsupported
Preventing OutOfMemory Exceptions
GridGain has a mechanism that prevents OutOfMemory exceptions by setting a limit to the amount of heap allocated to SQL queries. If a query reaches the limit, it is either stopped or offloaded to disk, depending on whether query offloading is enabled. Refer to the Memory Quotas page for details.
The following code snippets illustrates how to catch a memory quota exception:
unsupported
unsupported
Example
The GridGain distribution package includes a ready-to-run SqlDmlExample that demonstrates the usage of all the above-mentioned DML operations.
This page is: Copyright © 2026 MariaDB. All rights reserved.
Last updated
Was this helpful?

