#ignite-964: change map-reduce-example.js
Project: http://git-wip-us.apache.org/repos/asf/incubator-ignite/repo Commit: http://git-wip-us.apache.org/repos/asf/incubator-ignite/commit/29300a4d Tree: http://git-wip-us.apache.org/repos/asf/incubator-ignite/tree/29300a4d Diff: http://git-wip-us.apache.org/repos/asf/incubator-ignite/diff/29300a4d Branch: refs/heads/ignite-961 Commit: 29300a4dc3f90dbf1e13c1a5a30a83b20abd6bdd Parents: 9413747 Author: ivasilinets <ivasilin...@gridgain.com> Authored: Thu Jul 9 18:15:40 2015 +0300 Committer: ivasilinets <ivasilin...@gridgain.com> Committed: Thu Jul 9 18:15:40 2015 +0300 ---------------------------------------------------------------------- examples/src/main/js/cache-api-example.js | 2 +- examples/src/main/js/cache-put-get-example.js | 2 +- examples/src/main/js/map-reduce-example.js | 72 +++++++++++++++------- examples/src/main/js/run-cache-script.js | 13 ++-- examples/src/main/js/run-function-example.js | 50 ++++++++++----- 5 files changed, 94 insertions(+), 45 deletions(-) ---------------------------------------------------------------------- http://git-wip-us.apache.org/repos/asf/incubator-ignite/blob/29300a4d/examples/src/main/js/cache-api-example.js ---------------------------------------------------------------------- diff --git a/examples/src/main/js/cache-api-example.js b/examples/src/main/js/cache-api-example.js index 1514941..9308353 100644 --- a/examples/src/main/js/cache-api-example.js +++ b/examples/src/main/js/cache-api-example.js @@ -24,7 +24,7 @@ var Ignition = apacheIgnite.Ignition; * 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 {@link ExampleJsNodeStartup} in another JVM which will + * Alternatively you can run ExampleJsNodeStartup in another JVM which will * start node with {@code examples/config/js/example-js-cache.xml} configuration. */ function main() { http://git-wip-us.apache.org/repos/asf/incubator-ignite/blob/29300a4d/examples/src/main/js/cache-put-get-example.js ---------------------------------------------------------------------- diff --git a/examples/src/main/js/cache-put-get-example.js b/examples/src/main/js/cache-put-get-example.js index 80c2080..bf2c472 100644 --- a/examples/src/main/js/cache-put-get-example.js +++ b/examples/src/main/js/cache-put-get-example.js @@ -25,7 +25,7 @@ var CacheEntry = apacheIgnite.CacheEntry; * 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 {@link ExampleJsNodeStartup} in another JVM which will + * Alternatively you can run ExampleJsNodeStartup in another JVM which will * start node with {@code examples/config/js/example-js-cache.xml} configuration. */ function main() { http://git-wip-us.apache.org/repos/asf/incubator-ignite/blob/29300a4d/examples/src/main/js/map-reduce-example.js ---------------------------------------------------------------------- diff --git a/examples/src/main/js/map-reduce-example.js b/examples/src/main/js/map-reduce-example.js index e6d7ee9..e56b99d 100644 --- a/examples/src/main/js/map-reduce-example.js +++ b/examples/src/main/js/map-reduce-example.js @@ -18,39 +18,65 @@ var apacheIgnite = require("apache-ignite"); var Ignition = apacheIgnite.Ignition; -Ignition.start(['127.0.0.1:9095'], null, onConnect); +/** + * Demonstrates a simple use of Compute.mapReduce. + * <p> + * Phrase passed as task argument is split into jobs each taking one word. Then jobs are distributed among + * cluster nodes. Each node computes word length and returns result to master node where total phrase length + * is calculated on reduce stage. + * <p> + * Remote nodes should always be started with special configuration file which + * enables P2P class loading: {@code 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. + */ +function main() { + /** 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) { - console.log(">>> Compute task split example started."); + function onConnect(err, ignite) { + console.log(">>> Compute map reduce example started."); - var map = function(nodes, args) { - var words = args.split(" "); + /** + * Splits the received string to words, creates a child job for each word, and sends + * these jobs to other nodes for processing. Each such job simply prints out the received word. + */ + var map = function(nodes, str) { + var words = str.split(" "); - for (var i = 0; i < words.length; i++) { - var f = function (word) { - print(">>> Printing '" + word + "' on this node from ignite job."); + for (var i = 0; i < words.length; i++) { + var job = function (word) { + print(">>> Printing '" + word + "' on this node from job."); - return word.length; - }; + return word.length; + }; - emit(f, words[i], nodes[i % nodes.length]); + emit(job, words[i], nodes[i % nodes.length]); + } } - } - var reduce = function(results) { - var sum = 0; + /** + * Reduces results received so far into one compound result to be returned. + */ + var reduce = function(results) { + var sum = 0; + + for (var i = 0; i < results.length; ++i) { + sum += results[i]; + } - for (var i = 0; i < results.length; ++i) { - sum += results[i]; + return sum; } - return sum; - } + // Called when map reduced finished. + var onMapReduce = function(err, cnt) { + console.log(">>> Total number of characters in the phrase is '" + cnt + "'."); + console.log(">>> End of compute map reduce example."); + } - var onMapReduce = function(err, cnt) { - console.log(">>> Total number of characters in the phrase is '" + cnt + "'."); - console.log(">>> Check all nodes for output (this node is also part of the cluster)."); + ignite.compute().execute(map, reduce, "Hello Ignite Enabled World!", onMapReduce); } +} - ignite.compute().execute(map, reduce, "Hello Ignite Enabled World!", onMapReduce); -} \ No newline at end of file +main(); \ No newline at end of file http://git-wip-us.apache.org/repos/asf/incubator-ignite/blob/29300a4d/examples/src/main/js/run-cache-script.js ---------------------------------------------------------------------- diff --git a/examples/src/main/js/run-cache-script.js b/examples/src/main/js/run-cache-script.js index 1640cea..d4d72cc 100644 --- a/examples/src/main/js/run-cache-script.js +++ b/examples/src/main/js/run-cache-script.js @@ -24,7 +24,7 @@ var Ignition = apacheIgnite.Ignition; * 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 {@link ExampleJsNodeStartup} in another JVM which will + * Alternatively you can run ExampleJsNodeStartup in another JVM which will * start node with {@code examples/config/js/example-js-cache.xml} configuration. */ function main() { @@ -63,12 +63,15 @@ function main() { return val.salary; } - var onRunScript = function(err, salary) { + /** Run remote job on server ignite node with arguments [cacheName, key]. */ + ignite.compute().runScript(job, [cacheName, key], onRun); + + var onRun = function(err, salary) { console.log(">>> " + key + "'s salary is " + salary); - } - /** Run remote job on server ignite node with arguments [cacheName, key]. */ - ignite.compute().runScript(job, [cacheName, key], onRunScript); + // Destroying cache. + ignite.destroyCache(cacheName, function(err) { console.log(">>> End of run cache script example."); }); + } } } } http://git-wip-us.apache.org/repos/asf/incubator-ignite/blob/29300a4d/examples/src/main/js/run-function-example.js ---------------------------------------------------------------------- diff --git a/examples/src/main/js/run-function-example.js b/examples/src/main/js/run-function-example.js index 1005c9f..832f9d6 100644 --- a/examples/src/main/js/run-function-example.js +++ b/examples/src/main/js/run-function-example.js @@ -18,27 +18,47 @@ var apacheIgnite = require("apache-ignite"); var Ignition = apacheIgnite.Ignition; -Ignition.start(['127.0.0.1:9095'], null, onConnect); +/** + * Demonstrates using of Compute job execution on the cluster. + * <p> + * This example takes a sentence composed of multiple words and counts number of non-space + * characters in the sentence by having each compute job count characters in each individual + * word. +* <p> + * <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. + */ +function main() { + /** 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) { - console.log(">>> Compute callable example started"); + function onConnect(err, ignite) { + console.log(">>> Compute callable example started"); - var f = function (args) { - var words = args.split(" "); + var job = function (args) { + var words = args.split(" "); - var sum = 0; + var sum = 0; - for (var i = 0; i < words.length; ++i) { - sum += words[i].length; + for (var i = 0; i < words.length; ++i) { + sum += words[i].length; + } + + return sum; } - return sum; - } + // Execute job on ignite server node. + ignite.compute().runScript(job, "Hello Ignite Enabled World!", onRun); - var onRunScript = function(err, sum) { - console.log(">>> Total number of characters in the phrase is '" + sum + "'."); - console.log(">>> Check all nodes for output (this node is also part of the cluster)."); + function onRun(err, sum) { + console.log(">>> Total number of characters in the phrase is '" + sum + "'."); + console.log(">>> End of compute callable example."); + } } +} - ignite.compute().runScript(f, "Hello Ignite Enabled World!", onRunScript); -} \ No newline at end of file +main(); \ No newline at end of file