Execute Script

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 Tasks 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.

Account:

Accounts are not used with this Snap.

 

Views:

Input

This Snap has at most one document input view.

Output

This Snap has at most one document output view.

Error

This Snap has at most one document error view and produces zero or more documents in the view.

Settings

Label

Scripting Language

Required. Language in which the script is provided. Options available include Python, Javascript, and Ruby.
Default value: Javascript

Script File

Required. 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 Type

Java class

 Snap Type

Java 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)