首页 > 解决方案 > 有没有办法将 python 脚本转换为一个类或一个包?

问题描述

我分三个阶段将数据从 IBM 迁移到 Snowflake - 提取、转换和加载。

下面是连接源 IBM 和执行 ETL 的目标 Snowflake 的 python 代码。有什么办法可以从下面的整个代码中创建一个类/包?

import snowflake.connector


tableName='F58001'

ctx = snowflake.connector.connect(
user='*',
password='*',
account='*.azure'
)

cs = ctx.cursor()

ctx.cursor().execute("USE DATABASE STORE_PROFILE")
ctx.cursor().execute("USE SCHEMA LANDING")


try:
ctx.cursor().execute("PUT file:///temp/data/{tableName}/* @%{tableName}".format(tableName=tableName))
except Exception:
pass

ctx.cursor().execute("truncate table {tableName}".format(tableName=tableName))
ctx.cursor().execute("COPY INTO {tableName} ON_ERROR = 'CONTINUE'  ".format(tableName=tableName, 
FIELD_OPTIONALLY_ENCLOSED_BY = '""', sometimes=',', ERROR_ON_COLUMN_COUNT_MISMATCH = 'TRUE'))

last_query_id= ctx.cursor().execute("select last_query_id()")

for res in last_query_id:
query_id = res[0]

ctx.cursor().execute(f"create or replace table save_copy_errors as select * from 
table(validate("+tableName+", job_id=> "+"'"+query_id+"'"+"))")

ax = ctx.cursor().execute("select * from save_copy_errors")

for errors in ax:
error = errors
print(error)

ctx.close()

标签: python-3.xclasspackagesnowflake-cloud-data-platform

解决方案


请查看以下存储库。它可能已经回答了你的问题。我目前正在努力将其移至 PYPI,以便可以使用 PIP 安装它

https://github.com/Infosys/Snowflake-Python-Development-Framework


推荐阅读