Repository: ignite Updated Branches: refs/heads/master 24f908748 -> 3b314f725
IGNITE-7002 .NET: Add cross-platform examples on .NET Core This closes #3119 Project: http://git-wip-us.apache.org/repos/asf/ignite/repo Commit: http://git-wip-us.apache.org/repos/asf/ignite/commit/3b314f72 Tree: http://git-wip-us.apache.org/repos/asf/ignite/tree/3b314f72 Diff: http://git-wip-us.apache.org/repos/asf/ignite/diff/3b314f72 Branch: refs/heads/master Commit: 3b314f72512ca417cb08a079dcb6f1521b37a474 Parents: 24f9087 Author: Pavel Tupitsyn <[email protected]> Authored: Tue Dec 5 10:58:02 2017 +0300 Committer: Pavel Tupitsyn <[email protected]> Committed: Tue Dec 5 10:58:02 2017 +0300 ---------------------------------------------------------------------- modules/platforms/dotnet/examples/README.txt | 2 + .../dotnetcore/Apache.Ignite.Examples.csproj | 29 +++ .../dotnet/examples/dotnetcore/App.config | 41 ++++ .../dotnet/examples/dotnetcore/Employee.cs | 67 ++++++ .../dotnet/examples/dotnetcore/LinqExample.cs | 239 +++++++++++++++++++ .../dotnet/examples/dotnetcore/Organization.cs | 58 +++++ .../dotnet/examples/dotnetcore/Program.cs | 109 +++++++++ .../dotnet/examples/dotnetcore/PutGetExample.cs | 122 ++++++++++ .../dotnet/examples/dotnetcore/README.txt | 8 + .../dotnet/examples/dotnetcore/SqlExample.cs | 191 +++++++++++++++ .../dotnet/run-dotnetcore-examples.bat | 27 +++ 11 files changed, 893 insertions(+) ---------------------------------------------------------------------- http://git-wip-us.apache.org/repos/asf/ignite/blob/3b314f72/modules/platforms/dotnet/examples/README.txt ---------------------------------------------------------------------- diff --git a/modules/platforms/dotnet/examples/README.txt b/modules/platforms/dotnet/examples/README.txt index e39f48a..db38a35 100644 --- a/modules/platforms/dotnet/examples/README.txt +++ b/modules/platforms/dotnet/examples/README.txt @@ -1,6 +1,8 @@ Apache Ignite.NET Examples ================================== +Windows-only. See dotnetcore folder for cross-platform .NET-core based examples. + * Open Visual Studio solution %IGNITE_HOME%\platforms\dotnet\examples\Apache.Ignite.Examples.sln * Build Apache.Ignite.ExamplesDll project. * Set desired example as startup object in Apache.Ignite.Examples project and run it. \ No newline at end of file http://git-wip-us.apache.org/repos/asf/ignite/blob/3b314f72/modules/platforms/dotnet/examples/dotnetcore/Apache.Ignite.Examples.csproj ---------------------------------------------------------------------- diff --git a/modules/platforms/dotnet/examples/dotnetcore/Apache.Ignite.Examples.csproj b/modules/platforms/dotnet/examples/dotnetcore/Apache.Ignite.Examples.csproj new file mode 100644 index 0000000..9deb9fa --- /dev/null +++ b/modules/platforms/dotnet/examples/dotnetcore/Apache.Ignite.Examples.csproj @@ -0,0 +1,29 @@ +<Project Sdk="Microsoft.NET.Sdk"> + + <PropertyGroup> + <OutputType>Exe</OutputType> + <TargetFramework>netcoreapp2.0</TargetFramework> + <StartupObject>Apache.Ignite.Examples.Program</StartupObject> + </PropertyGroup> + + <PropertyGroup> + <NoWarn>NU1701</NoWarn> + </PropertyGroup> + + <ItemGroup> + <Reference Include="Apache.Ignite.Core"> + <HintPath Condition="Exists('..\..\Apache.Ignite')">..\..\Apache.Ignite\bin\$(Configuration)\Apache.Ignite.Core.dll</HintPath> + <HintPath Condition="Exists('..\..\bin\Apache.Ignite.Core.dll')">..\..\bin\Apache.Ignite.Core.dll</HintPath> + </Reference> + <Reference Include="Apache.Ignite.Linq"> + <HintPath Condition="Exists('..\..\Apache.Ignite.Linq')">..\..\Apache.Ignite.Linq\bin\$(Configuration)\Apache.Ignite.Linq.dll</HintPath> + <HintPath Condition="Exists('..\..\bin\Apache.Ignite.Linq.dll')">..\..\bin\Apache.Ignite.Linq.dll</HintPath> + </Reference> + </ItemGroup> + + <ItemGroup> + <PackageReference Include="System.Configuration.ConfigurationManager" Version="4.4.0" /> + <PackageReference Include="Remotion.Linq" Version="2.0.1" /> + </ItemGroup> + +</Project> http://git-wip-us.apache.org/repos/asf/ignite/blob/3b314f72/modules/platforms/dotnet/examples/dotnetcore/App.config ---------------------------------------------------------------------- diff --git a/modules/platforms/dotnet/examples/dotnetcore/App.config b/modules/platforms/dotnet/examples/dotnetcore/App.config new file mode 100644 index 0000000..e653d62 --- /dev/null +++ b/modules/platforms/dotnet/examples/dotnetcore/App.config @@ -0,0 +1,41 @@ +<?xml version="1.0" encoding="utf-8" ?> + +<!-- + Licensed to the Apache Software Foundation (ASF) under one or more + contributor license agreements. See the NOTICE file distributed with + this work for additional information regarding copyright ownership. + The ASF licenses this file to You under the Apache License, Version 2.0 + (the "License"); you may not use this file except in compliance with + the License. You may obtain a copy of the License at + + http://www.apache.org/licenses/LICENSE-2.0 + + Unless required by applicable law or agreed to in writing, software + distributed under the License is distributed on an "AS IS" BASIS, + WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + See the License for the specific language governing permissions and + limitations under the License. +--> + +<configuration> + <configSections> + <section name="igniteConfiguration" type="Apache.Ignite.Core.IgniteConfigurationSection, Apache.Ignite.Core" /> + </configSections> + + <runtime> + <gcServer enabled="true" /> + </runtime> + + <igniteConfiguration xmlns="http://ignite.apache.org/schema/dotnet/IgniteConfigurationSection" + localhost="127.0.0.1"> + <atomicConfiguration atomicSequenceReserveSize="10" /> + + <discoverySpi type="TcpDiscoverySpi"> + <ipFinder type="TcpDiscoveryMulticastIpFinder"> + <endpoints> + <string>127.0.0.1:47500..47502</string> + </endpoints> + </ipFinder> + </discoverySpi> + </igniteConfiguration> +</configuration> \ No newline at end of file http://git-wip-us.apache.org/repos/asf/ignite/blob/3b314f72/modules/platforms/dotnet/examples/dotnetcore/Employee.cs ---------------------------------------------------------------------- diff --git a/modules/platforms/dotnet/examples/dotnetcore/Employee.cs b/modules/platforms/dotnet/examples/dotnetcore/Employee.cs new file mode 100644 index 0000000..a8afaec --- /dev/null +++ b/modules/platforms/dotnet/examples/dotnetcore/Employee.cs @@ -0,0 +1,67 @@ +/* + * Licensed to the Apache Software Foundation (ASF) under one or more + * contributor license agreements. See the NOTICE file distributed with + * this work for additional information regarding copyright ownership. + * The ASF licenses this file to You under the Apache License, Version 2.0 + * (the "License"); you may not use this file except in compliance with + * the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +namespace Apache.Ignite.Examples +{ + using Core.Cache.Configuration; + + /// <summary> + /// Employee. + /// </summary> + public class Employee + { + /// <summary> + /// Constructor. + /// </summary> + /// <param name="name">Name.</param> + /// <param name="salary">Salary.</param> + /// <param name="organizationId">The organization identifier.</param> + public Employee(string name, long salary, int organizationId = 0) + { + Name = name; + Salary = salary; + OrganizationId = organizationId; + } + + /// <summary> + /// Name. + /// </summary> + [QuerySqlField] + public string Name { get; set; } + + /// <summary> + /// Organization id. + /// </summary> + [QuerySqlField(IsIndexed = true)] + public int OrganizationId { get; set; } + + /// <summary> + /// Salary. + /// </summary> + [QuerySqlField] + public long Salary { get; set; } + + /// <summary> + /// Returns a string that represents the current object. + /// </summary> + public override string ToString() + { + return $"{typeof(Employee).Name} [{nameof(Name)}={Name}, {nameof(Salary)}={Salary}, " + + $"{nameof(OrganizationId)}={OrganizationId}]"; + } + } +} http://git-wip-us.apache.org/repos/asf/ignite/blob/3b314f72/modules/platforms/dotnet/examples/dotnetcore/LinqExample.cs ---------------------------------------------------------------------- diff --git a/modules/platforms/dotnet/examples/dotnetcore/LinqExample.cs b/modules/platforms/dotnet/examples/dotnetcore/LinqExample.cs new file mode 100644 index 0000000..1132eb8 --- /dev/null +++ b/modules/platforms/dotnet/examples/dotnetcore/LinqExample.cs @@ -0,0 +1,239 @@ +/* + * Licensed to the Apache Software Foundation (ASF) under one or more + * contributor license agreements. See the NOTICE file distributed with + * this work for additional information regarding copyright ownership. + * The ASF licenses this file to You under the Apache License, Version 2.0 + * (the "License"); you may not use this file except in compliance with + * the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +namespace Apache.Ignite.Examples +{ + using System; + using System.Linq; + using Apache.Ignite.Core; + using Apache.Ignite.Core.Cache; + using Apache.Ignite.Core.Cache.Affinity; + using Apache.Ignite.Core.Cache.Configuration; + using Apache.Ignite.Core.Cache.Query; + using Apache.Ignite.Linq; + + /// <summary> + /// This example populates cache with sample data and runs several LINQ queries over this data. + /// </summary> + public class LinqExample + { + /// <summary>Organization cache name.</summary> + private const string OrganizationCacheName = "dotnet_cache_query_organization"; + + /// <summary>Employee cache name.</summary> + private const string EmployeeCacheName = "dotnet_cache_query_employee"; + + /// <summary>Colocated employee cache name.</summary> + private const string EmployeeCacheNameColocated = "dotnet_cache_query_employee_colocated"; + + [STAThread] + public static void Run() + { + var ignite = Ignition.TryGetIgnite() ?? Ignition.StartFromApplicationConfiguration(); + + Console.WriteLine(); + Console.WriteLine(">>> Cache LINQ example started."); + + var employeeCache = ignite.GetOrCreateCache<int, Employee>( + new CacheConfiguration(EmployeeCacheName, typeof(Employee))); + + var employeeCacheColocated = ignite.GetOrCreateCache<AffinityKey, Employee>( + new CacheConfiguration(EmployeeCacheNameColocated, typeof(Employee))); + + var organizationCache = ignite.GetOrCreateCache<int, Organization>( + new CacheConfiguration(OrganizationCacheName, new QueryEntity(typeof(int), typeof(Organization)))); + + // Populate cache with sample data entries. + PopulateCache(employeeCache); + PopulateCache(employeeCacheColocated); + PopulateCache(organizationCache); + + // Run SQL query example. + QueryExample(employeeCache); + + // Run compiled SQL query example. + CompiledQueryExample(employeeCache); + + // Run SQL query with join example. + JoinQueryExample(employeeCacheColocated, organizationCache); + + // Run SQL query with distributed join example. + DistributedJoinQueryExample(employeeCache, organizationCache); + + // Run SQL fields query example. + FieldsQueryExample(employeeCache); + } + + /// <summary> + /// Queries employees that have specific salary. + /// </summary> + /// <param name="cache">Cache.</param> + private static void QueryExample(ICache<int, Employee> cache) + { + const int minSalary = 10000; + + var qry = cache.AsCacheQueryable().Where(emp => emp.Value.Salary > minSalary); + + Console.WriteLine(); + Console.WriteLine($">>> Employees with salary > {minSalary}:"); + + foreach (var entry in qry) + Console.WriteLine(">>> " + entry.Value); + + Console.WriteLine(); + Console.WriteLine(">>> Generated SQL: " + qry.ToCacheQueryable().GetFieldsQuery().Sql); + } + + /// <summary> + /// Queries employees that have specific salary with a compiled query. + /// </summary> + /// <param name="cache">Cache.</param> + private static void CompiledQueryExample(ICache<int, Employee> cache) + { + const int minSalary = 10000; + + var cache0 = cache.AsCacheQueryable(); + + // Compile cache query to eliminate LINQ overhead on multiple runs. + Func<int, IQueryCursor<ICacheEntry<int, Employee>>> qry = + CompiledQuery.Compile((int ms) => cache0.Where(emp => emp.Value.Salary > ms)); + + Console.WriteLine(); + Console.WriteLine($">>> Employees with salary > {minSalary} using compiled query:"); + + foreach (var entry in qry(minSalary)) + Console.WriteLine(">>> " + entry.Value); + } + + /// <summary> + /// Queries employees that work for organization with provided name. + /// </summary> + /// <param name="employeeCache">Employee cache.</param> + /// <param name="organizationCache">Organization cache.</param> + private static void JoinQueryExample(ICache<AffinityKey, Employee> employeeCache, + ICache<int, Organization> organizationCache) + { + const string orgName = "Apache"; + + var employees = employeeCache.AsCacheQueryable(); + var organizations = organizationCache.AsCacheQueryable(); + + var qry = + from employee in employees + from organization in organizations + where employee.Value.OrganizationId == organization.Key && organization.Value.Name == orgName + select employee; + + + Console.WriteLine(); + Console.WriteLine(">>> Employees working for " + orgName + ":"); + + foreach (var entry in qry) + Console.WriteLine(">>> " + entry.Value); + + Console.WriteLine(); + Console.WriteLine(">>> Generated SQL: " + qry.ToCacheQueryable().GetFieldsQuery().Sql); + } + + /// <summary> + /// Queries employees that work for organization with provided name. + /// </summary> + /// <param name="employeeCache">Employee cache.</param> + /// <param name="organizationCache">Organization cache.</param> + private static void DistributedJoinQueryExample(ICache<int, Employee> employeeCache, + ICache<int, Organization> organizationCache) + { + const string orgName = "Apache"; + + var queryOptions = new QueryOptions {EnableDistributedJoins = true}; + + var employees = employeeCache.AsCacheQueryable(queryOptions); + var organizations = organizationCache.AsCacheQueryable(queryOptions); + + var qry = + from employee in employees + from organization in organizations + where employee.Value.OrganizationId == organization.Key && organization.Value.Name == orgName + select employee; + + + Console.WriteLine(); + Console.WriteLine(">>> Employees working for " + orgName + " (distributed joins):"); + + foreach (var entry in qry) + Console.WriteLine(">>> " + entry.Value); + } + + /// <summary> + /// Queries names and salaries for all employees. + /// </summary> + /// <param name="cache">Cache.</param> + private static void FieldsQueryExample(ICache<int, Employee> cache) + { + var qry = cache.AsCacheQueryable().Select(entry => new {entry.Value.Name, entry.Value.Salary}); + + Console.WriteLine(); + Console.WriteLine(">>> Employee names and their salaries:"); + + foreach (var row in qry) + Console.WriteLine(">>> [Name=" + row.Name + ", salary=" + row.Salary + ']'); + + Console.WriteLine(); + Console.WriteLine(">>> Generated SQL: " + qry.ToCacheQueryable().GetFieldsQuery().Sql); + } + + /// <summary> + /// Populate cache with data for this example. + /// </summary> + /// <param name="cache">Cache.</param> + private static void PopulateCache(ICache<int, Organization> cache) + { + cache.Put(1, new Organization("Apache")); + cache.Put(2, new Organization("Microsoft")); + } + + /// <summary> + /// Populate cache with data for this example. + /// </summary> + /// <param name="cache">Cache.</param> + private static void PopulateCache(ICache<AffinityKey, Employee> cache) + { + cache.Put(new AffinityKey(1, 1), new Employee("James Wilson", 12500, 1)); + cache.Put(new AffinityKey(2, 1), new Employee("Daniel Adams", 11000, 1)); + cache.Put(new AffinityKey(3, 1), new Employee("Cristian Moss", 12500, 1)); + cache.Put(new AffinityKey(4, 2), new Employee("Allison Mathis", 25300, 2)); + cache.Put(new AffinityKey(5, 2), new Employee("Breana Robbin", 6500, 2)); + cache.Put(new AffinityKey(6, 2), new Employee("Philip Horsley", 19800, 2)); + cache.Put(new AffinityKey(7, 2), new Employee("Brian Peters", 10600, 2)); + } + + /// <summary> + /// Populate cache with data for this example. + /// </summary> + /// <param name="cache">Cache.</param> + private static void PopulateCache(ICache<int, Employee> cache) + { + cache.Put(1, new Employee("James Wilson", 12500, 1)); + cache.Put(2, new Employee("Daniel Adams", 11000, 1)); + cache.Put(3, new Employee("Cristian Moss", 12500, 1)); + cache.Put(4, new Employee("Allison Mathis", 25300, 2)); + cache.Put(5, new Employee("Breana Robbin", 6500, 2)); + cache.Put(6, new Employee("Philip Horsley", 19800, 2)); + cache.Put(7, new Employee("Brian Peters", 10600, 2)); + } + } +} http://git-wip-us.apache.org/repos/asf/ignite/blob/3b314f72/modules/platforms/dotnet/examples/dotnetcore/Organization.cs ---------------------------------------------------------------------- diff --git a/modules/platforms/dotnet/examples/dotnetcore/Organization.cs b/modules/platforms/dotnet/examples/dotnetcore/Organization.cs new file mode 100644 index 0000000..5d09e6b --- /dev/null +++ b/modules/platforms/dotnet/examples/dotnetcore/Organization.cs @@ -0,0 +1,58 @@ +/* + * Licensed to the Apache Software Foundation (ASF) under one or more + * contributor license agreements. See the NOTICE file distributed with + * this work for additional information regarding copyright ownership. + * The ASF licenses this file to You under the Apache License, Version 2.0 + * (the "License"); you may not use this file except in compliance with + * the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +namespace Apache.Ignite.Examples +{ + using System; + using Core.Cache.Configuration; + + /// <summary> + /// Organization. + /// </summary> + public class Organization + { + /// <summary> + /// Constructor. + /// </summary> + /// <param name="name">Name.</param> + public Organization(string name) + { + Name = name; + LastUpdated = DateTime.UtcNow; + } + + /// <summary> + /// Name. + /// </summary> + [QuerySqlField(IsIndexed = true)] + public string Name { get; set; } + + /// <summary> + /// Last update time. + /// </summary> + [QuerySqlField] + public DateTime LastUpdated { get; set; } + + /// <summary> + /// Returns a string that represents the current object. + /// </summary> + public override string ToString() + { + return $"{typeof(Organization).Name} [{nameof(Name)}={Name}, {nameof(LastUpdated)}={LastUpdated}]"; + } + } +} http://git-wip-us.apache.org/repos/asf/ignite/blob/3b314f72/modules/platforms/dotnet/examples/dotnetcore/Program.cs ---------------------------------------------------------------------- diff --git a/modules/platforms/dotnet/examples/dotnetcore/Program.cs b/modules/platforms/dotnet/examples/dotnetcore/Program.cs new file mode 100644 index 0000000..58626df --- /dev/null +++ b/modules/platforms/dotnet/examples/dotnetcore/Program.cs @@ -0,0 +1,109 @@ +/* + * Licensed to the Apache Software Foundation (ASF) under one or more + * contributor license agreements. See the NOTICE file distributed with + * this work for additional information regarding copyright ownership. + * The ASF licenses this file to You under the Apache License, Version 2.0 + * (the "License"); you may not use this file except in compliance with + * the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +namespace Apache.Ignite.Examples +{ + using System; + + /// <summary> + /// Examples selector - program entry point. + /// </summary> + public static class Program + { + /// <summary> + /// Runs the program. + /// </summary> + [STAThread] + public static void Main(string[] args) + { + if (args.Length == 1 && args[0] == "--all") + { + Write("Running all examples unattended ..."); + + PutGetExample.Run(); + SqlExample.Run(); + LinqExample.Run(); + + return; + } + + while (true) + { + Write("======================================"); + Write("Welcome to Apache Ignite.NET Examples!"); + Write("Choose an example to run:"); + Write("1. Cache put-get"); + Write("2. SQL"); + Write("3. LINQ"); + Write("4. Exit examples"); + + switch (ReadNumber()) + { + case 1: + Write("Starting cache put-get example ..."); + PutGetExample.Run(); + break; + case 2: + Write("Starting SQL example ..."); + SqlExample.Run(); + break; + case 3: + Write("Starting LINQ example ..."); + LinqExample.Run(); + break; + case 4: + return; + } + + Write(); + Write("Example finished, press any key to continue ..."); + Console.ReadKey(); + } + } + + /// <summary> + /// Reads the number from console. + /// </summary> + private static int ReadNumber() + { + Write("Enter a number: "); + + while (true) + { + var input = Console.ReadLine(); + + if (!int.TryParse(input, out var id)) + { + Write("Not a number, try again: "); + } + else if (id < 1 || 4 < id) + { + Write("Out of range, try again: "); + } + else + { + return id; + } + } + } + + /// <summary> + /// Writes string to console. + /// </summary> + private static void Write(string s = null) => Console.WriteLine(s == null ? null : $">>> {s}"); + } +} \ No newline at end of file http://git-wip-us.apache.org/repos/asf/ignite/blob/3b314f72/modules/platforms/dotnet/examples/dotnetcore/PutGetExample.cs ---------------------------------------------------------------------- diff --git a/modules/platforms/dotnet/examples/dotnetcore/PutGetExample.cs b/modules/platforms/dotnet/examples/dotnetcore/PutGetExample.cs new file mode 100644 index 0000000..e3a1cbe --- /dev/null +++ b/modules/platforms/dotnet/examples/dotnetcore/PutGetExample.cs @@ -0,0 +1,122 @@ +/* + * Licensed to the Apache Software Foundation (ASF) under one or more + * contributor license agreements. See the NOTICE file distributed with + * this work for additional information regarding copyright ownership. + * The ASF licenses this file to You under the Apache License, Version 2.0 + * (the "License"); you may not use this file except in compliance with + * the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +namespace Apache.Ignite.Examples +{ + using System; + using System.Threading.Tasks; + using Core; + using Core.Binary; + + /// <summary> + /// This example demonstrates several put-get operations on Ignite cache + /// with binary values. Note that binary object can be retrieved in + /// fully-deserialized form or in binary object format using special + /// cache projection. + /// </summary> + public static class PutGetExample + { + /// <summary>Cache name.</summary> + private const string CacheName = "dotnet_cache_put_get"; + + /// <summary> + /// Runs the example. + /// </summary> + public static void Run() + { + var ignite = Ignition.TryGetIgnite() ?? Ignition.StartFromApplicationConfiguration(); + Console.WriteLine(); + Console.WriteLine(">>> Cache put-get example started."); + + // Clean up caches on all nodes before run. + ignite.GetOrCreateCache<object, object>(CacheName).Clear(); + + PutGet(ignite); + PutGetBinary(ignite); + PutGetAsync(ignite).Wait(); + } + + /// <summary> + /// Execute individual Put and Get. + /// </summary> + /// <param name="ignite">Ignite instance.</param> + private static void PutGet(IIgnite ignite) + { + var cache = ignite.GetCache<int, Organization>(CacheName); + + // Create new Organization to store in cache. + var org = new Organization("Microsoft"); + + // Put created data entry to cache. + cache.Put(1, org); + + // Get recently created employee as a strongly-typed fully de-serialized instance. + var orgFromCache = cache.Get(1); + + Console.WriteLine(); + Console.WriteLine(">>> Retrieved organization instance from cache: " + orgFromCache); + } + + /// <summary> + /// Execute individual Put and Get, getting value in binary format, without de-serializing it. + /// </summary> + /// <param name="ignite">Ignite instance.</param> + private static void PutGetBinary(IIgnite ignite) + { + var cache = ignite.GetCache<int, Organization>(CacheName); + + // Create new Organization to store in cache. + var org = new Organization("Microsoft"); + + // Put created data entry to cache. + cache.Put(1, org); + + // Create projection that will get values as binary objects. + var binaryCache = cache.WithKeepBinary<int, IBinaryObject>(); + + // Get recently created organization as a binary object. + var binaryOrg = binaryCache.Get(1); + + // Get organization's name from binary object (note that object doesn't need to be fully deserialized). + var name = binaryOrg.GetField<string>("name"); + + Console.WriteLine(); + Console.WriteLine(">>> Retrieved organization name from binary object: " + name); + } + + /// <summary> + /// Execute individual Put and Get. + /// </summary> + /// <param name="ignite">Ignite instance.</param> + private static async Task PutGetAsync(IIgnite ignite) + { + var cache = ignite.GetCache<int, Organization>(CacheName); + + // Create new Organization to store in cache. + var org = new Organization("Microsoft"); + + // Put created data entry to cache. + await cache.PutAsync(1, org); + + // Get recently created employee as a strongly-typed fully de-serialized instance. + var orgFromCache = await cache.GetAsync(1); + + Console.WriteLine(); + Console.WriteLine(">>> Retrieved organization instance from cache asynchronously: " + orgFromCache); + } + } +} http://git-wip-us.apache.org/repos/asf/ignite/blob/3b314f72/modules/platforms/dotnet/examples/dotnetcore/README.txt ---------------------------------------------------------------------- diff --git a/modules/platforms/dotnet/examples/dotnetcore/README.txt b/modules/platforms/dotnet/examples/dotnetcore/README.txt new file mode 100644 index 0000000..26c6c1f --- /dev/null +++ b/modules/platforms/dotnet/examples/dotnetcore/README.txt @@ -0,0 +1,8 @@ +Apache Ignite.NET Examples for .NET Core +======================================== + +Cross-platform examples, run on Windows, Linux, macOS. + + * Type "dotnet run" in terminal and hit Enter to run examples. + * Run ignite.bat / ignite.sh in a separate terminal(s) to run examples with standalone nodes. + * Or, run examples in multiple terminals simultaneously to have multiple nodes in a cluster. \ No newline at end of file http://git-wip-us.apache.org/repos/asf/ignite/blob/3b314f72/modules/platforms/dotnet/examples/dotnetcore/SqlExample.cs ---------------------------------------------------------------------- diff --git a/modules/platforms/dotnet/examples/dotnetcore/SqlExample.cs b/modules/platforms/dotnet/examples/dotnetcore/SqlExample.cs new file mode 100644 index 0000000..1fb2e90 --- /dev/null +++ b/modules/platforms/dotnet/examples/dotnetcore/SqlExample.cs @@ -0,0 +1,191 @@ +/* + * Licensed to the Apache Software Foundation (ASF) under one or more + * contributor license agreements. See the NOTICE file distributed with + * this work for additional information regarding copyright ownership. + * The ASF licenses this file to You under the Apache License, Version 2.0 + * (the "License"); you may not use this file except in compliance with + * the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +namespace Apache.Ignite.Examples +{ + using System; + using System.Collections; + using Apache.Ignite.Core; + using Apache.Ignite.Core.Cache; + using Apache.Ignite.Core.Cache.Affinity; + using Apache.Ignite.Core.Cache.Configuration; + using Apache.Ignite.Core.Cache.Query; + + /// <summary> + /// This example populates cache with sample data and runs several SQL and + /// full text queries over this data. + /// </summary> + public class SqlExample + { + /// <summary>Organization cache name.</summary> + private const string OrganizationCacheName = "dotnet_cache_query_organization"; + + /// <summary>Employee cache name.</summary> + private const string EmployeeCacheName = "dotnet_cache_query_employee"; + + /// <summary>Employee cache name.</summary> + private const string EmployeeCacheNameColocated = "dotnet_cache_query_employee_colocated"; + + [STAThread] + public static void Run() + { + var ignite = Ignition.TryGetIgnite() ?? Ignition.StartFromApplicationConfiguration(); + + Console.WriteLine(); + Console.WriteLine(">>> Cache query example started."); + + var employeeCache = ignite.GetOrCreateCache<int, Employee>( + new CacheConfiguration(EmployeeCacheName, typeof(Employee))); + + var employeeCacheColocated = ignite.GetOrCreateCache<AffinityKey, Employee>( + new CacheConfiguration(EmployeeCacheNameColocated, typeof(Employee))); + + var organizationCache = ignite.GetOrCreateCache<int, Organization>( + new CacheConfiguration(OrganizationCacheName, new QueryEntity(typeof(int), typeof(Organization)))); + + // Populate cache with sample data entries. + PopulateCache(employeeCache); + PopulateCache(employeeCacheColocated); + PopulateCache(organizationCache); + + // Run SQL query example. + SqlQueryExample(employeeCache); + + // Run SQL query with join example. + SqlJoinQueryExample(employeeCacheColocated); + + // Run SQL query with distributed join example. + SqlDistributedJoinQueryExample(employeeCache); + + // Run SQL fields query example. + SqlFieldsQueryExample(employeeCache); + } + + /// <summary> + /// Queries employees that have specified salary. + /// </summary> + /// <param name="cache">Cache.</param> + private static void SqlQueryExample(ICache<int, Employee> cache) + { + const int minSalary = 10000; + + var qry = cache.Query(new SqlQuery(typeof(Employee), "salary > ?", minSalary)); + + Console.WriteLine(); + Console.WriteLine($">>> Employees with salary > {minSalary} (SQL):"); + + foreach (var entry in qry) + Console.WriteLine(">>> " + entry.Value); + } + + /// <summary> + /// Queries employees that work for organization with provided name. + /// </summary> + /// <param name="cache">Cache.</param> + private static void SqlJoinQueryExample(ICache<AffinityKey, Employee> cache) + { + const string orgName = "Apache"; + + var qry = cache.Query(new SqlQuery("Employee", + "from Employee, \"dotnet_cache_query_organization\".Organization " + + "where Employee.organizationId = Organization._key and Organization.name = ?", orgName)); + + Console.WriteLine(); + Console.WriteLine($">>> Employees working for {orgName}:"); + + foreach (var entry in qry) + Console.WriteLine(">>> " + entry.Value); + } + + /// <summary> + /// Queries employees that work for organization with provided name. + /// </summary> + /// <param name="cache">Cache.</param> + private static void SqlDistributedJoinQueryExample(ICache<int, Employee> cache) + { + const string orgName = "Apache"; + + var qry = cache.Query(new SqlQuery("Employee", + "from Employee, \"dotnet_cache_query_organization\".Organization " + + "where Employee.organizationId = Organization._key and Organization.name = ?", orgName) + { + EnableDistributedJoins = true + }); + + Console.WriteLine(); + Console.WriteLine(">>> Employees working for " + orgName + " (distributed joins):"); + + foreach (var entry in qry) + Console.WriteLine(">>> " + entry.Value); + } + + /// <summary> + /// Queries names and salaries for all employees. + /// </summary> + /// <param name="cache">Cache.</param> + private static void SqlFieldsQueryExample(ICache<int, Employee> cache) + { + var qry = cache.QueryFields(new SqlFieldsQuery("select name, salary from Employee")); + + Console.WriteLine(); + Console.WriteLine(">>> Employee names and their salaries:"); + + foreach (IList row in qry) + Console.WriteLine($">>> [Name={row[0]}, salary={row[1]}{']'}"); + } + + /// <summary> + /// Populate cache with data for this example. + /// </summary> + /// <param name="cache">Cache.</param> + private static void PopulateCache(ICache<int, Organization> cache) + { + cache.Put(1, new Organization("Apache")); + cache.Put(2, new Organization("Microsoft")); + } + + /// <summary> + /// Populate cache with data for this example. + /// </summary> + /// <param name="cache">Cache.</param> + private static void PopulateCache(ICache<AffinityKey, Employee> cache) + { + cache.Put(new AffinityKey(1, 1), new Employee("James Wilson", 12500, 1)); + cache.Put(new AffinityKey(2, 1), new Employee("Daniel Adams", 11000, 1)); + cache.Put(new AffinityKey(3, 1), new Employee("Cristian Moss", 12500, 1)); + cache.Put(new AffinityKey(4, 2), new Employee("Allison Mathis", 25300, 2)); + cache.Put(new AffinityKey(5, 2), new Employee("Breana Robbin", 6500, 2)); + cache.Put(new AffinityKey(6, 2), new Employee("Philip Horsley", 19800, 2)); + cache.Put(new AffinityKey(7, 2), new Employee("Brian Peters", 10600, 2)); + } + + /// <summary> + /// Populate cache with data for this example. + /// </summary> + /// <param name="cache">Cache.</param> + private static void PopulateCache(ICache<int, Employee> cache) + { + cache.Put(1, new Employee("James Wilson", 12500, 1)); + cache.Put(2, new Employee("Daniel Adams", 11000, 1)); + cache.Put(3, new Employee("Cristian Moss", 12500, 1)); + cache.Put(4, new Employee("Allison Mathis", 25300, 2)); + cache.Put(5, new Employee("Breana Robbin", 6500, 2)); + cache.Put(6, new Employee("Philip Horsley", 19800, 2)); + cache.Put(7, new Employee("Brian Peters", 10600, 2)); + } + } +} http://git-wip-us.apache.org/repos/asf/ignite/blob/3b314f72/modules/platforms/dotnet/run-dotnetcore-examples.bat ---------------------------------------------------------------------- diff --git a/modules/platforms/dotnet/run-dotnetcore-examples.bat b/modules/platforms/dotnet/run-dotnetcore-examples.bat new file mode 100644 index 0000000..e16ec4e --- /dev/null +++ b/modules/platforms/dotnet/run-dotnetcore-examples.bat @@ -0,0 +1,27 @@ +:: +:: Licensed to the Apache Software Foundation (ASF) under one or more +:: contributor license agreements. See the NOTICE file distributed with +:: this work for additional information regarding copyright ownership. +:: The ASF licenses this file to You under the Apache License, Version 2.0 +:: (the "License"); you may not use this file except in compliance with +:: the License. You may obtain a copy of the License at +:: +:: http://www.apache.org/licenses/LICENSE-2.0 +:: +:: Unless required by applicable law or agreed to in writing, software +:: distributed under the License is distributed on an "AS IS" BASIS, +:: WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +:: See the License for the specific language governing permissions and +:: limitations under the License. +:: + +:: .NET Core examples runner (see examples/dotnetcore) +:: Build main solution before running examples (build.bat). +set IGNITE_NATIVE_TEST_CLASSPATH=true + +pushd . + +cd examples\dotnetcore +dotnet run %* + +popd \ No newline at end of file
