You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@ignite.apache.org by vo...@apache.org on 2016/12/05 12:38:46 UTC

[42/52] ignite git commit: IGNITE-4017: Added DML example. This closes #1293.

IGNITE-4017: Added DML example. This closes #1293.


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

Branch: refs/heads/master
Commit: e205214ab4680f3b873a5cd4cb41ce877d2d3f17
Parents: a53fd38
Author: Alexander Paschenko <al...@gmail.com>
Authored: Mon Nov 28 15:51:24 2016 +0300
Committer: devozerov <vo...@gridgain.com>
Committed: Mon Nov 28 15:51:24 2016 +0300

----------------------------------------------------------------------
 .../examples/datagrid/CacheQueryDmlExample.java | 163 +++++++++++++++++++
 .../ignite/examples/CacheExamplesSelfTest.java  |   8 +
 2 files changed, 171 insertions(+)
----------------------------------------------------------------------


http://git-wip-us.apache.org/repos/asf/ignite/blob/e205214a/examples/src/main/java/org/apache/ignite/examples/datagrid/CacheQueryDmlExample.java
----------------------------------------------------------------------
diff --git a/examples/src/main/java/org/apache/ignite/examples/datagrid/CacheQueryDmlExample.java b/examples/src/main/java/org/apache/ignite/examples/datagrid/CacheQueryDmlExample.java
new file mode 100644
index 0000000..21027d0
--- /dev/null
+++ b/examples/src/main/java/org/apache/ignite/examples/datagrid/CacheQueryDmlExample.java
@@ -0,0 +1,163 @@
+/*
+ * 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.
+ */
+
+package org.apache.ignite.examples.datagrid;
+
+import org.apache.ignite.Ignite;
+import org.apache.ignite.IgniteCache;
+import org.apache.ignite.Ignition;
+import org.apache.ignite.cache.query.SqlFieldsQuery;
+import org.apache.ignite.configuration.CacheConfiguration;
+import org.apache.ignite.examples.model.Organization;
+import org.apache.ignite.examples.model.Person;
+
+import java.util.List;
+
+/**
+ * Example to showcase DML capabilities of Ignite's SQL engine.
+ */
+public class CacheQueryDmlExample {
+    /** Organizations cache name. */
+    private static final String ORG_CACHE = CacheQueryDmlExample.class.getSimpleName() + "Organizations";
+
+    /** Persons cache name. */
+    private static final String PERSON_CACHE = CacheQueryDmlExample.class.getSimpleName() + "Persons";
+
+    /**
+     * Executes example.
+     *
+     * @param args Command line arguments, none required.
+     * @throws Exception If example execution failed.
+     */
+    @SuppressWarnings({"unused", "ThrowFromFinallyBlock"})
+    public static void main(String[] args) throws Exception {
+        try (Ignite ignite = Ignition.start("examples/config/example-ignite.xml")) {
+            print("Cache query DML example started.");
+
+            CacheConfiguration<Long, Organization> orgCacheCfg = new CacheConfiguration<>(ORG_CACHE);
+            orgCacheCfg.setIndexedTypes(Long.class, Organization.class);
+
+            CacheConfiguration<Long, Person> personCacheCfg = new CacheConfiguration<>(PERSON_CACHE);
+            personCacheCfg.setIndexedTypes(Long.class, Person.class);
+
+            // Auto-close cache at the end of the example.
+            try (
+                IgniteCache<Long, Organization> orgCache = ignite.getOrCreateCache(orgCacheCfg);
+                IgniteCache<Long, Person> personCache = ignite.getOrCreateCache(personCacheCfg)
+            ) {
+                insert(orgCache, personCache);
+                select(personCache, "Insert data");
+
+                update(personCache);
+                select(personCache, "Update salary for Master degrees");
+
+                delete(personCache);
+                select(personCache, "Delete non-Apache employees");
+            }
+            finally {
+                // Distributed cache could be removed from cluster only by #destroyCache() call.
+                ignite.destroyCache(PERSON_CACHE);
+                ignite.destroyCache(ORG_CACHE);
+            }
+
+            print("Cache query DML example finished.");
+        }
+    }
+
+    /**
+     * Populate cache with test data.
+     *
+     * @param orgCache Organization cache,
+     * @param personCache Person cache.
+     */
+    private static void insert(IgniteCache<Long, Organization> orgCache, IgniteCache<Long, Person> personCache) {
+        // Insert organizations.
+        SqlFieldsQuery qry = new SqlFieldsQuery("insert into Organization (_key, id, name) values (?, ?, ?)");
+
+        orgCache.query(qry.setArgs(1L, 1L, "ASF"));
+        orgCache.query(qry.setArgs(2L, 2L, "Eclipse"));
+
+        // Insert persons.
+        qry = new SqlFieldsQuery(
+            "insert into Person (_key, id, orgId, firstName, lastName, salary, resume) values (?, ?, ?, ?, ?, ?, ?)");
+
+        personCache.query(qry.setArgs(1L, 1L, 1L, "John", "Doe", 4000, "Master"));
+        personCache.query(qry.setArgs(2L, 2L, 1L, "Jane", "Roe", 2000, "Bachelor"));
+        personCache.query(qry.setArgs(3L, 3L, 2L, "Mary", "Major", 5000, "Master"));
+        personCache.query(qry.setArgs(4L, 4L, 2L, "Richard", "Miles", 3000, "Bachelor"));
+    }
+
+    /**
+     * Example of conditional UPDATE query: raise salary by 10% to everyone who has Master degree.
+     *
+     * @param personCache Person cache.
+     */
+    private static void update(IgniteCache<Long, Person> personCache) {
+        String sql =
+            "update Person set salary = salary * 1.1 " +
+            "where resume = ?";
+
+        personCache.query(new SqlFieldsQuery(sql).setArgs("Master"));
+    }
+
+    /**
+     * Example of conditional DELETE query: delete non-Apache employees.
+     *
+     * @param personCache Person cache.
+     */
+    private static void delete(IgniteCache<Long, Person> personCache) {
+        String sql =
+            "delete from Person " +
+            "where id in (" +
+                "select p.id " +
+                "from Person p, \"" + ORG_CACHE + "\".Organization as o " +
+                "where o.name != ? and p.orgId = o.id" +
+            ")";
+
+        personCache.query(new SqlFieldsQuery(sql).setArgs("ASF")).getAll();
+    }
+
+    /**
+     * Query current data.
+     *
+     * @param personCache Person cache.
+     * @param msg Message.
+     */
+    private static void select(IgniteCache<Long, Person> personCache, String msg) {
+        String sql =
+            "select p.id, concat(p.firstName, ' ', p.lastName), o.name, p.resume, p.salary " +
+            "from Person as p, \"" + ORG_CACHE + "\".Organization as o " +
+            "where p.orgId = o.id";
+
+        List<List<?>> res = personCache.query(new SqlFieldsQuery(sql).setDistributedJoins(true)).getAll();
+
+        print(msg);
+
+        for (Object next : res)
+            System.out.println(">>>     " + next);
+    }
+
+    /**
+     * Prints message.
+     *
+     * @param msg Message to print before all objects are printed.
+     */
+    private static void print(String msg) {
+        System.out.println();
+        System.out.println(">>> " + msg);
+    }
+}

http://git-wip-us.apache.org/repos/asf/ignite/blob/e205214a/examples/src/test/java/org/apache/ignite/examples/CacheExamplesSelfTest.java
----------------------------------------------------------------------
diff --git a/examples/src/test/java/org/apache/ignite/examples/CacheExamplesSelfTest.java b/examples/src/test/java/org/apache/ignite/examples/CacheExamplesSelfTest.java
index 43b05b5..4bec419 100644
--- a/examples/src/test/java/org/apache/ignite/examples/CacheExamplesSelfTest.java
+++ b/examples/src/test/java/org/apache/ignite/examples/CacheExamplesSelfTest.java
@@ -23,6 +23,7 @@ import org.apache.ignite.examples.datagrid.CacheApiExample;
 import org.apache.ignite.examples.datagrid.CacheContinuousQueryExample;
 import org.apache.ignite.examples.datagrid.CacheDataStreamerExample;
 import org.apache.ignite.examples.datagrid.CachePutGetExample;
+import org.apache.ignite.examples.datagrid.CacheQueryDmlExample;
 import org.apache.ignite.examples.datagrid.CacheQueryExample;
 import org.apache.ignite.examples.datagrid.CacheTransactionExample;
 import org.apache.ignite.examples.datagrid.starschema.CacheStarSchemaExample;
@@ -129,6 +130,13 @@ public class CacheExamplesSelfTest extends GridAbstractExamplesTest {
     /**
      * @throws Exception If failed.
      */
+    public void testCacheQueryDmlExample() throws Exception {
+        CacheQueryDmlExample.main(EMPTY_ARGS);
+    }
+
+    /**
+     * @throws Exception If failed.
+     */
     public void testCacheApiExample() throws Exception {
         CacheApiExample.main(EMPTY_ARGS);
     }