1
\$\begingroup\$

Following is the script I'm using to group key-value pairs based on an ID field viz. interface_id:

// Ensure compatibility with both JDK 7 and 8 JSR-223 Script Engines
try { load("nashorn:mozilla_compat.js"); } catch(e) { }

// Import the interface required by the Script snap.
importPackage(com.snaplogic.scripting.language);

// Import the Java utility classes.
importPackage(java.util);

/**
 * Create an object that implements the methods defined by the "ScriptHook"
 * interface.  We'll be passing this object to the constructor for the
 * ScriptHook interface.
 */
var impl = {
    /*
     * These variables (input, output, error, log) are defined by the
     * ExecuteScript snap when evaluating this script.
     */
    input : input,
    output : output,
    error : error,
    log : log,

    /**
     * The "execute()" method is called once when the pipeline is started
     * and allowed to process its inputs or just send data to its outputs.
     *
     * Exceptions are automatically caught and sent to the error view.
     */
    execute : function () {
        var interfaceIds = new java.util.ArrayList();
        var interfaceParams = new java.util.ArrayList();
        while (this.input.hasNext()) {
            var doc = this.input.next();

            var interfaceId = doc.interface_id;
            var paramName = doc.param_name;
            var paramValue = doc.param_value;

            var index = -1;

            if(!interfaceIds.contains(interfaceId)) {
                interfaceIds.add(interfaceId);
                var interfaceMap = new java.util.LinkedHashMap();
                interfaceMap.put("interface_id", interfaceId);
                interfaceParams.add(interfaceMap);
            }

            index = interfaceIds.indexOf(interfaceId);
            if(index >= 0) {
                interfaceParams.get(index).put(paramName,paramValue);
            }
        }

        for(var i=0; i<interfaceParams.size(); i++) {
            this.output.write(interfaceParams.get(i));
        }
    }
};

/**
 * The Script Snap will look for a ScriptHook object in the "hook"
 * variable.  The snap will then call the hook's "execute" method.
 */
var hook = new com.snaplogic.scripting.language.ScriptHook(impl);

The Pipeline

Pipeline

Input

Input

Output

Output

Output as JSON

In the above script, since I'm more comfortable with Java, I used ArrayList and LinkedHashMap. Is it the correct way to do this? If yes, please do review the overall correctness and efficiency of the code. If not, is there a way to do this only using the scripting language and not using Java's collections framework. My understanding is that, for large datasets, it is better to avoid collections framework (ideally, avoiding the script altogether is a better option). Any other approach is also welcome.


PS: I did not find a tag for so I'm tagging it under JavaScript and Java. This is because, I've set the scripting language to JavaScript and I'm using Java's collections framework in it.

Script Description

\$\endgroup\$
  • \$\begingroup\$ Ideally, this post should be tagged as SnapLogic but since it is not available I'm using the tags to indicate the appropriate language elements. \$\endgroup\$ – Bilesh Ganguly Jan 20 '17 at 11:23

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Browse other questions tagged or ask your own question.