use*_*372 7 snowflake-cloud-data-platform
我正在尝试使用 python pandas 连接器连接到雪花。
我在 Windows 上使用 anaconda 发行版,但卸载了现有的连接器和 pyarrow,并使用此页面上的说明重新安装:https ://docs.snowflake.com/en/user-guide/python-connector-pandas.html
我有以下版本
熊猫 1.0.4 py37h47e9c7a_0
点 20.1.1 py37_1
pyarrow 0.17.1 pypi_0 pypi
蟒蛇 3.7.7 h81c818b_4
雪花连接器-python 2.2.7 pypi_0 pypi
运行本文档的步骤 2 时:https://docs.snowflake.com/en/user-guide/python-connector-install.html,我得到:4.21.2
尝试使用时fetch_pandas_all()出现错误:NotSupportedError: Unknown error
我使用的代码如下:
import snowflake.connector
import pandas as pd
SNOWFLAKE_DATA_SOURCE = '<DB>.<Schema>.<VIEW>'
query = '''
select *
from table(%s)
LIMIT 10;
'''
def create_snowflake_connection():
conn = snowflake.connector.connect(
user='MYUSERNAME',
account='MYACCOUNT',
authenticator = 'externalbrowser',
warehouse='<WH>',
database='<DB>',
role='<ROLE>',
schema='<SCHEMA>'
)
return conn
con = create_snowflake_connection()
cur = con.cursor()
temp = cur.execute(query, (SNOWFLAKE_DATA_SOURCE)).fetch_pandas_all()
cur.close()
Run Code Online (Sandbox Code Playgroud)
我想知道我还需要安装/升级/检查什么才能开始fetch_pandas_all()工作?
编辑:在下面发布答案后,我意识到问题出在带有authenticator='externalbrowser'的SSO(单点登录)上。当使用独立帐户时,我可以获取。
小智 13
我找到了一种解决方法,可以通过依赖 fetchall() 而不是 fetch_all_pandas() 来避免 SSO 错误:
try:
cur.execute(sql)
all_rows = cur.fetchall()
num_fields = len(cur.description)
field_names = [i[0] for i in cur.description]
finally:
cur.close()
con.close()
df = pd.DataFrame(all_rows)
df.columns = field_names
Run Code Online (Sandbox Code Playgroud)
小智 6
原因是 Snowflake-connector-python 没有安装您需要与 pandas 一起玩的“pyarrow”。
您可以安装并导入 Pyarrow 或
做 :
pip install “snowflake-connector-python[pandas]”
并尝试一下。
小智 1
当你运行这段代码时会发生什么?
from snowflake import connector
import time
import logging
for logger_name in ['snowflake.connector', 'botocore', 'boto3']:
logger = logging.getLogger(logger_name)
logger.setLevel(logging.DEBUG)
ch = logging.FileHandler('test.log')
ch.setLevel(logging.DEBUG)
ch.setFormatter(logging.Formatter('%(asctime)s - %(threadName)s %(filename)s:%(lineno)d - %(funcName)s() - %(levelname)s - %(message)s'))
logger.addHandler(ch)
from snowflake.connector.cursor import CAN_USE_ARROW_RESULT
import pyarrow
import pandas as pd
print('CAN_USE_ARROW_RESULT', CAN_USE_ARROW_RESULT)Run Code Online (Sandbox Code Playgroud)
这将输出 CAN_USE_ARROW_RESULT 是否为 true,如果不为 true,则 pandas 将无法工作。当您进行 pip 安装时,您运行了哪一个?
pip安装snowflake-connector-python pip安装snowflake-connector-python[pandas]
另外,你在什么操作系统上运行?
| 归档时间: |
|
| 查看次数: |
23039 次 |
| 最近记录: |