callScript

Execute script for each row in a table

Execute a Python script for each row in a table, where the script does the processing of the values in the table that are passed as arguments.

The script must be uploaded to the LogicHub (drag and drop script file to Automations > Scripts).

Operator Usage in Easy Mode

  1. Click + on the parent node.
  2. Enter the Call Script operator in the search field and select the operator from the Results to open the operator form.
  3. In the Input Table drop-down, enter or select the table containing the data to run this operator on.
  4. In the Script Name drop-down, select the name of the script.
  5. In the Args drop-down, select one or multiple column names to use as arguments for the script.
  6. In the Time between Calls field, enter a value in seconds, minutes, hours, days, or weeks.
  7. Click Run to view the result.
  8. Click Save to add the operator to the playbook.
  9. Click Cancel to discard the operator form.

Usage Details

callScript(inputTable, scriptName, args, timeBetweenCalls)

Input:
inputTable: Table containing the data to run this operator on.
scriptName: Name of the script as appear on the Scripts page
args: Column names to use as arguments for the script
timeBetweenCalls: Time between calls

Output:
new lhub_id, exitCode, error and output for each row. If the output is a list of JSON objects (e.g. rows), the output explodes the array into rows and extracts JSON values in the JSON objects into new columns.

args are the columns names (such as ["user", "accessCode"]). From the script they are accessed using sys.argv[1] and so on. To produce output to the table, you must print the result. See examples below for more information.

Example 1

Table = "users"

firstName lastName emailAddress
EmilBilgazyev[email protected]

Create the following Python script file, save it as scriptOne.py, then drag and drop this file to Automations > Scripts page.

import sys

# sys.argv[1] is value of firstName column
# sys.argv[2] is value of lastName column
# sys.argv[3] is value of emailAddress column
# ordering is important

email = sys.argv[3] # [email protected]
work = email.split('@')[1].split()[0] #logichub

print("%s %s works at %s" % (sys.argv[2].upper(), sys.argv[1], work.upper()))

Create a computation step and in the LQL write following line.

callScript(users, "scriptOne.py", ["firstName", "lastName", "emailAddress"], "1 s")
-- Note: arguments (columns) ordering are important, you need to remember which columns is at which index

Output

firstName lastName emailAddress output exitCode error
EmilBilgazyev[email protected]BILGAZYEV Emil works at LOGICHUB

Example 2

If the script produces multiple rows for each row, you must print them as an array of JSON objects that will get populated into columns and rows.

import sys
fn = sys.argv[1]
ln = sys.argv[2]
ea = sys.argv[3]
print('[{"FIRSTNAME":"%s", "length":%d}, {"LASTNAME":"%s", "length":%d}, {"EMAILADDRESS":"%s", "length":%d}]'%(fn, len(fn), ln, len(ln), ea, ln(ea)))

Output

firstName lastName emailAddress output FIRSTNAME LASTNAME EMAILADDRESS length exitCode error
EmilBilgazyev[email protected] JSON objectEMIL4
EmilBilgazyev[email protected]JSON objectBILGAZYEV9
EmilBilgazyev[email protected]JSON object[email protected]17

For 1 row, 3 rows were produced. Because each row has a different JSON schema.

  • FIRSTNAME, length
  • LASTNAME, length
  • EMAILADDRESS, length

The operator did the union of all field names, and produced new columns for each field name.


Did this page help you?