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.
Join the DZone community and get the full member experience.
Join For FreeWe 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.
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:
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:
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 SeatId
s greater than 99990:
Be careful while determining which fields of your objects can be queried and indexed. The unnecessary indexes will cause wasting the memory.
Opinions expressed by DZone contributors are their own.
Comments