Teradata Clearscape Analytics - Python & Teradata SQL Connection @teradata @teradataml

Data Owls
Data Owls
1.1 هزار بار بازدید - 8 ماه پیش - Source from :
Source from :
https://www.teradata.com/Platform/Cle...
https://quickstarts.teradata.com/tera...
----------------------------------------------------------------------------
Teradata SQL on Clearscape Jupyter Script:
%connect local
select user;
select user;
----------------------------------------------------------------------------
Teradata Python connection on Clearscape Jupyter:
getpass to ask password to user and prevent storing it plain in the Notebook
import getpass
import pandas as pd
import all Teradataml functions and supporting libraries
from teradataml import *
from teradataml.table_operators.Script import Script
from sqlalchemy import func
eng = create_context(host = 'host.docker.internal', username='demo_user', password = '')
print(eng)
query = '''SELECT * from dbc.databasesv;'''
tbl_df=DataFrame.from_query(query)
tbl_df    

----------------------------------------------------------------------------
Teradata Python Connection from anywhere:
import teradataml

DB_URL = "host.docker.internal"                                 #Add Host
USER = "demo_user"                                   #Add Username
PASS = ""                                   #Add Password

try:
   Establish a connection to the Teradata database
   with teradatasql.connect(host=DB_URL, user=USER, password=PASS) as con:
       Create a cursor to execute queries
       with con.cursor() as cur:
           try:
               Creating the table SampleEmployee
               cur.execute(f"select user;")
               for p in cur:
                   print(f"{p[0]}")
               
               Execute the SELECT query to get the results from SampleEmployee table
               cur.execute(f"SELECT * FROM {USER}.SampleEmployee")

               Extract data from the result set and print it
               for row in cur:
                   print(f"Associate ID: {row[0]}, Associate_Name: {row[1]}, Job_Title:{row[2]}")

               

           except teradatasql.DatabaseError as db_err:
               Handle any errors that occur during query execution
               print("Error while executing the query:", db_err)

except teradatasql.DatabaseError as db_err:
   Handle any errors that occur during the database connection
   print("Error while connecting to the Teradata database:", db_err)
8 ماه پیش در تاریخ 1402/09/04 منتشر شده است.
1,171 بـار بازدید شده
... بیشتر