1. To access your Redshift from Python, you must install pyodbc package. Install it by running the below command
2. Now use a sample Python program like below to access your data from Redshift
import
pyodbc
conn
=
pyodbc.connect(
'Driver={DataDirect 8.0 Amazon Redshift Wire Protocol}; HostName=redshift-cluster-1.cy1mp8nn6ntk.us-west-2.redshift.amazonaws.com; Database=dev; UID=awsuser; PWD=Galaxy472; Port=5439'
)
cursor
=
conn.cursor()
## Create Tables
cursor.execute(
"CREATE TABLE Track ( TrackId INT NOT NULL, Name VARCHAR(200) NOT NULL, AlbumId INT, MediaTypeId INT NOT NULL, GenreId INT, Composer VARCHAR(220), Milliseconds INT NOT NULL, Bytes INT, UnitPrice NUMERIC(10,2) NOT NULL);"
)
cursor.execute(
"INSERT INTO Track (TrackId, Name, AlbumId, MediaTypeId, GenreId, Composer, Milliseconds, Bytes, UnitPrice) VALUES (1, 'For Those About To Rock (We Salute You)', 1, 1, 1, 'Angus Young, Malcolm Young, Brian Johnson', 343719, 11170334, 0.99);"
)
conn.commit()
##Access Data using SQL
cursor.execute(
"select * from Track"
)
while
True
:
row
=
cursor.fetchone()
if
not
row:
break
print
(row)
##Access Data using SQL
cursor.execute(
"select * from Artist"
)
while
True
:
row
=
cursor.fetchone()
if
not
row:
break
print
(row)