Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to Connect to RDS Instance from AWS Glue Python Shell?

I am trying to access RDS Instance from AWS Glue, I have a few python scripts running in EC2 instances and I currently use PYODBC to connect, but while trying to schedule jobs for glue, I cannot import PYODBC as it is not natively supported by AWS Glue, not sure how drivers will work in glue shell as well.

like image 280
EngineJanwaar Avatar asked Mar 05 '23 01:03

EngineJanwaar


2 Answers

From: Introducing Python Shell Jobs in AWS Glue announcement:

Python shell jobs in AWS Glue support scripts that are compatible with Python 2.7 and come pre-loaded with libraries such as the Boto3, NumPy, SciPy, pandas, and others.

The module list doesn't include pyodbc module, and it cannot be provided as custom .egg file because it depends on libodbc.so.2 and pyodbc.so libraries.

I think you have 2 options:

  1. Create a jdbc connection to your DB from Glue's console, and use Glue's internal methods to query it. This will require code changes of course.
  2. Use Lambda function instead. You'll need to pack pyodbc and the required libs along with your code in a zip file. Someone has already compiled those libs for AWS Lambda, see here.

Hope it helps

like image 122
ya2410 Avatar answered Apr 02 '23 08:04

ya2410


For AWS Glue use either Dataframe/DynamicFrame and specify the SQL Server JDBC driver. AWS Glue already contain JDBC Driver for SQL Server in its environment so you don't need to add any additional driver jar with glue job.

df1=spark.read.format("jdbc").option("driver", "com.microsoft.sqlserver.jdbc.SQLServerDriver").option("url", url_src).option("dbtable", dbtable_src).option("user", userID_src).option("password", password_src).load()

if you are using a SQL instead of table:

df1=spark.read.format("jdbc").option("driver", "com.microsoft.sqlserver.jdbc.SQLServerDriver").option("url", url_src).option("dbtable", ("your select statement here") A).option("user", userID_src).option("password", password_src).load()

As an alternate solution you can also use jtds driver for SQL server in your python script running in AWS Glue

like image 43
Abraham Avatar answered Apr 02 '23 09:04

Abraham