使用Python连接到Teradata

时间:2017-03-04 18:03:18

标签: python teradata pandas-to-sql

我正在尝试连接到teradata服务器并使用python将数据帧加载到表中。这是我的代码 -

import sqlalchemy 

engine = sqlalchemy.create_engine("teradata://username:passwor@hostname:port/")

f3.to_sql(con=engine, name='sample', if_exists='replace', schema = 'schema_name')

但是我收到以下错误 -

InterfaceError: (teradata.api.InterfaceError) ('DRIVER_NOT_FOUND', "No driver found for 'Teradata'.  Available drivers: SQL Server,SQL Server Native Client 11.0,ODBC Driver 13 for SQL Server")

有人可以帮我弄清楚我的方法有什么不对吗?

4 个答案:

答案 0 :(得分:3)

在Python中连接Teradata的方法有很多种。以下列表并非详尽无遗。

<强> SQLAlchemy的

如果您希望使用SQLAlchemy,则还需要安装包SQLAlchemy-Teradata。以下是您可以联系的方式:

from sqlalchemy import create_engine
from sqlalchemy.ext.declarative import declarative_base, DeferredReflection
from sqlalchemy.orm import scoped_session, sessionmaker

[...]

# Connect
engine = create_engine('teradata://' + user + ':' + password + '@' + host + ':22/' + database)
db_session = scoped_session(sessionmaker(autocommit=False, autoflush=False, bind=engine))
db_session.execute('SET SESSION CHARACTERISTICS AS TRANSACTION ISOLATION LEVEL READ UNCOMMITTED;')  # To avoid locking tables when doing select on tables
db_session.commit()

Base = declarative_base(cls=DeferredReflection)
Base.query = db_session.query_property()

然后您可以使用db_session进行查询。见SQLAlchemy Session API

<强> Pyodbc

如果您想使用Pyodbc,首先需要在您的计算机上安装Teradata驱动程序。我的示例,在安装Teradata驱动程序后,我在/etc/odbcinst.ini

中有以下条目
[Teradata]
Driver=/opt/teradata/client/16.00/odbc_64/lib/tdata.so
APILevel=CORE
ConnectFunctions=YYY
DriverODBCVer=3.51
SQLLevel=1

然后我可以连接以下内容:

import pyodbc
[...]

#Teradata Connection
connection= pyodbc.connect("driver={Teradata};dbcname=" + host + ";uid=" + user + ";pwd=" + pwd + ";charset=utf8;", autocommit=True)
connection.setdecoding(pyodbc.SQL_CHAR, encoding='utf-8')
connection.setdecoding(pyodbc.SQL_WCHAR, encoding='utf-8')
connection.setdecoding(pyodbc.SQL_WMETADATA, encoding='utf-8')
connection.setencoding(encoding='utf-8')

cursor= n.cursor()
cursor.execute("Select 'Hello World'")
for row in cursor:
    print (row)

答案 1 :(得分:1)

要连接teradata数据库,你需要pyodbc,我也有teradata方言的问题。

示例:

import pyodbc

user = 'user'

pasw = 'pass'

host = 'host'

connection = pyodbc.connect('DRIVER=Teradata;DBCNAME=' + host +';UID=' + user + ';PWD=' + pasw +';QUIETMODE=YES', autocommit=True,unicode_results=True)

答案 2 :(得分:0)

我不确定你为什么使用sqlalchemy。但您可以探索使用Teradata模块连接到Teradata,如另一个链接中所述: Connecting Python with Teradata using Teradata module

答案 3 :(得分:0)

我在气流中遇到了类似的问题,我用jar和jaydebeapi连接了teradata数据库并执行sql:

[root@myhost transfer]# cat test_conn.py 
import jaydebeapi
from contextlib import closing


jclassname='com.teradata.jdbc.TeraDriver'
jdbc_driver_loc = '/opt/spark-2.3.1/jars/terajdbc4-16.20.00.06.jar,/opt/spark-2.3.1/jars/tdgssconfig-16.20.00.06.jar'
jdbc_driver_name = 'com.teradata.jdbc.TeraDriver'
host='my_teradata.address'

url='jdbc:teradata://' + host + '/TMODE=TERA'
login="teradata_user_name"
psw="teradata_passwd"

sql = "SELECT COUNT(*) FROM  A_TERADATA_TABLE_NAME where month_key='202009'"


conn = jaydebeapi.connect(jclassname=jdbc_driver_name,
                                  url=url, 
                                  driver_args=[login, psw],
                                  jars=jdbc_driver_loc.split(","))

with closing(conn) as conn:
    with closing(conn.cursor()) as cur:
        cur.execute(sql)
        print(cur.fetchall())
   
[root@myhost transfer]# python test_conn.py
[(7734133,)]
[root@myhost transfer]#