You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@ignite.apache.org by yz...@apache.org on 2017/01/23 10:41:56 UTC

[03/10] ignite git commit: IGNITE-4556 .NET: DML example

IGNITE-4556 .NET: DML example

This closes #1439


Project: http://git-wip-us.apache.org/repos/asf/ignite/repo
Commit: http://git-wip-us.apache.org/repos/asf/ignite/commit/19a7e969
Tree: http://git-wip-us.apache.org/repos/asf/ignite/tree/19a7e969
Diff: http://git-wip-us.apache.org/repos/asf/ignite/diff/19a7e969

Branch: refs/heads/ignite-comm-balance-master
Commit: 19a7e969434c7de8da4e7a4f64d73d4af364ca38
Parents: 664dc88
Author: Pavel Tupitsyn <pt...@apache.org>
Authored: Fri Jan 20 11:51:46 2017 +0300
Committer: Pavel Tupitsyn <pt...@apache.org>
Committed: Fri Jan 20 11:51:46 2017 +0300

----------------------------------------------------------------------
 .../Apache.Ignite.Examples.csproj               |   1 +
 .../Datagrid/QueryDmlExample.cs                 | 159 +++++++++++++++++++
 2 files changed, 160 insertions(+)
----------------------------------------------------------------------


http://git-wip-us.apache.org/repos/asf/ignite/blob/19a7e969/modules/platforms/dotnet/examples/Apache.Ignite.Examples/Apache.Ignite.Examples.csproj
----------------------------------------------------------------------
diff --git a/modules/platforms/dotnet/examples/Apache.Ignite.Examples/Apache.Ignite.Examples.csproj b/modules/platforms/dotnet/examples/Apache.Ignite.Examples/Apache.Ignite.Examples.csproj
index 3f7e1dc..ebf9e92 100644
--- a/modules/platforms/dotnet/examples/Apache.Ignite.Examples/Apache.Ignite.Examples.csproj
+++ b/modules/platforms/dotnet/examples/Apache.Ignite.Examples/Apache.Ignite.Examples.csproj
@@ -62,6 +62,7 @@
     <Compile Include="Datagrid\PutGetExample.cs" />
     <Compile Include="Datagrid\LinqExample.cs" />
     <Compile Include="Datagrid\BinaryModeExample.cs" />
+    <Compile Include="Datagrid\QueryDmlExample.cs" />
     <Compile Include="Datagrid\QueryExample.cs" />
     <Compile Include="Datagrid\StoreExample.cs" />
     <Compile Include="Datagrid\TransactionDeadlockDetectionExample.cs" />

http://git-wip-us.apache.org/repos/asf/ignite/blob/19a7e969/modules/platforms/dotnet/examples/Apache.Ignite.Examples/Datagrid/QueryDmlExample.cs
----------------------------------------------------------------------
diff --git a/modules/platforms/dotnet/examples/Apache.Ignite.Examples/Datagrid/QueryDmlExample.cs b/modules/platforms/dotnet/examples/Apache.Ignite.Examples/Datagrid/QueryDmlExample.cs
new file mode 100644
index 0000000..b264a0e
--- /dev/null
+++ b/modules/platforms/dotnet/examples/Apache.Ignite.Examples/Datagrid/QueryDmlExample.cs
@@ -0,0 +1,159 @@
+/*
+ * 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.Datagrid
+{
+    using System;
+    using Apache.Ignite.Core;
+    using Apache.Ignite.Core.Cache;
+    using Apache.Ignite.Core.Cache.Configuration;
+    using Apache.Ignite.Core.Cache.Query;
+    using Apache.Ignite.ExamplesDll.Binary;
+
+    /// <summary>
+    /// This example showcases DML capabilities of Ignite's SQL engine.
+    /// <para />
+    /// 1) Build the project Apache.Ignite.ExamplesDll (select it -> right-click -> Build).
+    ///    Apache.Ignite.ExamplesDll.dll must appear in %IGNITE_HOME%/platforms/dotnet/examples/Apache.Ignite.ExamplesDll/bin/${Platform]/${Configuration} folder.
+    /// 2) Set this class as startup object (Apache.Ignite.Examples project -> right-click -> Properties ->
+    ///     Application -> Startup object);
+    /// 3) Start example (F5 or Ctrl+F5).
+    /// <para />
+    /// This example can be run with standalone Apache Ignite.NET node:
+    /// 1) Run %IGNITE_HOME%/platforms/dotnet/bin/Apache.Ignite.exe:
+    /// Apache.Ignite.exe -configFileName=platforms\dotnet\examples\apache.ignite.examples\app.config -assembly=[path_to_Apache.Ignite.ExamplesDll.dll]
+    /// 2) Start example.
+    /// </summary>
+    public class QueryDmlExample
+    {
+        /// <summary>Organization cache name.</summary>
+        private const string OrganizationCacheName = "dotnet_cache_query_dml_organization";
+
+        /// <summary>Employee cache name.</summary>
+        private const string EmployeeCacheName = "dotnet_cache_query_dml_employee";
+
+        [STAThread]
+        public static void Main()
+        {
+            using (var ignite = Ignition.StartFromApplicationConfiguration())
+            {
+                Console.WriteLine();
+                Console.WriteLine(">>> Cache query DML example started.");
+
+                var employeeCache = ignite.GetOrCreateCache<int, Employee>(
+                    new CacheConfiguration(EmployeeCacheName, new QueryEntity(typeof(int), typeof(Employee))));
+
+                var organizationCache = ignite.GetOrCreateCache<int, Organization>(new CacheConfiguration(
+                    OrganizationCacheName, new QueryEntity(typeof(int), typeof(Organization))));
+
+                Insert(organizationCache, employeeCache);
+                Select(employeeCache, "Inserted data");
+
+                Update(employeeCache);
+                Select(employeeCache, "Update salary for ASF employees");
+
+                Delete(employeeCache);
+                Select(employeeCache, "Delete non-ASF employees");
+
+                Console.WriteLine();
+            }
+
+            Console.WriteLine();
+            Console.WriteLine(">>> Example finished, press any key to exit ...");
+            Console.ReadKey();
+        }
+
+        /// <summary>
+        /// Selects and displays Employee data.
+        /// </summary>
+        /// <param name="employeeCache">Employee cache.</param>
+        /// <param name="message">Message.</param>
+        private static void Select(ICache<int, Employee> employeeCache, string message)
+        {
+            Console.WriteLine("\n>>> {0}", message);
+
+            var qry = new SqlFieldsQuery(string.Format(
+                "select emp._key, emp.name, org.name, emp.salary " +
+                "from Employee as emp, " +
+                "\"{0}\".Organization as org " +
+                "where emp.organizationId = org._key", OrganizationCacheName));
+
+            using (var cursor = employeeCache.QueryFields(qry))
+            {
+                foreach (var row in cursor)
+                {
+                    Console.WriteLine(">>> {0}: {1}, {2}, {3}", row[0], row[1], row[2], row[3]);
+                }
+            }
+        }
+
+        /// <summary>
+        /// Populate cache with test data.
+        /// </summary>
+        /// <param name="organizationCache">Organization cache.</param>
+        /// <param name="employeeCache">Employee cache.</param>
+        private static void Insert(ICache<int, Organization> organizationCache, ICache<int, Employee> employeeCache)
+        {
+            // Insert organizations.
+            var qry = new SqlFieldsQuery("insert into Organization (_key, name) values (?, ?)", 1, "ASF");
+            organizationCache.QueryFields(qry);
+
+            qry.Arguments = new object[] {2, "Eclipse"};
+            organizationCache.QueryFields(qry);
+
+            // Insert employees.
+            qry = new SqlFieldsQuery("insert into Employee (_key, name, organizationId, salary) values (?, ?, ?, ?)");
+
+            qry.Arguments = new object[] {1, "John Doe", 1, 4000};
+            employeeCache.QueryFields(qry);
+
+            qry.Arguments = new object[] {2, "Jane Roe", 1, 5000};
+            employeeCache.QueryFields(qry);
+
+            qry.Arguments = new object[] {3, "Mary Major", 2, 2000};
+            employeeCache.QueryFields(qry);
+
+            qry.Arguments = new object[] {4, "Richard Miles", 2, 3000};
+            employeeCache.QueryFields(qry);
+        }
+
+        /// <summary>
+        /// Conditional UPDATE query: raise salary for ASF employees.
+        /// </summary>
+        /// <param name="employeeCache">Employee cache.</param>
+        private static void Update(ICache<int, Employee> employeeCache)
+        {
+            var qry = new SqlFieldsQuery("update Employee set salary = salary * 1.1 where organizationId = ?", 1);
+
+            employeeCache.QueryFields(qry);
+        }
+
+        /// <summary>
+        /// Conditional DELETE query: remove non-ASF employees.
+        /// </summary>
+        /// <param name="employeeCache">Employee cache.</param>
+        private static void Delete(ICache<int, Employee> employeeCache)
+        {
+            var qry = new SqlFieldsQuery(string.Format(
+                "delete from Employee where _key in (" +
+                "select emp._key from Employee emp, \"{0}\".Organization org " +
+                "where org.Name != ? and org._key = emp.organizationId)", OrganizationCacheName), "ASF");
+
+            employeeCache.QueryFields(qry);
+        }
+    }
+}