create_context - Teradata Python Package

Teradata® Python Package User Guide

Product
Teradata Python Package
Release Number
16.20
Published
February 2020
Language
English (United States)
Last Update
2020-02-29
dita:mapPath
rkb1531260709148.ditamap
dita:ditavalPath
Generic_no_ie_no_tempfilter.ditaval
dita:id
B700-4006
lifecycle
previous
Product Category
Teradata Vantage

Use the create_context() function to create a connection to Vantage using the teradatasql and teradatasqlalchemy DBAPI and dialect combination.

You can pass all required parameters (host, username, password) for establishing a connection to Teradata, or pass a sqlalchemy engine to the tdsqlengine parameter to override the default DBAPI and dialect combination. You can create connection to Vantage enabled with various security mechanisms.

There can be scenarios, where connecting user do not have permissions to create tables or views in his own database, but is allowed to create the required objects in other common database. In such scenarios, user must use parameter temp_database_name in create_context. User must pass the database name where he or she is allowed to create the database objects such as 'table/view'. In case this argument is not provided, then the intermediate database objects are created in connecting users default database.

  • When overwriting an existing context associated with a Vantage connection, most of the operations on any teradataml DataFrames created before will not work.
  • Teradata recommends calling remove_context() to end a session, so that intermediate views/tables created by teradataml are garbage collected.
  • Executing create_context() triggers garbage collection in teradataml.

    See Garbage Collection in teradataml for more details.

  • teradataml expects the client environments are already setup with appropriate security mechanisms and are in working conditions.

    For more information, please refer to Teradata Vantage™ - Advanced SQL Engine Security Administration.

  • Passwords passed as part of either password or logdata parameters can also be in encrypted format using Stored Password Protection. Examples section below demonstrates passing encrypted password to both password and logdata parameters.

    For more information on Stored Password Protection and how to generate key and encrypted password file used in examples, see the Stored Password Protection section under the teradatasql project, on https://pypi.org/user/teradata/.

Example: Create a context using hostname, username and password

>>> from teradataml.context.context import *
>>> create_context(host = 'tdhost', username='tduser', password = 'tdpassword')

Example: Create a context using already created sqlalchemy engine

>>> from sqlalchemy import create_engine
>>> sqlalchemy_engine  = create_engine('teradatasql://'+ tduser +':' + tdpassword + '@'+tdhost
>>> from teradataml.context.context import *
>>> create_context(tdsqlengine = sqlalchemy_engine)

Example: Create a context with default logmech 'TD2'

>>> from teradataml.context.context import *
>>> create_context(host = 'tdhost', username='tduser', password = 'tdpassword', logmech='TD2')

Example: Create a context with default logmech 'TDNEGO'

>>> from teradataml.context.context import *
>>> create_context(host = 'tdhost', username='tduser', password = 'tdpassword', logmech='TDNEGO')

Example: Create a context with default logmech 'LDAP'

>>> from teradataml.context.context import *
>>> create_context(host = 'tdhost', username='tduser', password = 'tdpassword', logmech='LDAP')

Example: Create a context with default logmech 'KRB5'

>>> from teradataml.context.context import *
>>> create_context(host = 'tdhost', username='tduser', password = 'tdpassword', logmech='KRB5')

Example: Create a context with default logmech 'KRB5

>>> from teradataml.context.context import *
>>> create_context(host = 'tdhost', logmech='JWT', logdata='token=eyJpc...h8dA')
You must use the 'logdata' parameter when using 'JWT' as logging mechanism.

Example: Create a context using encrypted password and key passed to the 'password' parameter

The password should be specified in the following format:
ENCRYPTED_PASSWORD(file:<PasswordEncryptionKeyFileName>, file:<EncryptedPasswordFileName>)
Where:
  • PasswordEncryptionKeyFileName specifies the name of a file that contains the password encryption key and associated information
  • EncryptedPasswordFileName specifies the name of a file that contains the encrypted password and associated information.
Each filename must be preceded by the 'file:' prefix. The PasswordEncryptionKeyFileName must be separated from the EncryptedPasswordFileName by a single comma.
>>> td_context = create_context(host = 'tdhost', username='tduser', password = "ENCRYPTED_PASSWORD(file:PassKey.properties, file:EncPass.properties)")

Example: Create a context using encrypted password and key passed to the 'logdata' parameter

>>> from sqlalchemy import create_engine
>>> sqlalchemy_engine = create_engine("teradatasql://@tdhost/?LOGMECH=LDAP&"
                                      "LOGDATA=tduser@@ENCRYPTED_PASSWORD(file:PassKey.properties,file:EncPass.properties)")
>>> td_context = create_context(tdsqlengine = sqlalchemy_engine)