AWS

AWS Glue에서 pymssql 을 이용해 MSSQL 데이터를 S3에 저장하기

whistory 2022. 12. 26. 09:46
반응형

 

AWS Glue를 이용해,

MSSQL 의 데이터를 S3에 저장하는 기능을 구현하려한다.

 

Glue 에서 [Python Shell script editor] 를 생성한다.

python shell script로 생성하면. gule 1.0으로 생성된다.

 

https://docs.aws.amazon.com/glue/latest/dg/release-notes.html

 

AWS Glue versions - AWS Glue

AWS Glue versions You can configure the AWS Glue version parameter when you add or update a job. The AWS Glue version determines the versions of Apache Spark and Python that AWS Glue supports. The Python version indicates the version that's supported for j

docs.aws.amazon.com

 

 

 

 

import pymssql  
import pandas as pd
from datetime import datetime

### mssql connection
mssql_server = 'server'
mssql_user = 'user'
mssql_password = 'password'
mssql_database = 'databse'

## table
source_table = 'table'

## bucket
bucket_name = "bucketname"
prefix = "mssqlsample"

thisTime = datetime.today().strftime('%Y%m%d%H%M%S')
tempFileName = f'{source_table}_{thisTime}.parquet'
    
conn = pymssql.connect(server=mssql_server
                        , user=mssql_user
                        , password=mssql_password
                        , database=mssql_database
                        , charset='EUC-KR')

cursor = conn.cursor()  

cursor.execute(f'select top 10 * from dbo.{source_table}')
row = cursor.fetchall()

Dataname = ['col1','col2','col3','col4']
df = pd.DataFrame(row, columns=Dataname)
df.to_parquet
print(df)

conn.close()

result = f"s3://{bucket_name}/{prefix}/{tempFileName}"

df.to_parquet(result
            , engine='pyarrow'
            , index=False)

print(f"complete ==> {result}")
ModuleNotFoundError: No module named 'pymssql'

파이썬 라이브러리인 pymssql 이 없어서 발생한 에러.

Glue 3.0 은 python 3.9

Glue 1.0 은 python 3.6

이므로, 3.6버전의 라이브러리를 다운 받아야 한다.

pymssql, s3fs, pyarrow를 다운받아야한다.

다운받은 whl 파일을 s3에 올린다.

 

 

 

[Job details] 의 Python library path 에 s3에 올린 파일들 경로를 적어준다.

 

 

실행하면 성공.

반응형