Running scripts in a wrapped stage in DataStage
You can wrap an executable script and turn it into a DataStage® stage.
You can wrap scripts into stages in DataStage. Sample scripts in this topic include Python, Netezza®, and Db2® scripts.
Creating a wrapped stage with a script
After you create the wrapped stage with your script, you can include the new stage in your DataStage flows. Wrapped stages that you create are visible in canvas under User-defined stages and on the Assets tab of your project in the "DataStage components" list.
Example: Python script
The following example shows how to create a wrapped stage that contains a Python script. The example demonstrates a simple use case to search for the largest input integer. See Defining wrapped stages in DataStage for details on the information in the example.
- Source code
- See the following source code for this example. It reads the input integer, then finds and
outputs the largest number.
# Searches through a list of input numbers and finds smallest or largest number # Syntax: python search_num.py <arg> # where arg=-S or -L import sys numStr = "" i = 0 min_max = 0 newStr = 0 arg = "Largest" if len(sys.argv) > 1: arg = sys.argv[1] if arg.lower() == "-s": arg = "Smallest" else: arg = "Largest" for line in sys.stdin: if line.strip() != "": strVal = line.strip(); newStr=strVal.replace('"','') if (i == 0): min_max = newStr numStr = '"' + line.strip() + '"' i = 1 else: numStr = numStr + ', "' + line.strip() + '"' intNum = int(newStr) intMax = int(min_max) if arg == "Smallest": if (intNum < intMax): min_max = newStr else: if (intNum > intMax): min_max = newStr print(" [Numbers:" + numStr + "]. " + arg + " number: " + str(min_max))
- Create your Python script by using the source code in this example. Save the script as
search_num.py. As a cluster administrator, create a directory
/ds-storage/tools/python3.9.9/python/ and locate your script
search_num.py there.
If multiple PX runtime instances are involved, you must create the directories and locate the scripts in them for each instance.
- From your project, on the Assets tab, click .
- Provide a name and optional description. Then, click Create.
- Specify details on the General tab:
- Wrapper name
- Execution mode: Parallel only
- Command (script information). For
example,
/ds-storage/tools/python3.9.9/python/ds-storage/search_num.py
- Optional description
- Click Add property + to add arguments for the script.:
- Name: Arg
- Data type: String
- Prompt: Argument
- Default: 10
- Repeats column: cleared
- Conversion: -Name Value
- Click Save.
- On the Wrapped tab, on the Input page, specify input ports and the properties:
- Click Add link +.
- Click in the Data definition column to select a table that describes the metadata for the port.
- Repeat step 7 for the Output ports.
- Click Save, then click Generate.

Example: Netezza script
The following example shows how to connect to a Netezza database, then run a simple query against the database. See Defining wrapped stages in DataStage for details on the information in the example.
- From your project, on the Assets tab, click .
- Provide a name and optional description. Then, click Create.
- Set Mode for "Sequential only" to execute commands only once.
- On the Properties tab, set host and port properties.
- On the Wrapped tab, for Exit codes, select All Exit codes successful to prevent failure on error.
- Click Generate.
- Create a DataStage flow with the new stage.
- Create your set of NZ-SQL statements and pass them as an input to the wrapped stage. The example
uses the Row Generator stage to pass a blank input to the Transformer stage, which generates three
SQL statements that are passed through the Funnel stage and into the Wrapped stage. This example
shows a Transformer output that defines an SQL statement. The example flow uses three outputs.
- Compile and run job.
Example: Db2 script
The following example shows how to connect to Db2 database, then run a simple query against the Db2 database. See Defining wrapped stages in DataStage for details on the information in the example.
- From your project, on the Assets tab, click .
- Provide a name and optional description. Then, click Create
- On the General tab of the new stage, set the Execution mode to Parallel
only and specify a command of
db2 -s -v
. - On the Wrapped tab, specify input ports and their properties.
- Create a data definition asset to define the input schema for the wrapped stage. Use a single column of type VARCHAR (255) to input SQL statements.
- Create a DataStage flow with a Row Generator stage, a Transformer stage, a funnel stage, and the new wrapped stage as a target.
- Configure the Row Generator stage to pass an input to the Transformer stage.
- Configure that Transformer stage to generate your SQL statements and add any job parameters.
Pass them through the funnel stage as an input to the wrapped stage. This example shows a
Transformer output that defines an SQL statement. The example flow uses three outputs.
- Compile and run job.
