Invoking external scripts

Once a Kognitio external script has been created as an SQL object (unless you’re using anonymous inline scripts) , they can be called on or invoked using the methods outlined in this section.

The examples from this page will include some external script creation but assume that you have script environments in place already.

Refer to the external script invocation syntax for more information about syntax and definitions.

Simple invocation

Once you’ve created an external script, you invoke it using SQL

EXTERNAL SCRIPT script_name

Simple example - listing environment variables

Each script invocation returns the environment variable env_var and associated value listed using the item iteration method available in the standard python os package.

CREATE EXTERNAL SCRIPT myschema.env_vars ENVIRONMENT python27
SENDS(env_var VARCHAR, value VARCHAR)
SCRIPT S'EOF(
#
#Import packages
import os,csv,sys
#
#Setup output stream
csvwriter = csv.writer(sys.stdout)
#
#Write out environment variables
for (k, v) in os.environ.iteritems():
    csvwriter.writerow([k, v])
#
)EOF'

To invoke this script simply submit the SQL:

EXTERNAL SCRIPT env_vars

or use the external script in a derived table:

SELECT * FROM
(EXTERNAL SCRIPT env_vars) dt1

There are other ways to invoke external scripts that are outlined in the following sections.

Data from SQL

External scripts can take data from the SQL database as input. This is defined in the RECEIVES clause, see the optional arguments section for more details on how to define the RECEIVES clause in external script creation. This is useful for performing complex analytics on your data or transforming data e.g. pivoting which is not easily done or not available in SQL.

Within native SQL

External scripts can also be used as a SQL derived table to feed the results of the external script into another SQL query for further processing. Useful for quickly processed scripts where the results change frequently or if the results of the external script need to be processed further within SQL.

Anonymously as an SQL query

Invoke external scripts without having to create them as a Kognitio object a schema. This is useful for developing scripts since changes are likely to be made, for example if you are experimenting with the advanced configurations to find the optimum amount of resources or testing package imports. Another use case is for external scripts that only need to be used once in a script. This prevents clutter building up in schemas.

Default environment variables

Access standard environment variables in external scripts which give them information about what and where they are run from. This includes Kognitio specific ones such as the node they are run on or the session number and are set by default upon execution.

Custom environment variables

Users can also send custom defined variables to external scripts as environment variables. Useful as flags e.g. forecasting script with two models, run dependent on the flag value or arguments such as critical values to use for statistical tests. If using external scripts to generate data, can be used to alter the amount of data to produce.