Teradata Python Module

Tools
Tools covers the tools and utilities you use to work with Teradata and its supporting ecosystem. You'll find information on everything from the Teradata Eclipse plug-in to load/extract tools.

Re: Teradata Python Module

I am repeatedly getting socket when trying to connect to Teradata:

Error: Teradata.api.DatabaseError: (65, "[08001] [Teradata][Unix system error]  65 Socket error - The Teradata server can't currently be reached over this network")

Anyhelp would be great... 

Re: Teradata Python Module

Hi Eric,

I've a requirement of running same sql query with 10 different users by logging on to bteq. I've used teradata python module for this & my code looks like below.

The problem here is, Connection establishment & executing the sql query is running  2 times for 2nd iteration & 3 times for 3rd iteration in loop &  so on. so, for last iteration is running for 9 times.

I suspect that problem is not closing the previous connenction causes the issue, even i tried with "session.close()" method, but it is not working.

Could you please have a look ?.

import teradata

for a in range(1,10):

    user="USER"+str(a)

    pwd="PASSWORD"+str(a)

    udaExec = teradata.UdaExec (appName="sample",version="1.0",logConsole=True,logDir="./logs")

    with udaExec.connect(method="odbc", system="[machinename]", username=user, password=pwd) as session:

       session.execute("select * from dbc.dbcinfo")

Thanks

nani

Teradata Employee

Re: Teradata Python Module

@nani -  Move the line "udaExec = teradata.UdaExec(..." above the "for" loop.   udaExec should only be initializd once.   Its not that the query is being executed 9 times but rather the logging system is initialized multiple times and so is logging messages multiple times.

@Jagadish - Make sure that the value for the "system" parameter is a valid tdpid and that it can be resolved to an IP address.

@bchu - You are missing the line in your program that initializes udaExec (e.g. "udaExec = teradata.UdaExec(...").  See the Hello World program for an example.

Enthusiast

Re: Teradata Python Module

I am executing a query and I want to iterate over the result set several times. The problem is that if i iterate over the result set once, I can't do it again. Looks like the result set is exhausted and no more available for any further computation.

Please see the below code for details and suggest how can I preserve the result set.

import teradata

class DB():
def __init__(self):
udaExec = teradata.UdaExec (appName="HelloWorld", version="1.0",logConsole=False)
session = udaExec.connect(method="odbc", system="tddemo",username="dbc", password="dbc")
self.session = session

def fun1(self):
rows = self.session.execute("SELECT databasename, ownername FROM DBC.DATABASES where DatabaseName='financial'")
return rows

db = DB()
rows = db.fun1()

# This loop prints accurate result like
#Row 1: [financial, Samples]
for row in rows:
print(row)

# This loop does not print anything
for row in rows:
print(row)

# This line also gets printed
print("The End")

Thanks

Enthusiast

Re: Teradata Python Module

Highlighted
Enthusiast

Re: Teradata Python Module

I am trying to fetch table ddl with the help of SHOW TABLE statement and it behaves weird and only returns a few words out of whole DDL. Please see my attempt and error below

import teradata
class DB():
def __init__(self):
udaExec = teradata.UdaExec (appName="test", version="1.0",logConsole=False)
session = udaExec.connect(method="odbc", system="tddemo",username="dbc", password="dbc")
self.session = session

def fun1(self):
# session.execute("create table financial.dummytable1(a varchar(10))")
rows = self.session.execute("SHOW TABLE financial.dummytable1")
for row in rows:
print(row)
db = DB()
db.fun1()

print("---The End---")

Here is the unexpected result

PRIMARY INDEX ( a );]HARACTER SET LATIN NOT CASESPECIFIC)CK ,
---The End---

Desired result

CREATE SET TABLE financial.dummytable1 ,NO FALLBACK ,
NO BEFORE JOURNAL,
NO AFTER JOURNAL,
CHECKSUM = DEFAULT,
DEFAULT MERGEBLOCKRATIO
(
a VARCHAR(10) CHARACTER SET LATIN NOT CASESPECIFIC)
PRIMARY INDEX ( a );
---The End---

Please help me understand what's going on here.

Thanks

Ali

Teradata Employee

Re: Teradata Python Module

@Ali - The reason the results of SHOW TABLE were not displayed properly on the terminal is because the newline characters in the result were most likely carriage returns instead of line feeds.  To ensure it displays correctly, you can split the output based on the presence of any newline sequence and print the lines individually.  E.g.

import re
for line in re.split("\r\n|\n\r|\r|\n", row[0]):
print(line)

 



Enthusiast

Re: Teradata Python Module

Thanks for your help. That worked. But took a few minute to write the exact required code, which is here

import teradata
import re
class DB():
def __init__(self):
udaExec = teradata.UdaExec (appName="test", version="1.0",logConsole=False)
session = udaExec.connect(method="odbc", system="tddemo",username="dbc", password="dbc")
self.session = session

def fun1(self):
rows = self.session.execute("SHOW TABLE financial.dummytable1")
for row in rows:
for line in re.split("\r\n|\n\r|\r|\n", row[0]):
print(line)

db = DB()
db.fun1()

print("---The End---")
Teradata Employee

Re: Teradata Python Module

I am trying to execute the Printtable.py script using external configuration file.The file udaexec.ini is in the same location as the python file.

Script:

import teradata

udaExec = teradata.UdaExec ()

with udaExec.connect("${dataSourceName}") as session:

    for row in session.execute("SELECT * FROM ${table}"):

        print(row)

Configuration File:

# Application Configuration

[CONFIG]

appName=Printtable

version=2

logConsole=False

dataSourceName=E_Dev

table=DWH_STAGE.EMP

# Default Data Source Configuration

[DEFAULT]

method=odbc

charset=UTF8

# Data Source Definition

[E_Dev]

system=100.65.07.120

username=XXX

password=XXX

Error:

C:\Python\Teradata_Python_Module>python C:\Python\Teradata_Python_Module\Practice\Printtable.py

Traceback (most recent call last):

  File "C:\Python\Teradata_Python_Module\Practice\Printtable.py", line 3, in <module>

    udaExec = teradata.UdaExec ()

  File "build\bdist.win32\egg\teradata\udaexec.py", line 106, in __init__

  File "build\bdist.win32\egg\teradata\udaexec.py", line 575, in resolve

  File "build\bdist.win32\egg\teradata\udaexec.py", line 596, in _resolve

teradata.api.InterfaceError: ('CONFIG_ERROR', 'appName is a required field, it must be passed in as a parameter or specified in a config file.')

Teradata Employee

Re: Teradata Python Module

Is there some way to get the names of the columns returned from a query?