Create, Insert, Delete, Update Operations on Teradata via JDBC in Python

access_time 7 months ago visibility1177 comment 0

Python JayDeBeApi module allows you to connect from Python to Teradata databases using Java JDBC drivers. In article Connect to Teradata database through Python, I showed how to use teradata package to connect to Teradata via Teradata ODBC driver. This article demos how to use this JayDeBeApi package.

About JayDeBeApi

Find more details about JayDeBeApi from the this pypy index page: https://pypi.org/project/JayDeBeApi/.

Make sure Python and pip is installed in your machine. 

Run the following command to install this package.

pip install JayDeBeApi

About Teradata JDBC driver

Teradata JDBC driver is required.

The following two JAR files are required:

  • tdgssconfig.jar
  • terajdbc4.jar

You can download it from:

  1. Teradata Download: https://downloads.teradata.com/download/connectivity/jdbc-driver
  2. Maven Repository: https://mvnrepository.com/artifact/com.teradata.jdbc
  3. Or any other repositories.

Code snippet

Similar as the SQL Server example (Connect to SQL Server via JayDeBeApi in Python), you can connect to Teradata using JayDeBeApi.

import jaydebeapi

database = "test"
table = "Employees"
user = "zeppelin"
password = "zeppelin"
driver = 'com.teradata.jdbc.TeraDriver'

conn = jaydebeapi.connect(driver,
                        f'jdbc:teradata://myserver/Database={database},LOGMECH=LDAP',
                          [user, password],
                          ["/path/to/terajdbc4.jar","/path/to/tdgssconfig.jar"])
curs = conn.cursor()

curs.execute('create table test.test_jaydebeapi'
             '("ID" INTEGER not null,'
             ' "NAME" VARCHAR not null,'
             ' primary index ("ID"))'
             )
curs.execute("insert into test.test_jaydebeapi values (1, 'Raymond')")
curs.execute("select * from test.test_jaydebeapi")
curs.execute("update test.test_jaydebeapi set NAME='RAY' WHERE ID=1")
curs.fetchall()
curs.execute("delete from test.test_jaydebeapi ALL")
curs.close()
conn.close()

In the above code snippet, the connection is established using connect function. Login mechanism is LADP and you can remove it if you use database user name and password to login. Remember to change the JAR file paths accordingly (absolute paths are preferable). 

Once connection is established, you can create a cursor object through which you can perform the following actions:

  • CREATE/DROP tables/other objects
  • SELECT
  • INSERT
  • DELETE
  • UPDATE
  • or any other valid Teradata SQL statement.

For SELECT statement, you can use fetchall or fetchfirst functions to retrieve records. 

References

Load Data from Teradata in Spark (PySpark)

Quick question - why do we not to use PySpark JDBC read/write functions to perform transactional operations against Teradata database? 

info Last modified by Administrator at 3 months ago copyright This page is subject to Site terms.
Like this article?
Share on

Please log in or register to comment.

account_circle Log in person_add Register

Log in with external accounts

Want to publish your article on Kontext?

Learn more

Kontext Column

Created for everyone to publish data, programming and cloud related articles.
Follow three steps to create your columns.


Learn more arrow_forward

More from Kontext

local_offer python local_offer python-file-operations

visibility 23
thumb_up 0
access_time 12 months ago

JSON is commonly used in modern applications for data storage and transfers. Pretty much all programming languages provide APIs to parse JSON. 

local_offer python local_offer spark local_offer pyspark local_offer spark-advanced

visibility 9289
thumb_up 0
access_time 2 years ago

For SQL developers that are familiar with SCD and merge statements, you may wonder how to implement the same in big data platforms, considering database or storages in Hadoop are not designed/optimised for record level updates and inserts. In this post, I’m going to demonstrate how to implement ...

local_offer teradata local_offer SQL

visibility 642
thumb_up 0
access_time 12 months ago

JSON is commonly used in modern applications for data storage and transfers. Pretty much all programming languages provide APIs to parse JSON. 

About column