Versions Compared

Key

  • This line was added.
  • This line was removed.
  • Formatting was changed.

...

You can use the Script Snap to execute Javascript, Python, or Ruby scripts using the JVM ScriptEngine mechanism. Accounts are not required to work with this Snap.

  • Once you select the language of your choice, click the Edit Script button to open the script editor. 

  • By default, the editor is populated with a basic script you can modify.

  • The basic script reads an input document, wraps it in a map, and writes the wrapper to the output view. If a script file is present in the SLDB, you can upload it to the Snap using the Script file property; this property also accepts pipeline parameters and upstream parameters. 

Note

You cannot create external process (like the popen function) on Cloudplex through the Script Snap or custom a Custom Snap. While external process creation on Groundplex is possible. You can request support@snaplogic.com to disable this if required.

...

Snap Type

The Script Snap is a write-type Snap.

...

Works in Ultra 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 is per the following: 

Paste code macro
languagejavascript
// Read the next document, wrap it in a map and write out the wrapper
var doc = this.input.next();
var wrapper = {
    "original" : doc
}
this.output.write(doc, wrapper);

Note the following two arguments in the output.write() method:

  • The first argument is the input document -- docdocument—doc.

  • The second argument is the data for the output document -- wrapperdocument—wrapper.

Both the arguments are required so that the lineage of the output document can be tied to the input document. This is important for an Ultra pipeline responding to web requests. The initial request becomes an input document to the first Snap in the SnapLogic pipeline, eventually resulting in an output document from the last Snap in the pipeline. The pipeline must maintain the lineage of each document so that each response can be correlated to the request that generated it.

...

Paste code macro
importClass(java.util.LinkedHashMap);
...
var inDoc = this.input.next();
var outDoc = new LinkedHashMap();
outDoc.put("original", inDoc);
this.output.write(inDoc, outDoc);

Breaking Changes

Breaking changes for Pipelines The following breaking changes apply to the pipelines using the Script Snap (or the deprecated Execute Script Snap) with the Python engine.

...

Field Name

Field Type

Description

Label*

Default Value: Script
Example: Script

String

Specify a unique name for the Snap. You can modify this to be more specific, especially if you have more than one of the same Snap in your pipeline.

Scripting Language*

Default valueJavascript
ExampleRuby

Dropdown list

Choose a language for the script. The available options are:

  • Javascript

  • Python

  • Ruby

Script file

Default value: None
Example: transform.py

String/Expression

Speciy Specify or select a script file that implements the ScriptHook interface. This field can be used if the script file is present in the SLDB. Click on the Upload (blue star) icon to upload the required script file from the SLDB. 

This field accepts pipeline parameters as well as upstream parameters provided that the script file is present in the SLDB. 

Edit Script

Default Value: A skeleton for the chosen scripting language.  You can click the Generate Template link to regenerate the skeleton.

Button

Click the Edit Script button to edit a script within the Snap instead of through an external file. From this page, you can export the script to a file in a project, import a script, or generate a template for the selected Scripting Language.

  • Once you select the language of your choice, click the Edit Script button to open the script editor. 

  • By default, the editor is populated with a basic script you can modify.

  • The basic script reads an input document, wraps it in a map, and writes the wrapper to the output view. If a script file is present in

a project, import a script, or generate a template for the selected Scripting Language.
  • the SLDB, you can upload it to the Snap using the Script file property; this property also accepts pipeline parameters and upstream parameters. 

Multiexcerpt include macro
nameSnap Execution
pageSOAP Execute

Default Value: Execute only
Example: Validate & Execute

Dropdown list

Multiexcerpt include macro
nameExecution_Detail_Write
pageSOAP Execute

ScriptHook Interface

This example requires that an input view be defined for it to work.

...

You can find the user’s home directory (user running the jcc) in the jcc filename “jcc_output.log" when you search with user.home. If you have multiple jar files, you can add all the paths in the same .jython file separated by colon, as shown below:

python.path=jar1_path:jar2_path:jar3_path

...

Note
  • The paths listed in python.path can be .jar files (Java libraries), directories containing Python libraries (compatible with Python 2.7), or .zip files packaging those Python libraries. Learn more about using the .jython file: Jython Registry.

  • The python.path variable is Jython's version of CPython’s PYTHONPATH variable. Refer to the official Python documentation for more about the PYTHONPATH.

  • If you are using multiple Groundplex nodes, you must add the package/JAR files in each node.

  • You can import third party libraries only on Groundplex nodes.

Additional Information

The document data can be converted to and from the JSON data interchange language. By convention, the root of every document is conceptually a JSON object—a collection of name-value pairs, where each name is a string, and each value is an object, an array, a string, a number, a boolean, or a null. Every modern programming language has a corresponding type for this concept:

...

To write an output document, your script must create a new object. In Python or Ruby, you can create an instance of the required language’s native object type, a Python dictionary, or a Ruby hash. The values you add to these objects must be one of the JSON-compatible types, including objects, arrays, strings, numbers, and booleans. You can use the corresponding array or list type of the language for an array. Objects written to the output view should be of Java types. Some downstream Snaps require this, for example, the Join Snap. To write a Python map to the output view in a Python script, convert the map to a Java HashMap.

General Instructions for all Scripting Languages

The script author should declare a global variable named 'hook' (note that this variable name is case-sensitive). The Script engine makes the following four global variables available to the script as defined in the Script#ScriptHook Interface section:

...

Paste code macro
languagepython
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")

        while self.input.hasNext():
            data = self.input.next()
            data["firstLast"] = "%s-%s" %(data["first"],data["last"])
            data["firstLast2"] = data["first"] + data["last"]

            data["numberMath"] = data["counter"] + 22
            data["numberMath2"] = data["counter"] + 23

            data["dateMonthPlusOne"] = data["birthday"].plusMonths(1)

            data["numberMathType"] = type(data["counter"])
            data["dateType"] = type(data["birthday"])


            try:
                data["mathTryCatch"] = data["counter2"] + 33
                self.output.write(data)
            except Exception as e:
                data["errorMessage"] = e.message
                self.error.write(data)


        self.log.info("Finished executing the Transform script") 

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

...

Script Snap Configuration Using Python, JS Script, and Ruby Script 

The following pipeline demonstrates how the Script Snap using executes in all three supported languages. This pipeline uses a simple JSON file with First Name, Last Name, and Birthday.

...

The following is a snapshot of the input for the Script Snap:

...

Python Script

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

Paste code macro
languagepython
from com.snaplogic.scripting.language import ScriptHook
from random import randint
from time import sleep
 
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 = 1
       while self.input.hasNext():
           data = self.input.next()
           sleep(randint(1,10))
           map = {"out": data}
           self.output.write(map)
       self.log.info("Finished executing the Transform script")
hook = TransformScript(input, output, error, log)

Java Script

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

Paste code macro
languagejavascript
// Ensure compatibility with both JDK 7 and 8 JSR-223 Script Engines
try { load("nashorn:mozilla_compat.js"); } catch(e) { }
 
script = {
  execute : function() {
    while (input.hasNext()) {
      var in_data = input.next()
      var new_data = {}
      var keyArray = in_data.keySet().toArray()
      for (var index in keyArray) {
        var key = keyArray[index]
        new_data[key] = in_data.get(key)
      }
 
      new_data.firstLast = new_data.first + "-" + new_data.last
      new_data.firstLast2 = new_data.first + in_data.get("last")
      new_data.numberMath = (new_data.counter + 22) | 0
      new_data.numberMath2 = new_data.counter + 23
      new_data.dateMath = new_data.birthday.plusMonths(1).toString()
      
      new_data.mathType = typeof(new_data.counter)      
      new_data.dateType = typeof(new_data.birthday)              
 
      output.write(new_data)
    }
   }  
};
var hook = new com.snaplogic.scripting.language.ScriptHook(script)

Ruby Script

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

...

The following example demonstrates the execution of the Script Snap using the pipeline parameters; the script file in the SLDB is passed as a Pipeline parameter. The pipeline demonstrated above is modified to accept pipeline parameters in this example by configuring the Script file property. For the scripts to be passed as a pipeline parameter, the script file should be present in the project folder in SnapLogic. Confirm that the script files are present in the Files section inside the Manager that the script files are present; if not, upload them by clicking on the '+' icon.

...

The individual Script Snaps are configured as shown below: 

As in the example above, this pipeline produces the same output upon on execution. The exported pipeline and sample script files used are available as a zip file in the Downloads section below.

Downloads

Multiexcerpt include macro
namedownload_instructions
pageOpenAPI

Attachments
patterns*.slp,*.zip

 

Related Content:

...