Bhupendra Singh
Bhupendra Singh

Reputation: 1

Executing a python script in snaplogic

I am trying to run a python script through script snap in snaplogic. I am facing some issues where it ask me to declare a script hook variable. Can you please help me on that.

Upvotes: -2

Views: 1489

Answers (2)

Sumit kumar
Sumit kumar

Reputation: 21

As explained by @dwhite0101, within Script Snap when you click Edit Script you get an option to generate code template.

ScriptHook is an interface that is implemented as callback mechanism for Script Snap to call into the script. It helps you to deal with input and output rows. The constructor below initializes the input, output, error and log variables. self object is similar to this in c++ that holds the current row values.

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

You can perform transformations in execute method:

def execute(self):
    self.log.info("Executing Transform script")
    while self.input.hasNext():
        in_doc = self.input.next()
        wrapper = java.util.HashMap()

        for field in in_doc:
            #your code

Next step is to store your results in an object and output it:

wrapper['original'] = result
self.output.write(result, wrapper)

Make sure to indent your code properly.

Upvotes: 2

dwhite0101
dwhite0101

Reputation: 21

With the script snap you should use the "Edit Script" button on the snap itself. This will open a script editor and generate a skeleton script in the language you've selected (Py in this case).

In the skeleton you can see the baseline methods and functions we define. In there you can see the usage and comments of the scripthook var. If you have an existing script I would recommend trying to write it into this skeleton's execute method than trying to implement scripthook in your existing code. You can also define your own methods and functions within the confines of the skeleton class and reference them with "this." notation.

For faster answers on SnapLogic related questions I'd recommend visiting the SnapLogic Community site.

Upvotes: 2

Related Questions