...
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 |
...
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 | ||
---|---|---|
| ||
// 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 | 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 value: Javascript | Dropdown list | Choose a language for the script. The available options are:
| ||||||||||||
Script file Default value: None | 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 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.the script to a file in a project, import a script, or generate a template for the selected Scripting Language.
| ||||||||||||
Default Value: Execute only | Dropdown list |
|
...
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
...
Paste code macro | ||
---|---|---|
| ||
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.
...
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 | ||||
---|---|---|---|---|
|
...