Examples of Data Grids, Compute Grids, Service Grids, and Executing SQL Queries
Examples of Data Grids, Compute Grids, Service Grids, and Executing SQL Queries
We give a few examples of how to use Apache Ignite as a Compute Grid, a Data Grid, a Service Grid, and to execute SQL queries. Let's get to it!
Join the DZone community and get the full member experience.
Join For FreeIn this article, we will show a few examples of how to use Apache Ignite as a Compute Grid, a Data Grid, a Service Grid, and executing SQL queries on Apache Ignite. These are basic examples and use the basic APIs that are available. There will be a few posts in the near future which explain the available APIs in Compute Grid, Service Grid, and Data Grid.
Ignite SQL Example
Apache Ignite comes with JDBC Thin driver support to execute SQL queries on the in-memory data grid. In the example below, we will create tables, insert data into tables, and get data from tables. I will assume that you are running Apache Ignite in your local environment, otherwise, please read this setup guide for running an Apache Ignite server.
Creating Tables
try (Connection conn = DriverManager.getConnection("jdbc:ignite:thin://127.0.0.1/");
Statement stmt = conn.createStatement();) {
//line 1
stmt.executeUpdate("CREATE TABLE City (id LONG PRIMARY KEY, name VARCHAR) WITH \"template=replicated\"");
//line 2
stmt.executeUpdate("CREATE TABLE Person (id LONG, name VARCHAR, city_id LONG, PRIMARY KEY (id, city_id)) WITH \"backups=1, affinityKey=city_id\"");
stmt.executeUpdate("CREATE INDEX idx_city_name ON City (name)");
stmt.executeUpdate("CREATE INDEX idx_person_name ON Person (name)");
}
In line 1
, we are creating a City table with CacheMode
replicated, which means it will be replicated on the whole cluster. There are three possible values for CacheMode
: LOCAL
, REPLICATED
, and PARTITIONED
. We will discuss this later on in more detail
In line 2
, we are creating a Person table. You might have noticed affinityKey
being used. The purpose of affinityKey
is to collate the data together.
Inserting Data Into Tables
try (PreparedStatement stmt = conn.prepareStatement("INSERT INTO City (id, name) VALUES (?, ?)")) {
stmt.setLong(1, 1L);
stmt.setString(2, "Forest Hill");
stmt.executeUpdate();
stmt.setLong(1, 2L);
stmt.setString(2, "Denver");
stmt.executeUpdate();
stmt.setLong(1, 3L);
stmt.setString(2, "St. Petersburg");
stmt.executeUpdate();
}
try (PreparedStatement stmt = conn.prepareStatement("INSERT INTO Person (id, name, city_id) VALUES (?, ?, ?)")) {
stmt.setLong(1, 1L);
stmt.setString(2, "John Doe");
stmt.setLong(3, 3L);
stmt.executeUpdate();
stmt.setLong(1, 2L);
stmt.setString(2, "Jane Roe");
stmt.setLong(3, 2L);
stmt.executeUpdate();
stmt.setLong(1, 3L);
stmt.setString(2, "Mary Major");
stmt.setLong(3, 1L);
stmt.executeUpdate();
stmt.setLong(1, 4L);
stmt.setString(2, "Richard Miles");
stmt.setLong(3, 2L);
stmt.executeUpdate();
}
Querying Data From Tables
try (Connection conn = DriverManager.getConnection("jdbc:ignite:thin://127.0.0.1/");
Statement stmt = conn.createStatement()) {
try (ResultSet rs = stmt.executeQuery("SELECT p.name, c.name FROM Person p, City c WHERE p.city_id = c.id")) {
while (rs.next())
System.out.println(rs.getString(1) + ", " + rs.getString(2));
}
}
}
You can find the full example code here.
Ignite Compute Grid Example
In this example, we will use Ignite's compute grid to fetch data.
try (Ignite ignite = Ignition.start(defaultIgniteCfg("cache-reading-compute-engine"))) {
long cityId = 1;
ignite.compute().affinityCall("SQL_PUBLIC_CITY", cityId, new IgniteCallable<List<String>>() {
private static final long serialVersionUID = -131151815825938052L;
@IgniteInstanceResource
private Ignite currentIgniteInstance;
@Override
public List<String> call() throws Exception {
List<String> names = new ArrayList<>();
IgniteCache<BinaryObject, BinaryObject> personCache = currentIgniteInstance.cache("SQL_PUBLIC_PERSON").withKeepBinary();
IgniteBiPredicate<BinaryObject, BinaryObject> filter = (BinaryObject key, BinaryObject value) -> {
return key.hasField("CITY_ID") && key.<Long>field("CITY_ID") == cityId;
};
ScanQuery<BinaryObject, BinaryObject> query = new ScanQuery<>(filter);
try (QueryCursor<Entry<BinaryObject, BinaryObject>> cursor = personCache.query(query)) {
Iterator<Entry<BinaryObject, BinaryObject>> itr = cursor.iterator();
while (itr.hasNext()) {
Entry<BinaryObject, BinaryObject> cache = itr.next();
names.add(cache.getValue().<String>field("NAME"));
}
}
return names;
}
}).forEach(System.out::println);;
}
In this example, we are getting a list of people who reside in the same city. We are calling a compute grid on the SQL_PUBLIC_CITY
cache to query with the affinitykey
cityId and the IgniteCallable
task. In the IgniteCallable
task, we have @IgniteInstanceResource
which will be injected by the Ignite server running this task.
Ignite Data Example
This example will make use of Ignite as an in-memory data grid.
try (Ignite ignite = Ignition.start(defaultIgniteCfg("ignite-data-grid"))) {
IgniteCache personCache = ignite.getOrCreateCache("personCache");
for (int i = 0; i < 10; i++) {
personCache.put(i, "Gaurav " + i);
}
for (int i = 0; i < 10; i++) {
System.out.println(personCache.get(i));
}
}
Ignite Service Grid Example
interface TimeService extends Service {
public LocalDateTime currentDateTime();
}
static class TimeServiceImpl implements TimeService {
private static final long serialVersionUID = 3977097368864906176L;
@Override
public void cancel(ServiceContext ctx) {
System.out.println("Service is cancelled!");
}
@Override
public void init(ServiceContext ctx) throws Exception {
System.out.println("Service is initialized!");
}
@Override
public void execute(ServiceContext ctx) throws Exception {
System.out.println("Service is deployed!");
}
@Override
public LocalDateTime currentDateTime() {
return LocalDateTime.now();
}
}
try (Ignite ignite = Ignition.start(defaultIgniteCfg("ignite-service-grid"))) {
ignite.services().deployClusterSingleton("timeServiceImpl", new TimeServiceImpl());
TimeService timeService = ignite.services().service("timeServiceImpl");
System.out.println("Current time is: " + timeService.currentDateTime());
}
If you want to deploy some service on the grid, then it should implement theService
interface. Also, service grid deployments are not zero deployments. You need to put the compiled jars into the Ignite server instance and then restart the instance as well.
Published at DZone with permission of Gaurav Rai Mazra , DZone MVB. See the original article here.
Opinions expressed by DZone contributors are their own.
{{ parent.title || parent.header.title}}
{{ parent.tldr }}
{{ parent.linkDescription }}
{{ parent.urlSource.name }}