.Net Thin Client
Requirements
-
Supported runtimes: .NET 4.0+, .NET Core 2.0+
-
Supported OS: Windows, Linux, macOS (any OS supported by .NET Core 2.0+)
Installation
The thin client API is provided by the GridGain.NET API library, which is located in the {GRIDGAIN_HOME}/platforms/dotnet
directory of the GridGain distribution package.
The API is located in the Apache.Ignite.Core
assembly.
Connecting to Cluster
The thin client API entry point is the Ignition.StartClient(IgniteClientConfiguration)
method. The IgniteClientConfiguration.Host
property is mandatory; it should point to the host where the server node is running.
var cfg = new IgniteClientConfiguration
{
Endpoints = new[] {"127.0.0.1:10800"}
};
using (var client = Ignition.StartClient(cfg))
{
var cache = client.GetOrCreateCache<int, string>("cache");
cache.Put(1, "Hello, World!");
}
Handling node failures:
var scanQry = new ScanQuery<int, Person>(new NameFilter());
using (var cur = cache.Query(scanQry))
{
var res = cur.GetAll().ToDictionary(entry => entry.Key, entry => entry.Value);
}
Using Key-Value API
Getting Cache Instance
The key-value API is provided by the ICacheClient
interface. You can use the following methods to obtain an instance of ICacheClient
:
-
GetCache(cacheName) — returns an instance of an existing cache.
-
CreateCache(cacheName) — creates a cache with the given name.
-
GetOrCreateCache(CacheClientConfiguration) — gets or creates a cache with the given configuration.
var cacheCfg = new CacheClientConfiguration
{
Name = "References",
CacheMode = CacheMode.Replicated,
WriteSynchronizationMode = CacheWriteSynchronizationMode.FullSync
};
var cache = client.GetOrCreateCache<int, string>(cacheCfg);
Use IIgniteClient.GetCacheNames()
to obtain a list of all existing caches.
Basic Operations
The following code snippet demonstrates how to execute basic cache operations on a specific cache.
var data = Enumerable.Range(1, 100).ToDictionary(e => e, e => e.ToString());
cache.PutAll(data);
var replace = cache.Replace(1, "2", "3");
Console.WriteLine(replace); //false
var value = cache.Get(1);
Console.WriteLine(value); //1
replace = cache.Replace(1, "1", "3");
Console.WriteLine(replace); //true
value = cache.Get(1);
Console.WriteLine(value); //3
cache.Put(101, "101");
cache.RemoveAll(data.Keys);
var sizeIsOne = cache.GetSize() == 1;
Console.WriteLine(sizeIsOne); //true
value = cache.Get(101);
Console.WriteLine(value); //101
cache.RemoveAll();
var sizeIsZero = cache.GetSize() == 0;
Console.WriteLine(sizeIsZero); //true
Working With Binary Objects
The thin client supports Binary Object API described in the Working with Binary Objects section. Use IClientCache.WithKeepBinary()
to switch the cache to binary mode and start working directly with binary objects avoiding serialization/deserialization. Use IIgniteClient.GetBinary()
to get an instance of IBinary
and build an object from scratch.
var binary = client.GetBinary();
var val = binary.GetBuilder("Person")
.SetField("id", 1)
.SetField("name", "Joe")
.Build();
var cache = client.GetOrCreateCache<int, object>("persons").WithKeepBinary<int, IBinaryObject>();
cache.Put(1, val);
var value = cache.Get(1);
Scan Queries
Use a scan query to get a set of entries that satisfy a given condition. The thin client sends the query to the cluster node where it is executed as a normal scan query.
The query condition is specified by an ICacheEntryFilter
object that is passed to the query constructor as an argument.
Define a query filter as follows:
class NameFilter : ICacheEntryFilter<int, Person>
{
public bool Invoke(ICacheEntry<int, Person> entry)
{
return entry.Value.Name.Contains("Smith");
}
}
Then execute the scan query:
var cache = client.GetOrCreateCache<int, Person>("personCache");
cache.Put(1, new Person {Name = "John Smith"});
cache.Put(2, new Person {Name = "John Johnson"});
using (var cursor = cache.Query(new ScanQuery<int, Person>(new NameFilter())))
{
foreach (var entry in cursor)
{
Console.WriteLine("Key = " + entry.Key + ", Name = " + entry.Value.Name);
}
}
Executing SQL Statements
The thin client provides a SQL API to execute SQL statements. SQL statements are declared using SqlFieldsQuery
objects and executed through the IIgniteClient.query(SqlFieldsQuery)
method.
var cache = client.GetOrCreateCache<int, Person>("Person");
cache.Query(new SqlFieldsQuery(
$"CREATE TABLE IF NOT EXISTS Person (id INT PRIMARY KEY, name VARCHAR) WITH \"VALUE_TYPE={typeof(Person)}\"")
{Schema = "PUBLIC"}).GetAll();
var key = 1;
var val = new Person {Id = key, Name = "Person 1"};
cache.Query(
new SqlFieldsQuery("INSERT INTO Person(id, name) VALUES(?, ?)")
{
Arguments = new object[] {val.Id, val.Name},
Schema = "PUBLIC"
}
).GetAll();
var cursor = cache.Query(
new SqlFieldsQuery("SELECT name FROM Person WHERE id = ?")
{
Arguments = new object[] {key},
Schema = "PUBLIC"
}
);
var results = cursor.GetAll();
var first = results.FirstOrDefault();
if (first != null)
{
Console.WriteLine("name = " + first[0]);
}
Security
SSL/TLS
To use encrypted communication between the thin client and the cluster, you have to enable it both in the cluster configuration and the client configuration. Refer to the Enabling SSL/TLS for Thin Clients section for the instruction on the cluster configuration.
The following code example demonstrates how to configure SSL parameters in the thin client.
var cfg = new IgniteClientConfiguration
{
Endpoints = new[] {"127.0.0.1:10800"},
SslStreamFactory = new SslStreamFactory
{
CertificatePath = ".../certs/client.pfx",
CertificatePassword = "password",
}
};
using (var client = Ignition.StartClient(cfg))
{
//...
}
Authentication
Configure authentication on the cluster side and provide a valid user name and password in the client configuration.
var cfg = new IgniteClientConfiguration
{
Endpoints = new[] {"127.0.0.1:10800"},
UserName = "gridgain",
Password = "gridgain"
};
using (var client = Ignition.StartClient(cfg))
{
//...
}
Authorization
Thin client authorization can be configured in the cluster. Refer to the Authorization page for details.
© 2020 GridGain Systems, Inc. All Rights Reserved. Privacy Policy | Legal Notices. GridGain® is a registered trademark of GridGain Systems, Inc.
Apache, Apache Ignite, the Apache feather and the Apache Ignite logo are either registered trademarks or trademarks of The Apache Software Foundation.