31.9. Script Examples
The following examples demonstrate various tasks to assist in the reader's understanding of the scripting syntax, and to get ideas on what tasks may be suitable for scripts in each environment.
Example 31.6. Distributed Execution
The following is a script that runs within a Distributed Executor. Each node will return its address, and all nodes will be collected in a
List
to be returned to the client:
// mode:distributed,language=javascript cacheManager.getAddress().toString();
Example 31.7. Word Count Stream
The following is a script that runs on the local cache, counting the occurrences of each word in the result set, and then returning the words and their occurrences in a key, value pairing:
// mode=local,language=javascript var Function = Java.type("java.util.function.Function") var Collectors = Java.type("java.util.stream.Collectors") var Arrays = Java.type("org.infinispan.scripting.utils.JSArrays") cache .entrySet().stream() .map(function(e) e.getValue()) .map(function(v) v.toLowerCase()) .map(function(v) v.split(/[\W]+/)) .flatMap(function(f) Arrays.stream(f)) .collect(Collectors.groupingBy(Function.identity(), Collectors.counting()));