Skip to content
For the complete documentation index, see llms.txt. This page is also available as Markdown.

SQL and Key-Value Usage

Access cached data with both SQL and the key-value API, using a sample project that creates a SQL schema and queries it both ways.

GridGain lets you use SQL and the key-value API to access cached data even when tables/caches were created and pre-loaded with SQL. You can use one or both of the methods, according to your application requirements.

This tutorial shows you how to query the cluster using both SQL and the key-value APIs using a sample project available on GitHub.

The project creates a SQL schema of cities of the world, populates it with data from a script, and:

  • Accesses the loaded data using SQL queries.

  • Accesses the loaded data using key-value operations.

  • Processes data remotely.

Creating Schema and Loading Data

To create a SQL schema and load data, follow the instructions provided in the README.md file. Basically, you need to create tables using the CREATE TABLE statement.

The CREATE TABLE statement supports a number of additional parameters allowing you to specify the properties of the underlying cache. For example, the statement that creates the city table is as follows:

CREATE TABLE City (
  ID INT(11),
  Name CHAR(35),
  CountryCode CHAR(3),
  District CHAR(20),
  Population INT(11),
  PRIMARY KEY (ID, CountryCode)
) WITH "template=partitioned, backups=1, affinityKey=CountryCode, CACHE_NAME=City, KEY_TYPE=demo.model.CityKey, VALUE_TYPE=demo.model.City";

Note that we define the key class and the value class using the KEY_TYPE and VALUE_TYPE parameters at the end of the statement. If you do not define these parameters, Ignite will create the required classes with default names.

For further information on how the names of the cache and corresponding keys are generated, see CREATE TABLE.

Using Key-Value API

The IgniteCache interface provides a number of methods used to access the cache via the key-value API. For example, IgniteCache.get(key) allows you to retrieve the value for a specific key. In the example below, we retrieve the Amsterdam record and update the POPULATION field. This code is executed on the client node (the data is fetched from the server nodes).

unsupported

You can also use binary objects to access the cached data. The benefit of using binary objects is that you avoid deserialization, which is important if you access objects from a server node that does not have the ​object's class representation.

unsupported

Executing SQL Queries

In this example, we execute SQL queries using a SqlFieldsQuery object and then iterate through the results:

See the SQL API page for more information on how to use SQL queries.

Running Compute Tasks

In the example above, where we updated the population in the Amsterdam record, the data was fetched from the server node. With affinity colocation, you can run custom code on the node at which a specific key is located without pulling the data to the client node.

To use affinity colocation with custom classes which are not available in the classpath of the server nodes, set the IgniteConfiguration.peerClassLoadingEnabled parameter to true.

In the next example, we update the Amsterdam record directly on the server node. Note that you need to specify the country code as the value of the affinity key in the second argument of the affinityRun() method.

unsupported

In this example, we access the data using binary objects. This means that the data is not de-serialized into objects of the City class (and, therefore, the class files are not required on the server node).

You can also get the cache without keeping the binary format and access/work with objects of the City class. In this case, the value class must be available in the classpath of the server nodes.

This page is: Copyright © 2026 MariaDB. All rights reserved.

Last updated

Was this helpful?