Execute Script

On this Page

This Snap is Deprecated. See Script instead.

Snap type:Write
Description:

This Snap executes a script file using the ScriptEngine mechanism. The script is contained within a file, which can be located anywhere SnapLogic and the SnapLogic user has access to it.

Prerequisites:[None]
Support and limitations:

Works in Ultra Task Pipelines if you pass the original document to the 'write()' method for the output view. For example, in the JavaScript template, the body of the loop looks like:
 

// Read the next document, wrap it in a map and write out the wrapper
   var doc = this.input.next();
   var wrapper = new java.util.HashMap();
   wrapper.put("original", doc);
   this.output.write(wrapper);

The last line should look like:

this.output.write(doc, wrapper);

The original document is needed so that lineage can be maintained.

Breaking changes for Pipelines using the Script Snap (or the deprecated Execute Script Snap) with the Python engine

To implement Python in the Script Snap we use Jython.

We recently upgraded the Jython engine from version 2.7-b3 (a beta version from 2014) to the current version, 2.7.2 (March, 2020). The following are the resultant issues and workarounds that we are suggesting:

  • There's an open bug in 2.7 that introduced a backwards-incompatible change in the SnapLogic platform wherein the Jython engine automatically converts BigInteger values to primitive long values. This impacts all your scripts that perform numeric manipulation of integer values from documents (SnapLogic uses the BigInteger type to represent integers in documents). Your Pipelines and Snaps with the Script Snap (or the deprecated Execute Script Snap) that use numeric manipulation scripts with integer or BigInteger data type may fail during execution. We recommend you to prospectively replace integer or BigInteger values with long values.

    Example:
    sum = a.intValue() + b.intValue()
    Here a and b are of BigInteger type that now fail as Jython 2.7.2 automatically and transparently calls longValue() on any BigInteger value it encounters. So a and b would need to use the long and not BigInteger type.

    The known fix is to rewrite the above calculation as sum = a + b by removing occurrences of .intValue() or .longValue() from your Python scripts. 

  • Before the 4.22 release (August 2020), when using the Script Snap with the Scripting language option selected as Python, requesting a key that did not exist in a dictionary (for example, my_dict['missing_key']) would return None. Starting from the 4.22 release, the same request now returns a KeyError exception. If you need to continue returning None, use the .get(key) method instead (for example, my_dict.get['missing_key']).

  • - zlib.compress():
 The zlib library compresses the JSON files retrieved from the SnapLogic APIs and backs-up Pipelines and accounts to a database. The following Python code, when trying to compress displays an ascii … ordinal not in range(128) error.
    Original code: in_doc["json"] = zlib.compress(in_doc["json"])
    Fix: in_doc["json"] = zlib.compress(in_doc["json"].encode("utf-8"))

  • {dictionary}.values().toArray()[i]:
 Prior to the 4.22 release (August 2020), to subscript a {dictionary}.values() method, you had to append the toArray() method to values(); else, you would see the Failure: ‘java.util.LinkedHashMap$LinkedValues’ object is unsubscriptable error. After the 4.22 release, toArray() returns Failure: ‘list’ object has no attribute ‘toArray’. However, the requirement for toArray() is no longer necessary for the subscript.
    Original code: sLine = data.values().toArray()[0]
    Fix: sLine = data.values()[0]

Account:

Accounts are not used with this Snap.


Views:
InputThis Snap has at most one document input view.
OutputThis Snap has at most one document output view.
ErrorThis Snap has at most one document error view and produces zero or more documents in the view.

Settings

Label

Error rendering macro 'excerpt-include' : No link could be created for 'SPD:Snap Label'.

Scripting LanguageRequired. Language in which the script is provided. Options available include Python, Javascript, and Ruby.
Default value: Javascript
Script FileRequired. A script file that implements the ScriptHook interface.
Example: slbd://transform.py
Default value: [None]

Examples


The ScriptHook interface that can be used by the script:

package com.snaplogic.scripting.language;

import java.util.Iterator;

/**
 * ScriptHook is the interface that should be implemented as a callback mechanism for
 * ExecuteScript Snap to call into the script.
 *
 */
public interface ScriptHook {

    /**
     * Scripts should implement this method to provide application logic.
     */
    void execute();

    /**
     * Input is interface that is used by the script to read input from the Snap's input view.
     */
    public static interface Input extends Iterator<Object> {
    }

    /**
     * Output is interface that is used by the script to send output data to the Snap output view.
     */
    public static interface Output {

        /**
         * Write the data to the Snap output.
         *
         * @param data
         */
        void write(Object data);
    }

    /**
     * Error is interface that is used by the script to send error data to Snap error view.
     */
    public static interface Error extends Output {
    }
}

General Instructions for All Scripting Languages

The script author should declare a global variable named 'hook' (note that this variable name is case sensitive).  
 
Script engine makes 4 global variables available to the script - input, output, error and log.

  • Variable 'input' is of type ScriptHook.Input defined above.
  • Variable 'output' is of type ScriptHook.Output defined above.
  • Variable 'error' is of type ScriptHook.Error defined above.
  • Variable 'log' is of type org.slf4j.Logger.


Type defined in the schema maps to the Java class as shown below:

 Snap TypeJava class
 NUMBER   java.math.BigDecimal
 INTEGER java.math.BigInteger 
 STRING java.lang.String
 DATETIME org.joda.time. DateTime  
 LOCALDATETIME org.joda.time. LocalDateTime 
 BOOLEAN java.lang.Boolean 
 DATE org.joda.time. LocalDate 
 TIME org.joda.time.LocalTime
 BINARY java.lang.Byte 
 TABLE java.util.List 
 ANY java.lang.Object 
 COMPOSITE java.util.Map 

 
Objects written to the output view should be of Java types. This is required by some downstream Snaps, for example the Joiner Snap. To write out a Python map to the output view in a Python script, convert the map to a Java HashMap as follows:

import java.util
python_map = {}
self.output.write(java.util.HashMap(python_map))

Python

Script Snap uses Jython engine to execute the scripts written in Python.

from com.snaplogic.scripting.language import ScriptHook
from com.snaplogic.scripting.language.ScriptHook import *

class TransformScript(ScriptHook):
    def __init__(self, input, output, error, log):
        self.input = input
        self.output = output
        self.error = error
        self.log = log

    def execute(self):
        self.log.info("Executing Transform script")
        i = 0
        while self.input.hasNext():
            data = self.input.next().intValue()
            if (i % 2 == 0):
                self.error.write(data * 10);
            else:
                self.output.write(data * 10)
            i = i + 1
        self.log.info("Finished executing the Transform script")

hook = TransformScript(input, output, error, log)

Ruby

Script Snap uses JRuby engine to execute the scripts written in Ruby.

class MyScript
    include com.snaplogic.scripting.language.ScriptHook
    attr_reader :log, :input, :output, :error
    def initialize(log, input, output, error)
        @log = log
        @input = input
        @output = output
        @error = error
        @array = [java.lang.Integer.valueOf(1), java.lang.Integer.valueOf(2)]
    end

    def execute()
        for element in @array
            output.write(java.lang.Integer.valueOf(element))
        end
    end
end

$hook = MyScript.new($log, $input, $output, $error)

JavaScript

Script Snap uses Rhino engine to execute the scripts written in JavaScript.

script = {

    execute : function() {
        for (i = 0; i < data.length; i++) {
            output.write(java.lang.Integer.valueOf(data[i]))
        }
    }
};
var data = [1, 2]
var hook = new com.snaplogic.scripting.language.ScriptHook(script)

Snap Pack History

 Click to view/expand
ReleaseSnap Pack VersionDateType Updates

4.27

main12833

 

Stable

Upgraded with the latest SnapLogic Platform release.
4.26main11181 StableUpgraded with the latest SnapLogic Platform release.
4.25main9554 StableUpgraded with the latest SnapLogic Platform release.
4.24main8556 
StableUpgraded with the latest SnapLogic Platform release.
4.23 Patch423patches7671 Latest

Fixes an issue with the PySpark Snap by removing the dependency on the json-path library, thus avoiding a conflict between the external library version and the SnapLogic json-path.jar.

4.23main7430 Stable

Upgraded with the latest SnapLogic Platform release.

4.22main6403 Latest

Upgraded the Jython engine from version 2.7-b3 (a beta version from 2014) to the current version, 2.7.2 (March, 2020). See the breaking changes note for Script Snap and the deprecated Execute Script Snap in the Limitations and Known Issues section for potential impacts of this upgrade.   

4.21snapsmrc542-Stable

Adds a new Cleanup method in the ScriptHook interface associated with the Script Snap. This method enables the Script Snap to automatically execute a clean-up once the configured script completes executing.

4.20snapsmrc535-StableUpgraded with the latest SnapLogic Platform release.
4.19snaprsmrc528-StableUpgraded with the latest SnapLogic Platform release.
4.18snapsmrc523-Latest

Enhanced the PySpark Snap to work in Kerberized Hadoop clusters with the addition of a Kerberos account.

4.17 Patch ALL7402-Latest

Pushed automatic rebuild of the latest version of each Snap Pack to SnapLogic UAT and Elastic servers.

4.17snapsmrc515-Stable

Added the Snap Execution field to all Standard-mode Snaps. In some Snaps, this field replaces the existing Execute during preview check box.

4.16 snapsmrc508-StableUpgraded with the latest SnapLogic Platform release.
4.15snapsmrc500-StableUpgraded with the latest SnapLogic Platform release.

4.14

snapsmrc490

-StableUpgraded with the latest SnapLogic Platform release.
4.13snapsmrc486-StableUpgraded with the latest SnapLogic Platform release.

4.12

snapsmrc480

-Stable

Updated the PySpark Snap to enable users to stop a PySpark job in progress by pressing the STOP button.

4.11

snapsmrc465

-Stable

Added a new Snap, the PySpark Snap which is supported on the Groundplex on edge node and Hadooplex node.

4.10

snapsmrc414

-StableUpgraded with the latest SnapLogic Platform release.

4.9

snapsmrc405

-StableUpgraded with the latest SnapLogic Platform release.

4.8

snapsmrc398

-StableUpgraded with the latest SnapLogic Platform release.

4.7

snapsmrc382

-StableUpgraded with the latest SnapLogic Platform release.

4.6.0

snapsmrc344

-StableUpgraded with the latest SnapLogic Platform release.

4.4.1

--Latest

Updated default generated template to ensure compatibility with both JDK 7 and 8 JSR-223 Script Engines.

March 2015

--Stable

Script Snap: Generate Template a link has been added within the Edit Script page to provide a template for each of the supported languages.

January 2015

--Stable

Script Snap: Optional Execute during preview property added.

December 20, 2014

--Stable

Documentation enhancement: updated script samples with a sample pipeline

May 2014

--Stable

Script Snap was introduced in this release.

Initial Release (June 2013)

--Stable

Execute Script introduced in this release.