ignite-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From ivasilin...@apache.org
Subject [31/50] [abbrv] incubator-ignite git commit: #ignite-964: add sql query examples.
Date Mon, 13 Jul 2015 06:35:32 GMT
#ignite-964: add sql query examples.


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

Branch: refs/heads/ignite-961
Commit: d4c223cac244255a8bf38430e51e47f60762a44c
Parents: 91f18fb
Author: ivasilinets <ivasilinets@gridgain.com>
Authored: Fri Jul 10 11:26:48 2015 +0300
Committer: ivasilinets <ivasilinets@gridgain.com>
Committed: Fri Jul 10 11:26:48 2015 +0300

----------------------------------------------------------------------
 examples/src/main/js/cache-query-example.js     |  99 +++++++++++++----
 .../main/js/cache-sql-fields-query-example.js   | 106 +++++++++++++++++++
 2 files changed, 183 insertions(+), 22 deletions(-)
----------------------------------------------------------------------


http://git-wip-us.apache.org/repos/asf/incubator-ignite/blob/d4c223ca/examples/src/main/js/cache-query-example.js
----------------------------------------------------------------------
diff --git a/examples/src/main/js/cache-query-example.js b/examples/src/main/js/cache-query-example.js
index 5241e69..18b1482 100644
--- a/examples/src/main/js/cache-query-example.js
+++ b/examples/src/main/js/cache-query-example.js
@@ -22,36 +22,91 @@ var SqlQuery = apacheIgnite.SqlQuery;
 var SqlFieldsQuery = apacheIgnite.SqlFieldsQuery;
 var CacheEntry = apacheIgnite.CacheEntry;
 
-var cacheName = "CacheQueryExample";
 
-Ignition.start(['127.0.0.1:9095'], null, onConnect);
+/**
+  * Cache queries example. This example demonstrates SQL queries over cache.
+  * <p>
+  * Remote nodes should always be started with special configuration file which
+  * enables P2P class loading: {@code 'ignite.{sh|bat} examples/config/js/example-js-cache.xml'}.
+  * <p>
+  * Alternatively you can run ExampleJsNodeStartup in another JVM which will
+  * start node with {@code examples/config/js/example-js-cache.xml} configuration.
+  */
+main() {
+    /** Cache name. */
+    var cacheName = "CacheQueryExample";
 
-function onConnect(err, ignite) {
-    if (err !== null)
-        throw "Start remote node with config examples/config/js/example-js-cache.xml.";
+    /** Connect to node that started with {@code examples/config/js/example-js-cache.xml}
configuration. */
+    Ignition.start(['127.0.0.1:9095'], null, onConnect);
 
-    console.log(">>> Cache query example started.");
+    function onConnect(err, ignite) {
+        if (err !== null)
+            throw "Start remote node with config examples/config/js/example-js-cache.xml.";
 
-    var entries = [new CacheEntry("key0", "val0"), new CacheEntry("key1", "val1")];
+        console.log(">>> Cache query example started.");
 
-    ignite.getOrCreateCache(cacheName, function(err, cache) {
-            cache.putAll(entries, onCachePut.bind(null, ignite));
-        });
-}
+        var entries = initializeEntries();
 
-function onCachePut(ignite, err) {
-    var qry = new SqlQuery("Select * from String");
-    qry.setReturnType("String");
+        ignite.getOrCreateCache(cacheName, function(err, cache) {
+                cacheQuery(ignite, cache, entries);
+            });
+    }
 
-     var fullRes = [];
+    function cacheQuery(ignite, cache, entries) {
+        cache.putAll(entries, onCachePut.bind(null, ignite));
 
-    qry.on("page", function(res) {
-        fullRes = fullRes.concat(res);
-    });
+        function onCachePut(ignite, err) {
+            console.log(">>> Create cache for people.")
 
-    qry.on("end", function(err) {
-        console.log(fullRes);
-    });
+            //SQL clause which selects salaries based on range.
+            var qry = new SqlQuery("salary > ? and salary <= ?");
+            qry.setReturnType("Object");
 
-    ignite.cache(cacheName).query(qry);
+            // Set page size for query.
+            qry.setPageSize(2);
+
+            //Set salary range.
+            qry.setArguments([0, 2000]);
+
+            var fullRes = [];
+
+            //This function is called when we get part of query result.
+            qry.on("page", function(res) {
+                console.log(">>> Get result on page: " + JSON.stringify(res));
+
+                fullRes = fullRes.concat(res);
+            });
+
+            //This function is called when query is finished.
+            qry.on("end", function(err) {
+                console.log(">>> People with salaries between 0 and 2000 (queried
with SQL query): " +
+                    JSON.stringify(fullRes));
+
+                // Destroying cache.
+                ignite.destroyCache(cacheName, function(err) {
+                        console.log(">>> End of query example.");
+                    });
+            });
+
+            //Run query.
+            ignite.cache(cacheName).query(qry);
+        }
+    }
+
+    // Initialize cache for people.
+    function initializeEntries() {
+        var key1 = "1";
+        var value1 = {"firstName" : "John", "lastName" : "Doe", "salary" : 2000};
+        var key2 = "2";
+        var value2 = {"firstName" : "Jane", "lastName" : "Doe", "salary" : 1000};
+        var key3 = "3";
+        var value3 = {"firstName" : "John", "lastName" : "Smith", "salary" : 1000};
+        var key4 = "4";
+        var value4 = {"firstName" : "Jane", "lastName" : "Smith", "salary" : 2000};
+        var key5 = "5";
+        var value5 = {"firstName" : "Ann", "lastName" : "Smith", "salary" : 3000};
+
+        return [new CacheEntry(key1, value1), new CacheEntry(key2, value2),
+            new CacheEntry(key3, value3), new CacheEntry(key4, entry4)];
+    }
 }
\ No newline at end of file

http://git-wip-us.apache.org/repos/asf/incubator-ignite/blob/d4c223ca/examples/src/main/js/cache-sql-fields-query-example.js
----------------------------------------------------------------------
diff --git a/examples/src/main/js/cache-sql-fields-query-example.js b/examples/src/main/js/cache-sql-fields-query-example.js
new file mode 100644
index 0000000..64a50f7
--- /dev/null
+++ b/examples/src/main/js/cache-sql-fields-query-example.js
@@ -0,0 +1,106 @@
+/*
+ * 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.
+ */
+
+var apacheIgnite = require("apache-ignite");
+
+var Ignition = apacheIgnite.Ignition;
+var SqlQuery = apacheIgnite.SqlQuery;
+var SqlFieldsQuery = apacheIgnite.SqlFieldsQuery;
+var CacheEntry = apacheIgnite.CacheEntry;
+
+/**
+  * Cache queries example. This example demonstrates SQL queries over cache.
+  * <p>
+  * Remote nodes should always be started with special configuration file which
+  * enables P2P class loading: {@code 'ignite.{sh|bat} examples/config/js/example-js-cache.xml'}.
+  * <p>
+  * Alternatively you can run ExampleJsNodeStartup in another JVM which will
+  * start node with {@code examples/config/js/example-js-cache.xml} configuration.
+  */
+main() {
+    /** Cache name. */
+    var cacheName = "CacheSqlFieldsQueryExample";
+
+    /** Connect to node that started with {@code examples/config/js/example-js-cache.xml}
configuration. */
+    Ignition.start(['127.0.0.1:9095'], null, onConnect);
+
+    function onConnect(err, ignite) {
+        if (err !== null)
+            throw "Start remote node with config examples/config/js/example-js-cache.xml.";
+
+        console.log(">>> Cache sql fields query example started.");
+
+        var entries = initializeEntries();
+
+        ignite.getOrCreateCache(cacheName, function(err, cache) {
+                cacheSqlFieldsQuery(ignite, cache, entries);
+            });
+    }
+
+    function cacheSqlFieldsQuery(ignite, cache, entries) {
+        cache.putAll(entries, onCachePut.bind(null, ignite));
+
+        function onCachePut(ignite, err) {
+            console.log(">>> Create cache for people.")
+
+            //Sql query to get names of all employees.
+            var qry = new SqlFieldsQuery("select concat(firstName, ' ', lastName) from Person");
+
+            // Set page size for query.
+            qry.setPageSize(2);
+
+            var fullRes = [];
+
+            //This function is called when we get part of query result.
+            qry.on("page", function(res) {
+                console.log(">>> Get result on page: " + JSON.stringify(res));
+
+                fullRes = fullRes.concat(res);
+            });
+
+            //This function is called when query is finished.
+            qry.on("end", function(err) {
+                console.log(">>> Names of all employees:): " + JSON.stringify(fullRes));
+                    
+                // Destroying cache.
+                ignite.destroyCache(cacheName, function(err) {
+                        console.log(">>> End of sql fields query example.");
+                    });
+            });
+
+            //Run query.
+            ignite.cache(cacheName).query(qry);
+        }
+    }
+
+    // Initialize cache for people.
+    function initializeEntries() {
+        var key1 = "1";
+        var value1 = {"firstName" : "John", "lastName" : "Doe", "salary" : 2000};
+        var key2 = "2";
+        var value2 = {"firstName" : "Jane", "lastName" : "Doe", "salary" : 1000};
+        var key3 = "3";
+        var value3 = {"firstName" : "John", "lastName" : "Smith", "salary" : 1000};
+        var key4 = "4";
+        var value4 = {"firstName" : "Jane", "lastName" : "Smith", "salary" : 2000};
+        var key5 = "5";
+        var value5 = {"firstName" : "Ann", "lastName" : "Smith", "salary" : 3000};
+
+        return [new CacheEntry(key1, value1), new CacheEntry(key2, value2),
+            new CacheEntry(key3, value3), new CacheEntry(key4, entry4)];
+    }
+}
\ No newline at end of file


Mime
View raw message