Skip to main content

Using Apache Ignite thin client - Apache Ignite insider blog

From the version 2.4.0, Apache Ignite introduced a new way to connect to the Ignite cluster, which allows communication with the Ignite cluster without starting an Ignite client node. Historically, Apache Ignite provides two notions of client and server nodes. Ignite client node intended as lightweight mode, which does not store data (however, it can store near cache), and does not execute any compute tasks. Mainly, client node used to communicate with the server remotely and allows manipulating the Ignite Caches using the whole set of Ignite API’s. There are two main downsides with the Ignite Client node:

  • Whenever Ignite client node connects to the Ignite cluster, it becomes the part of the cluster topology. The bigger the topology is, the harder it is for maintaining.
  • In the client mode, Apache Ignite node consumes a lot of resources for performing cache operations.

To solve the above problems, Apache Ignite provides a new binary client protocol for implementing thin Ignite client in any programming language or platforms.

Note that, word thin means, it doesn’t start any Ignite node for communicating with the Ignite cluster and doesn’t implement any discovery/communication SPI logic.

Thin client connects to the Ignite cluster through a TCP socket and performs CRUD operations using a well-defined binary protocol. The protocol is a fully socket-based, request- response style protocol. The protocol is designed to be strict enough to ensure standardization in the communication (such as connection handshake, message length, etc.), but still flexible enough that developers may expand upon the protocol to implement custom features.

Apache Ignite provides brief data formats and communication details in the documentation for using the binary protocol.Ignite already supports .NET, and Java thin client builds on top of the protocol and plans to release a thin client for major languages such as goLang, python, etc. However, you can implement your thin client in any favorite programming language of your choice by using the binary protocol.

Also note that, the performance of the Apache Ignite thin client is slightly lower than Ignite client node as it works through an intermediary node. Assume that, you have two nodes of the Apache Ignite A, B and you are using a thin client C for retrieving data from the cluster. With the thin client C, you have connected to the node B, and whenever you try to retrieve any data that belongs to the node A, the requests always go through the client B. In case of the Ignite client node, it sends the request directly to the node A.

Most of the times, you should not care about how the message formats look like, or the socket handshake performs. Thin client for every programming language encapsulates the ugly hard work under the hood for you. Anyway, if you want to have a deep dive into the Ignite binary protocol or have any issue to create your own thin client, please refer to the Ignite documentation.

Before moving on to more advanced topics, let’s have a look at a simple application to use Ignite thin client. In this simple application, I show you the bits and pieces you need to get started with the thin client. The source code for the examples is available at the GitHub repository, see chapter-2.

Step 1. Clone or download the project from the GitHub repository. If you are planning to develop the project from scratch, add the following maven dependency in your pom.xml file. The only ignite-core library need for the thin client, the rest of the libraries only used for logging.

<dependency>
    <groupId>org.apache.ignite</groupId>
    <artifactId>ignite-core</artifactId>
    <version>2.6.0</version>
</dependency>
<!-- Logging wih SLF4J -->
<dependency>
    <groupId>org.slf4j</groupId>
    <artifactId>slf4j-api</artifactId>
    <version>1.6.1</version>
</dependency>
<dependency>
    <groupId>ch.qos.logback</groupId>
    <artifactId>logback-classic</artifactId>
    <version>1.0.1</version>
</dependency>
<dependency>
    <groupId>ch.qos.logback</groupId>
    <artifactId>logback-core</artifactId>
    <version>1.0.1</version>
</dependency>

Step 2. Now, let's create a new Java class with the name HelloThinClient.
Step 3. Copy and paste the following source code. Do not forget to save the file.

import org.apache.ignite.IgniteException;
import org.apache.ignite.Ignition;
import org.apache.ignite.client.ClientCache;
import org.apache.ignite.client.IgniteClient;
import org.apache.ignite.configuration.ClientConfiguration;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

public class HelloThinClient {
 private static final Logger logger = LoggerFactory.getLogger(HelloThinClient.class);
 private static final String HOST = "127.0.0.1";
 private static final String PORT = "10800";
 private static final String CACHE_NAME = "thin-cache";

 public static void main(String[] args) {
  logger.info("Simple Ignite thin client example working over TCP socket.");
  ClientConfiguration cfg = new ClientConfiguration().setAddresses(HOST + ":" + PORT);
  try (IgniteClient igniteClient = Ignition.startClient(cfg)) {
   ClientCache < String, String > clientCache = igniteClient.getOrCreateCache(CACHE\ _NAME);
   // put a few value
   clientCache.put("Moscow", "095");
   clientCache.put("Vladimir", "033");
   // get the region code of the Vladimir String val = clientCache.get("Vladimir");
   logger.info("Print value: {}", val);
  } catch (IgniteException e) {
   logger.error("Ignite exception:", e.getMessage());
  } catch (Exception e) {
   logger.error("Ignite exception:", e.getMessage());
  }
 }
}

Step 4. Let's have a closer look at the program we have written above.

private static final Logger logger = LoggerFactory.getLogger(HelloThinClient.class);
 private static final String HOST = "127.0.0.1";
 private static final String PORT = "10800";
 private static final String CACHE_NAME = "thin-cache";

First, we have declared a few constants: logger, host IP address, port and the cache name that we are going to create. If you have a different IP address, you should change it here. Port 10800 is the default for Ignite thin client.

СlientConfiguration cfg = new ClientConfiguration().setAddresses(HOST+":"+PORT);

These are our next exciting line in the program. We have created an instance of the Ignite СlientConfiguration and passed the address we declared above. In the next try-catch block, we have defined a new cache with name thin-cache and put 2 key-value pairs. We also used Ignition.startClient method to initialize a connection to Ignite node.

try (IgniteClient igniteClient = Ignition.startClient(cfg)) {
   ClientCache < String, String > clientCache = igniteClient.getOrCreateCache(CACHE\ _NAME);
   // put a few value
   clientCache.put("Moscow", "095");
   clientCache.put("Vladimir", "033");
   // get the region code of the Vladimir String val = clientCache.get("Vladimir");
   logger.info("Print value: {}", val);
  } catch (IgniteException e) {
   logger.error("Ignite exception:", e.getMessage());
  } catch (Exception e) {
   logger.error("Ignite exception:", e.getMessage());
  }
}

Later, we retrieved the value of key Vladimir and printed the value in the console.

Step 5. Start your Apache Ignite single node cluster if it is not started yet. Use the following command in your favorite terminal.

$ IGNITE_HOME/bin/ignite.sh

Step 6. To build the project, issue the following command.

$ mvn clean install

This will run Maven, telling it to execute the install goal. This goal will compile, test, and package your project code and then copy it into the local dependency repository. The first time the build process will take a few minutes to complete, after successful compilation, an executable jar named HelloThinClient-runnable.jar will be created in the target directory.

Step 7. Run the application by typing the following command.

$ java -jar .\target\HelloThinClient-runnable.jar

You should see a lot of logs into the terminal. At the end of the log, you should find something like this.

Figure 1.

The application connected through the TCP socket to the Ignite node and performed put and get operation on cache thin-cache. If you take a look at the Ignite node console, you should notice that Ignite cluster topology has not been changed.

Figure 2.

Comments

Popular posts from this blog

Send e-mail with attachment through OSB

Oracle Service Bus (OSB) contains a good collection of adapter to integrate with any legacy application, including ftp, email, MQ, tuxedo. However e-mail still recognize as a stable protocol to integrate with any application asynchronously. Send e-mail with attachment is a common task of any business process. Inbound e-mail adapter which, integrated with OSB support attachment but outbound adapter doesn't. This post is all about sending attachment though JavaCallout action. There are two ways to handle attachment in OSB: 1) Use JavaCallout action to pass the binary data for further manipulation. It means write down a small java library which will get the attachment and send the e-mail. 2) Use integrated outbound e-mail adapter to send attachment, here you have to add a custom variable named attachment and assign the binary data to the body of the attachment variable. First option is very common and easy to implement through javax.mail api, however a much more developer manage t

Tip: SQL client for Apache Ignite cache

A new SQL client configuration described in  The Apache Ignite book . If it got you interested, check out the rest of the book for more helpful information. Apache Ignite provides SQL queries execution on the caches, SQL syntax is an ANSI-99 compliant. Therefore, you can execute SQL queries against any caches from any SQL client which supports JDBC thin client. This section is for those, who feels comfortable with SQL rather than execute a bunch of code to retrieve data from the cache. Apache Ignite out of the box shipped with JDBC driver that allows you to connect to Ignite caches and retrieve distributed data from the cache using standard SQL queries. Rest of the section of this chapter will describe how to connect SQL IDE (Integrated Development Environment) to Ignite cache and executes some SQL queries to play with the data. SQL IDE or SQL editor can simplify the development process and allow you to get productive much quicker. Most database vendors have their own front-en

Load balancing and fail over with scheduler

Every programmer at least develop one Scheduler or Job in their life time of programming. Nowadays writing or developing scheduler to get you job done is very simple, but when you are thinking about high availability or load balancing your scheduler or job it getting some tricky. Even more when you have a few instance of your scheduler but only one can be run at a time also need some tricks to done. A long time ago i used some data base table lock to achieved such a functionality as leader election. Around 2010 when Zookeeper comes into play, i always preferred to use Zookeeper to bring high availability and scalability. For using Zookeeper you have to need Zookeeper cluster with minimum 3 nodes and maintain the cluster. Our new customer denied to use such a open source product in their environment and i was definitely need to find something alternative. Definitely Quartz was the next choose. Quartz makes developing scheduler easy and simple. Quartz clustering feature brings the HA and