Over a million developers have joined DZone.

Apache Ignite: QueryEntity and Basic SQL Query With C# Client

DZone's Guide to

Apache Ignite: QueryEntity and Basic SQL Query With C# Client

In this article, we will configure and run a simple Apache Ignite instance, connect it with a C# client, and query a cache with SQL.

· Database Zone
Free Resource

Whether you work in SQL Server Management Studio or Visual Studio, Redgate tools integrate with your existing infrastructure, enabling you to align DevOps for your applications with DevOps for your SQL Server databases. Discover true Database DevOps, brought to you in partnership with Redgate.

We have lots of distributed cache solutions in our bags: Apache Ignite, Hazelcast, Oracle Coherence, JCS, Ehcache, etc.

In this article, we will configure and run a simple Apache Ignite instance, connect it with a C# client, and query a cache with SQL.

1. Configure and Run an Ignite Instance

After downloading Apache Ignite, edit the configuration file named default-config.xml, which stands in config directory:

<?xml version="1.0" encoding="UTF-8"?>
	<bean id="ignite.cfg" class="org.apache.ignite.configuration.IgniteConfiguration">
		<property name="clientMode" value="false"/>
		<property name="peerClassLoadingEnabled" value="true"/>
		<property name="discoverySpi">
			<bean class="org.apache.ignite.spi.discovery.tcp.TcpDiscoverySpi">
				<property name="ipFinder">
					<bean class="org.apache.ignite.spi.discovery.tcp.ipfinder.vm.TcpDiscoveryVmIpFinder">
						<property name="addresses">
								<!-- In distributed environment, replace with actual host IP address. -->

As seen above, we disable the clientMode and enable peerClassLoading. We enable peerClassLoading for dynamic class loading. For further information about thepeerClassLoadingEnabled property, please take a look at the documentation.

Then make some tuning about the JVM parameters if you need. You can find them in the bin/ignite.bat file.

We will enable the H2 debug console by adding this line to the BAT file:


We are ready to start our simple Ignite instance. Run the batch file and watch the logs.

Image title

The H2 debug console is enabled, as you can see in the log.

2. Define Your SQL-Queryable Cache and Object in Your Code

public class Seat: IBinarizable {
 [QuerySqlField(IsIndexed = true)]
 public int SeatId {

 public int ActId {

 public int SectionId {

 public int BlockId {

 public int RowId {

 public int SeatNo {

 public Seat(int seatId, int actId, int sectionId, int blockId, int rowId, int seatNo) {
  SeatId = seatId;
  ActId = actId;
  SectionId = sectionId;
  BlockId = blockId;
  RowId = rowId;
  SeatNo = seatNo;

 public Seat() {}

 public void WriteBinary(IBinaryWriter writer) {
  writer.WriteInt("SeatId", SeatId);
  writer.WriteInt("ActId", ActId);
  writer.WriteInt("SectionId", SectionId);
  writer.WriteInt("BlockId", BlockId);
  writer.WriteInt("RowId", RowId);
  writer.WriteInt("SeatNo", SeatNo);

 public void ReadBinary(IBinaryReader reader) {
  SeatId = reader.ReadInt("SeatId");
  ActId = reader.ReadInt("ActId");
  SectionId = reader.ReadInt("SectionId");
  BlockId = reader.ReadInt("BlockId");
  RowId = reader.ReadInt("RowId");
  SeatNo = reader.ReadInt("SeatNo");

We can see that the Seat class implements the IBinarizable interface and the SeatId field is annotated with QuerySqlField. Also, the IsIndexed parameter of the annotation is set to true for indexing the field.                                                

3. Start the Ignite Client With C#

IIgnite ignite = Ignition.Start("default-config-client.xml");
ICache < string, Seat > RowCache = ignite.GetOrCreateCache < string, Seat > (
 new CacheConfiguration("Row", new QueryEntity(typeof(string), typeof(Seat))));

default-client-config.xml is the copy of the default-config.xml but the clientMode property is set to true.

This is important: We are creating the cache with the QueryEntity configuration. The query entity is a description of cache entry (composed of key and value) in a way of how it must be indexed and can be queried. 

After running the ignite C# client, we look at the H2 debug console. We see the SQL-queryable table that we created:

Image title

When we fill the cache with the seat entities, the table is loaded and the SeatId field is indexed. The table has _KEY and _VAL columns for us:

Image title

We can query the cache with the C# client as:

var sql = new SqlQuery(typeof(Seat), "where SeatId > ?", 99990);
IQueryCursor<ICacheEntry<string, Seat>> queryCursor = RowCache.Query(sql);
foreach (ICacheEntry<string, Seat> entry in queryCursor)

This code block will flash the SeatIds greater than 99990:

Image title

Be careful while determining which fields of your objects can be queried and indexed. The unnecessary indexes will cause wasting the memory.

It’s easier than you think to extend DevOps practices to SQL Server with Redgate tools. Discover how to introduce true Database DevOps, brought to you in partnership with Redgate

apache ignite ,queries ,sql ,c# ,database ,tutorial

Opinions expressed by DZone contributors are their own.

{{ parent.title || parent.header.title}}

{{ parent.tldr }}

{{ parent.urlSource.name }}