Using environment variables to pass parameters into external scripts

In addition to the environment variables set up automatically by Kognitio it is possible to use environment variables to pass parameters into external scripts. These are specified in the external script invocation call. Note when these parameters are passed into external scripts by Kognitio they are automatically capitalized and prefixed with WX2_.

Note the syntax for custom parameters:

EXTERNAL SCRIPT script-name
[PARAMETERS param-name1=value1, ...]
[FROM (sql-input-query)]

Example: python script using environment variables for generating pseudo-random numbers

Below is an example of an inline external script utilising environment variables to pass parameters into external scripts. The script returns a set of pseudo-random integers between 1 and 10, by using WX2_CONNECTOR_NUM as a random seed.

CREATE EXTERNAL SCRIPT myschema.getrand ENVIRONMENT PYTHON27
SENDS(res int)
SCRIPT S'EOF(
#
#Package import
import os, csv, sys, random
#
#Input and output streams
input = csv.reader(sys.stdin)
result = csv.writer(sys.stdout)
#
#Environment variables
sta = int(os.environ["WX2_START"])
fin = int(os.environ["WX2_FINISH"])
s = int(os.environ["WX2_CONNECTOR_NUM"])
sd = int(os.environ["WX2_SEED"])
#
#Seed and write out results
random.seed(s*sd)
result.writerow([random.randint(sta,fin)])
)EOF'

The script is called using the SQL:

EXTERNAL SCRIPT myschema.getrand
PARAMETERS start=1, finish=10, seed=5;

The environment variable WX2_CONNECTOR_NUM is used as part of the seed so that each thread returns a different pseudo-random number, as this variable will have a different value on each thread.

In addition to the environment variable, 3 parameters are passed to the script. The start and finish parameters specify the range within which the random results should fall. The seed parameter is included so that by changing the seed on subsequent calls of the script, different sets of randoms can be obtained but results are repeatable if required.