【问题标题】:is there a way to convert a python script into one class or a package?有没有办法将 python 脚本转换为一个类或一个包?
【发布时间】:2020-12-15 17:07:55
【问题描述】:

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

以下是连接源 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 文件都可以用作模块。
  • 当您尝试将您的代码分解为函数或类时是否出现了特殊问题?
  • @mkrieger1 我基本上是在尝试查看打包此代码的选项,以便可以多次调用它(希望并行运行 5 个表)

标签: python-3.x class package snowflake-cloud-data-platform


【解决方案1】:

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

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

【讨论】:

    猜你喜欢
    • 2020-01-08
    • 2013-01-10
    • 1970-01-01
    • 2022-10-23
    • 1970-01-01
    • 2014-04-17
    • 1970-01-01
    • 2021-05-15
    • 1970-01-01
    相关资源
    最近更新 更多