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
Please enter at least three characters to search
Refcards Trend Reports
Events Video Library
Refcards
Trend Reports

Events

View Events Video Library

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

Because the DevOps movement has redefined engineering responsibilities, SREs now have to become stewards of observability strategy.

Apache Cassandra combines the benefits of major NoSQL databases to support data management needs not covered by traditional RDBMS vendors.

The software you build is only as secure as the code that powers it. Learn how malicious code creeps into your software supply chain.

Generative AI has transformed nearly every industry. How can you leverage GenAI to improve your productivity and efficiency?

Related

  • Resolving Parameter Sensitivity With Parameter Sensitive Plan Optimization in SQL Server 2022
  • Useful System Table Queries in Relational Databases
  • How to Restore a Transaction Log Backup in SQL Server
  • How to Attach SQL Database Without a Transaction Log File

Trending

  • Designing for Sustainability: The Rise of Green Software
  • Mastering Advanced Traffic Management in Multi-Cloud Kubernetes: Scaling With Multiple Istio Ingress Gateways
  • Designing AI Multi-Agent Systems in Java
  • Power BI Embedded Analytics — Part 3: Power BI Embedded Demo
  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 user avatar
Ayberk Cansever
·
May. 29, 17 · Tutorial
Likes (4)
Comment
Save
Tweet
Share
6.5K 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.

Related

  • Resolving Parameter Sensitivity With Parameter Sensitive Plan Optimization in SQL Server 2022
  • Useful System Table Queries in Relational Databases
  • How to Restore a Transaction Log Backup in SQL Server
  • How to Attach SQL Database Without a Transaction Log File

Partner Resources

×

Comments
Oops! Something Went Wrong

The likes didn't load as expected. Please refresh the page and try again.

ABOUT US

  • About DZone
  • Support and feedback
  • Community research
  • Sitemap

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 100
  • Nashville, TN 37211
  • support@dzone.com

Let's be friends:

Likes
There are no likes...yet! 👀
Be the first to like this post!
It looks like you're not logged in.
Sign in to see who liked this post!