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

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



Download this Pipeline

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

Output from Python Script



Download the Script

Java Script

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

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


Download this Script

Ruby Script

Script Snap uses the 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()
  
        while input.hasNext()  do
          data = input.next()
        
            data["firstLast2"] = data["first"] + data["last"]
            
            data["numberMath"] = data["counter"] + 22
            data["numberMath2"] = data["counter"] + 23
            data["dateMonthPlusOne"] = data["birthday"].plusMonths(1)
   
            begin
                data["mathTryCatch"] = data["counter"] + 33
                output.write(data)
            rescue Exception=>e
                data["errorMessage"] = e.message
                error.write(data)
            end
        end

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


Download this Script