As a data scientist, we need to extract data inevitably. Before extracting data, we should firstly connect to database. In this blog, I’ll talk about how to connect to Teradata Database with following points:
- Required packages
- Connecting to Teradata database
- Extracting data with SQL query
Required packages
teradata
module is a freely available, open source, library for the Python
programming language, whose aim is to make it easy to script powerful
interactions with Teradata Database. It adopts the philosophy of udaSQL,
providing a DevOps focused SQL Execution Engine that allows developers to focus
on their SQL and procedural logic without worrying about Operational
requirements such as external configuration, query banding, and logging.
Connecting to Teradata database
Let’s read this example line by line: The first line initializes the UdaExec
framework that provides DevOps support features such as configuration and
logging. We tell UdaExec the name and version of our application during
initialization so that we can get feedback about our application in DBQL and
Teradata Viewpoint as this information is included in the QueryBand of all
database sessions created by our script. We also tell UdaExec not to log to
the console (e.g. logConsole=False
) so that our print statement is easier to
read. The second line creates a connection to a Teradata system named “tdprod”
using ODBC
as the connection method, and specify USEREGIONALSETTINGS="N"
to
ensure that float values can be loaded and make decimal separator be ‘.’
Extracting data with SQL query
After creating the connection to Teradata, we can extract data by SQL query.
We can apply session
in pandas.read_sql()
in order to executing SQL query
employee_req
.
Reference
- “Teradata Python Module”, Developer.teradata.com, 2015. [Online]. Available: https://developer.teradata.com/tools/reference/teradata-python-module
- E. Scheie, O. Ratzesberger and B. Lewis, “Teradata/PyTd”, GitHub, 2015. [Online]. Available: https://github.com/Teradata/PyTd
- “Teradata Logo.png”, En.wikipedia.org, 2012. [Online]. Available: https://en.wikipedia.org/wiki/File:Teradata_Logo.png