DZone
Thanks for visiting DZone today,
Edit Profile
  • Manage Email Subscriptions
  • How to Post to DZone
  • Article Submission Guidelines
Sign Out View Profile
  • Post an Article
  • Manage My Drafts
Newsletter
Log In / Join
Refcards Trend Reports
Events Video Library
Refcards
Trend Reports

Events

View Events Video Library

Related

  • PostgreSQL vs Oracle: When Higher Cost Means Better Performance
  • Grounding AI Agents in Governed Data
  • Engineering Self-Healing SQL Pipelines With LLMs: Validation, Guardrails, and Safe Recovery
  • How I Built a SQL Diagnostic Tool That Works Without Touching Your Database

Trending

  • Build Software Faster With Three Simple Principles
  • Mistaking Code Production for Engineering Progress: AI Productivity Myths Part 1
  • Building a Practical Cloud-Native Golden Path: A Guide to Kubernetes-Based Service Delivery, Self-Service, and Developer-Friendly Defaults
  • Introducing senro: A Pipeline Engine You Program in Go

Draft This article hasn't been published, so readers can't see it.

In moderation This article is waiting for review, so readers can't see it yet.

Deleted This article has been deleted, so readers can't see it.

  1. DZone
  2. Data Engineering
  3. Databases
  4. Apache Ignite: QueryEntity and Basic SQL Query With C# Client

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.

By 
Ayberk Cansever
·
May. 29, 17 · Tutorial
Post on X (opens in a new tab) Share on LinkedIn (opens in a new tab)
6.9K Views

Join the DZone community Save articles, join the discussion, and download Refcards and Trend Reports with a free account.

Join for free Already a member? Log in

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"?>
<beans
	xmlns="http://www.springframework.org/schema/beans"
	xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
       xsi:schemaLocation="
       http://www.springframework.org/schema/beans
       http://www.springframework.org/schema/beans/spring-beans.xsd">
	<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">
							<list>
								<!-- In distributed environment, replace with actual host IP address. -->
								<value>127.0.0.1:47500..47509</value>
							</list>
						</property>
					</bean>
				</property>
			</bean>
		</property>
	</bean>
</beans>

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:

set IGNITE_H2_DEBUG_CONSOLE=true

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 {
  set;
  get;
 }

 public int ActId {
  set;
  get;
 }

 public int SectionId {
  set;
  get;
 }

 public int BlockId {
  set;
  get;
 }

 public int RowId {
  set;
  get;
 }

 public int SeatNo {
  set;
  get;
 }

 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)
Console.WriteLine(entry.Value.SeatId);

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.

Database Apache Ignite sql

Opinions expressed by DZone contributors are their own.

Related

  • PostgreSQL vs Oracle: When Higher Cost Means Better Performance
  • Grounding AI Agents in Governed Data
  • Engineering Self-Healing SQL Pipelines With LLMs: Validation, Guardrails, and Safe Recovery
  • How I Built a SQL Diagnostic Tool That Works Without Touching Your Database

Partner Resources

×

Comments

Loading comments

The comments didn't load

Please try again in a moment.

Likes

Loading

The next likes didn't load. Please try again.

The likes didn't load

Please try again in a moment.

See who liked this

Log in or join DZone for free to see who liked this article.

Log in Join for free
  • RSS
  • X
  • Facebook

ABOUT US

  • About DZone
  • Support and feedback
  • Community research

ADVERTISE

  • Advertise with DZone

CONTRIBUTE ON DZONE

  • Article Submission Guidelines
  • Become a Contributor
  • Core Program
  • Visit the Writers' Zone

LEGAL

  • Terms of Service
  • Privacy Policy

CONTACT US

  • 3343 Perimeter Hill Drive
  • Suite 215
  • Nashville, TN 37211
  • [email protected]

Let's be friends:

  • RSS
  • X
  • Facebook