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
Over 2 million developers have joined DZone.
Log In / Join
Refcards Trend Reports Events Over 2 million developers have joined DZone. Join Today! Thanks for visiting DZone today,
Edit Profile Manage Email Subscriptions Moderation Admin Console How to Post to DZone Article Submission Guidelines
View Profile
Sign Out
Refcards
Trend Reports
Events
Zones
Culture and Methodologies Agile Career Development Methodologies Team Management
Data Engineering AI/ML Big Data Data Databases IoT
Software Design and Architecture Cloud Architecture Containers Integration Microservices Performance Security
Coding Frameworks Java JavaScript Languages Tools
Testing, Deployment, and Maintenance Deployment DevOps and CI/CD Maintenance Monitoring and Observability Testing, Tools, and Frameworks
Culture and Methodologies
Agile Career Development Methodologies Team Management
Data Engineering
AI/ML Big Data Data Databases IoT
Software Design and Architecture
Cloud Architecture Containers Integration Microservices Performance Security
Coding
Frameworks Java JavaScript Languages Tools
Testing, Deployment, and Maintenance
Deployment DevOps and CI/CD Maintenance Monitoring and Observability Testing, Tools, and Frameworks
  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.

Ayberk Cansever user avatar by
Ayberk Cansever
·
May. 29, 17 · Tutorial
Like (4)
Save
Tweet
Share
5.85K Views

Join the DZone community and get the full member experience.

Join For Free

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.

Popular on DZone

  • Hidden Classes in Java 15
  • SAST: How Code Analysis Tools Look for Security Flaws
  • What Is Policy-as-Code? An Introduction to Open Policy Agent
  • Mr. Over, the Engineer [Comic]

Comments

Partner Resources

X

ABOUT US

  • About DZone
  • Send feedback
  • Careers
  • Sitemap

ADVERTISE

  • Advertise with DZone

CONTRIBUTE ON DZONE

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

LEGAL

  • Terms of Service
  • Privacy Policy

CONTACT US

  • 600 Park Offices Drive
  • Suite 300
  • Durham, NC 27709
  • support@dzone.com
  • +1 (919) 678-0300

Let's be friends: