Show Table of Contents
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()));
Where did the comment section go?
Red Hat's documentation publication system recently went through an upgrade to enable speedier, more mobile-friendly content. We decided to re-evaluate our commenting platform to ensure that it meets your expectations and serves as an optimal feedback mechanism. During this redesign, we invite your input on providing feedback on Red Hat documentation via the discussion platform.