【发布时间】:2022-06-29 20:55:44
【问题描述】:
一段时间以来,我一直在使用 Python 向 Snowflake 读取数据并将数据写入到我拥有完全更新权限的表,使用的是我的同事在 Internet 上找到的 Snowflake 帮助器类。请参阅下面的课程,了解我一直在使用的课程,其中抽象出我的个人 Snowflake 连接信息和一个简单阅读的查询,如果您的架构中有一个“TEST”表,则该查询有效。
from snowflake.sqlalchemy import URL
from sqlalchemy import create_engine
import keyring
import pandas as pd
from sqlalchemy import text
# Pull the username and password to be used to connect to snowflake
stored_username = keyring.get_password('my_username', 'username')
stored_password = keyring.get_password('my_password', 'password')
class SNOWDBHelper:
def __init__(self):
self.user = stored_username
self.password = stored_password
self.account = 'account'
self.authenticator = 'authenticator'
self.role = stored_username + '_DEV_ROLE'
self.warehouse = 'warehouse'
self.database = 'database'
self.schema = 'schema'
def __connect__(self):
self.url = URL(
user=stored_username,
password=stored_password,
account='account',
authenticator='authenticator',
role=stored_username + '_DEV_ROLE',
warehouse='warehouse',
database='database',
schema='schema'
)
# =============================================================================
self.url = URL(
user=self.user,
password=self.password,
account=self.account,
authenticator=self.authenticator,
role=self.role,
warehouse=self.warehouse,
database=self.database,
schema=self.schema
)
self.engine = create_engine(self.url)
self.connection = self.engine.connect()
def __disconnect__(self):
self.connection.close()
def read(self, sql):
self.__connect__()
result = pd.read_sql_query(sql, self.engine)
self.__disconnect__()
return result
def write(self, wdf, tablename):
self.__connect__()
wdf.to_sql(tablename.lower(), con=self.engine, if_exists='append', index=False)
self.__disconnect__()
# Initiate the SnowDBHelper()
SNOWDB = SNOWDBHelper()
query = """SELECT * FROM """ + 'TEST'
snow_table = SNOWDB.read(query)
我现在需要更新现有的 Snowflake 表,我的同事建议我可以使用 read 函数将包含更新 SQL 的查询发送到我的 Snowflake 表。因此,我调整了我在 Snowflake UI 中成功使用的更新查询来更新表,并使用 read 函数将其发送到 Snowflake。 它实际上告诉我表中的相关行已更新,但它们没有更新。请参阅下面的更新查询我用来尝试将“测试”表中的字段“字段”更改为“X”和我收到的成功消息。总体而言,对这种 hacky 更新尝试方法并不感到兴奋(表更新是某种副作用??),但是有人可以帮忙在这个框架内更新方法吗?
# Query I actually store in file: '0-Query-Update-Effective-Dating.sql'
UPDATE "Database"."Schema"."Test" AS UP
SET UP.FIELD = 'X'
# Read the query in from file and utilize it
update_test = open('0-Query-Update-Effective-Dating.sql')
update_query = text(update_test.read())
SNOWDB.read(update_query)
# Returns message of updated rows, but no rows updated
number of rows updated number of multi-joined rows updated
0 316 0
【问题讨论】:
标签: python-3.x sqlalchemy snowflake-cloud-data-platform snowflake-connector