td_set_context | Teradata Package for R - td_set_context - Teradata Package for R

Teradata® Package for R User Guide

Deployment
VantageCloud
VantageCore
Edition
Enterprise
IntelliFlex
VMware
Product
Teradata Package for R
Release Number
17.20
Published
March 2024
Language
English (United States)
Last Update
2024-04-09
dita:mapPath
efv1707506846369.ditamap
dita:ditavalPath
ayr1485454803741.ditaval
dita:id
nqx1518630623256
Product Category
Teradata Vantage

A context must be initialized before executing analytic functions. Use the td_set_context function to initialize or update a context using an already established connection.

  • If there is no previous context, then a new context is initialized.
  • If the context already exists and the connection object "con" is created using tdplyr::NativeDriver() (as in Example 2), then the context attribute "temp.database" is updated if it is different compared to that of previous context.

    Since the connection parameters "host", "user" ("uid") and "database" (that is, the default schema) are the same for both "con" and the connection object from the context (because dbConnect on NativeDriver object initializes the context), the previous context created by dbConnect is not overwritten and all nonpersistent work tables remain in the session.

  • If the context already exists and the connection object "con" is created using teradatasql::TeradataDriver() (as in Example 3), then:
    • If the connection parameters of "con" are different from those of the connection object from the previous context, then the previous context is overwritten and all nonpersistent work tables are removed.
    • If the connection parameters are the same for both "con" and the connection object from the previous context, then the previous context is not overwritten and all nonpersistent work tables remain in the session.

    In this case, the new connection object is retrieved using td_get_context()$connection before using it.

Required argument:
  • con: Specifies a DBIConnection object with a Teradata or a TeradataConnection subclass.
Optional argument:
  • temp.database: Specifies the database name used to create temporary database objects (table or view) for processing data. Such objects are garbage collected at the end of the session. If a user wants to specifically use a known database for temporary objects, the database name can be specified using this argument.
    • Make sure the user has privilege to create objects in this database, otherwise the functions that create temporary objects fail.
    • If this argument is not specified, default database of the connection is used for temporary objects.
    • To get the temporary database name, use function td_get_context.

Example 1: Create a new context and switch its temporary database without removing nonpersistent work tables

con <- td_crate_context(host = "<dbcname>", uid = "<tduser>",
                         pwd = "<tdpwd>", dType = "native")
# Get the current context information.
td_get_context()
# Change temporary database.
td_set_context(con, temp.database = "database_for_transient_objects")
# Get the current and changed context information.
td_get_context()
# Remove the current context.
td_remove_context()

Example 2: Use dbConnect() to connect to Analytics Database through NativeDriver() and set context using different temporary database

This example uses dbConnect() function to connect to Analytics Database through tdplyr NativeDriver() and set context using different temporary database to change temp.database of an existing context without removing nonpersistent work tables.

con <- DBI::dbConnect(tdplyr::NativeDriver(), "<dbcname>", "<tduser>",
                      "<tdpwd>")
# Set context using different temporary database.
td_set_context(con, temp.database = "database_for_transient_objects")
# Remove the current context.
td_remove_context()

Example 3: Use dbConnect() to connect to Analytics Database through Teradata SQL Driver

This example uses dbConnect() function to connect to Analytics Database through Teradata SQL Driver, TeradataDriver().

con <- DBI::dbConnect(teradatasql::TeradataDriver(),
                      '{"host":"<dbcname>","user":"<tduser>","password":"<tdpwd>"}')
# Set context using different temporary database.
td_set_context(con, temp.database = "database_for_transient_objects")
# The td_set_context() function just sets the context and does not return
# the connection object. Hence, get the connection object as follows.
con <- td_get_context()$connection
# Remove the current context.
td_remove_context()

Example 4: Manage multiple connections

con1 <- DBI::dbConnect(teradatasql::TeradataDriver(),
                       '{"host":"<dbcname>","user":"<tduser>","password":"<tdpwd>"}')
con2 <- DBI::dbConnect(teradatasql::TeradataDriver(),
                       '{"host":"<dbcname>","user":"<tduser>","password":"<tdpwd>"}')
# Initialize the context with 'con1'.
td_set_context(con = con1, temp.database = "database_for_transient_objects")

# Get the current context information.
td_get_context()
# Initialize the context with 'con2'. This overwrites current context and
# 'con1' is disconnected.
td_set_context(con = con2)

# Get the current context information.
td_get_context()
# Remove the current context.
# Note: This performs garbage collection and disconnects current context
#       connection i.e., 'con2'.
td_remove_context()