使用 python 将 snowpark 脚本实现为雪花中的作业

问题描述 投票:0回答:0

当我在雪花工作表中运行以下代码时
我在本地有数据,想从本地获取数据并使用 python 的 snowpark 模块将其存储在内部阶段

CREATE OR REPLACE FUNCTION create_my_function()
RETURNS STRING
LANGUAGE PYTHON
RUNTIME_VERSION=3.8
handler='my_function'
PACKAGES = ('snowflake-snowpark-python')
AS
$$
import snowflake.connector
import os

def my_function():
    # Establish a connection to Snowflake
    conn = snowflake.connector.connect(
        user='######',
        password='########',
        account='##############',
        role='#########',
        warehouse='########',
        database='###########',
        schema='############'
    )
    # Create a cursor object
    cur = conn.cursor()

    # Get the file path
    file_path = r'C:\Users\softility\Downloads\EMP_Details.csv'

    # Upload the file to the Snowflake stage
    stage_name = 'my_stage'
    file_name = os.path.basename(r'C:\Users\softility\Downloads\EMP_Details.csv')
    #put_command = f"PUT 'file://{r'C:\Users\softility\Downloads\EMP_Details.csv'}' @{my_stage}/{EMP_Details.csv}"
    put_command = "PUT 'file://" + r'C:\Users\softility\Downloads\EMP_Details.csv' + f"' @{stage_name}/{file_name}"

    cur.execute(put_command)

    # Execute a SQL statement using the cursor to call the function
    sql_query = f"SELECT my_function(@{my_stage}/{EMP_Details.csv})"
    cur.execute(sql_query)

    # Fetch the result
    result = cur.fetchone()[0]

    # Close the cursor and connection
    cur.close()
    conn.close()

    # Return the result as a string
    return str(result)

$$

以上代码执行成功

但是当我通过在 SQL 查询中调用它来执行该函数时出现以下错误

Python Interpreter Error: Traceback (most recent call last): File "_udf_code.py", line 7, in my_function File "/usr/lib/python_udf/6b1024ccb8a3be4c1b5532ae6df89d7c9b5abd482263003274ee0e150684fc3c/lib/python3.8/site-packages/snowflake/connector/__init__.py", line 50, in Connect return StoredProcConnection(**kwargs) File "/usr/lib/python_udf/6b1024ccb8a3be4c1b5532ae6df89d7c9b5abd482263003274ee0e150684fc3c/lib/python3.8/site-packages/snowflake/connector/connection.py", line 206, in __init__ ] = self._prefetch_session_parameters() File "/usr/lib/python_udf/6b1024ccb8a3be4c1b5532ae6df89d7c9b5abd482263003274ee0e150684fc3c/lib/python3.8/site-packages/snowflake/connector/connection.py", line 214, in _prefetch_session_parameters ret = self.cmd_query( File "/usr/lib/python_udf/6b1024ccb8a3be4c1b5532ae6df89d7c9b5abd482263003274ee0e150684fc3c/lib/python3.8/site-packages/snowflake/connector/connection.py", line 571, in cmd_query ret = json.loads(_snowflake.execute_sql(sql, describe_only, stmt_params)) AttributeError: module '_snowflake' has no attribute 'execute_sql' in function CREATE_MY_FUNCTION with handler my_function
snowflake-cloud-data-platform
© www.soinside.com 2019 - 2024. All rights reserved.