execute_script | Script Method | Teradata Python Package - execute_script - Teradata Package for Python

Teradata® Package for Python User Guide

Product
Teradata Package for Python
Release Number
17.00
Published
November 2021
Language
English (United States)
Last Update
2022-01-14
dita:mapPath
bol1585763678431.ditamap
dita:ditavalPath
ayr1485454803741.ditaval
dita:id
B700-4006
lifecycle
previous
Product Category
Teradata Vantage

Use the execute_script to run script on Vantage.

This method does not have any arguments.

Example

This example shows a workflow to create a Script object, install Python script on Vantage, runs it and then removes the script from Vantage.

The script "mapper.py" reads in a line of text input ("Old Macdonald Had A Farm") from csv and splits the line into individual words, emitting a new row for each word.

To run this example, "mapper.py" and "barrier.csv" are required and must be present under the same location specified by the argument files_local_path.
  • "barrier.csv" is present under <teradataml_install_location>/teradataml/data directory.
  • "mapper.py" can be created as follows:
    #!/usr/bin/python
    import sys
    for line in sys.stdin:
        line = line.strip()
        words = line.split()
        for word in words:
            print ('%s\t%s' % (word, 1))
  • Load example data.
    >>> load_example_data("Script", ["barrier"])
  • Import required packages.
    >>> from collections import OrderedDict
    >>> from teradatasqlalchemy import (VARCHAR)
  • Create teradataml DataFrame.
    >>> barrierdf = DataFrame.from_table("barrier")
  • Create a Script object that allows user to run script on Vantage.
    >>> sto = Script(data=barrierdf,
                script_name='mapper.py',
                files_local_path= 'data/scripts',
                script_command='python3 ./<database name>/mapper.py',
                data_order_column="Id",
                is_local_order=False
                delimiter=',',
                nulls_first=False,
                sort_ascending=False,
                charset='latin', returns=OrderedDict([("word", VARCHAR(15)), ("count_input", VARCHAR(2))])
                )
  • Install script on Vantage.
    >>> sto.install_file(file_identifier='mapper', file_name='mapper.py', is_binary=False)
  • Set the search path to the database where the file is installed.
    >>> get_context().execute("SET SESSION SEARCHUIFDBPATH = <database name>;")
  • Run the user script on Vantage.
    >>> sto.execute_script()
    ############ STDOUT Output ############
     
            word count_input
    0  Macdonald           1
    1          A           1
    2       Farm           1
    3        Had           1
    4        Old           1
    5          1           1
  • Remove the installed file from Vantage.
    >>> sto.remove_file(file_identifier='mapper', force_remove=True)