Snowflake Python 将文件存储在阶段(stage)。

huangapple go评论147阅读模式
英文:

Snowflake python sheet store file at stage

问题

我想将 xlsx 文件存储在 Snowflake 内部阶段,但出现以下错误:

Traceback (most recent call last):
  Worksheet, line 17, in main
  File "pandas/util/_decorators.py", line 211, in wrapper
    return func(*args, **kwargs)
  File "pandas/util/_decorators.py", line 211, in wrapper
    return func(*args, **kwargs)
  File "pandas/core/generic.py", line 2374, in to_excel
    formatter.write(
  File "pandas/io/formats/excel.py", line 944, in write
    writer = ExcelWriter(  # type: ignore[abstract]
  File "pandas/io/excel/_xlsxwriter.py", line 205, in __init__
    super().__init__(
  File "pandas/io/excel/_base.py", line 1313, in __init__
    self._handles = get_handle(
  File "pandas/io/common.py", line 734, in get_handle
    check_parent_directory(str(handle))
  File "pandas/io/common.py", line 597, in check_parent_directory
    raise OSError(rf"Cannot save file into a non-existent directory: '{parent}'")
OSError: Cannot save file into a non-existent directory: '@JV_TEST'

如何将文件存储到阶段?(我想使用 pandas 和 xlsxwriter,因为我想对 Excel 表格应用一些操作)

翻译完毕,不包括代码部分。

英文:

I have the following small test, where I would like to store a xlsx file at internal stage in snowflake:

import snowflake.snowpark as snowpark
import xlsxwriter
import pandas as pd
from snowflake.snowpark.functions import col

def main(session: snowpark.Session): 
# Your code goes here, inside the "main" handler.
tableName = 'information_schema.packages'
dataframe = session.table(tableName).filter(col("language") == 'python')

x = dataframe.to_pandas()
unload_location = "@JV_TEST/jv.xlsx"

x.to_excel(unload_location)
# Print a sample of the dataframe to standard output.
dataframe.show()

# Return value will appear in the Results tab.
return dataframe

But I get the following error:

Traceback (most recent call last):
  Worksheet, line 17, in main
  File "pandas/util/_decorators.py", line 211, in wrapper
    return func(*args, **kwargs)
  File "pandas/util/_decorators.py", line 211, in wrapper
    return func(*args, **kwargs)
  File "pandas/core/generic.py", line 2374, in to_excel
    formatter.write(
  File "pandas/io/formats/excel.py", line 944, in write
    writer = ExcelWriter(  # type: ignore[abstract]
  File "pandas/io/excel/_xlsxwriter.py", line 205, in __init__
    super().__init__(
  File "pandas/io/excel/_base.py", line 1313, in __init__
    self._handles = get_handle(
  File "pandas/io/common.py", line 734, in get_handle
    check_parent_directory(str(handle))
  File "pandas/io/common.py", line 597, in check_parent_directory
    raise OSError(rf"Cannot save file into a non-existent directory: '{parent}'")
OSError: Cannot save file into a non-existent directory: '@JV_TEST'

How can I store the file at stage? (I would like to use pandas and xlsxwrite, because I would like to apply some stuff to the excel sheet)

答案1

得分: 2

目前不可能使用Python工作表写入阶段。
Python存储过程和UDFs的一般限制也适用于Python工作表。

这意味着您只能将文件写入/tmp,一旦会话终止,文件将无法再从/tmp访问。
您只能使用Python的内置函数如open()来写入/tmp,而不能使用外部函数如xlsxwriter

总的来说,使用Python操作存储在阶段上的文件是不可能的。

英文:

At this time it is not possible to write to a stage using Python Worksheets.
The general limitations from Python Stored Procedures and UDFs apply also to Python Worksheets.

This means you can only write files to /tmp and once your session is terminated the files won't be accessible anymore from /tmp.
You can only write to /tmp using Python's built in functions like open(), it doesn't work with external ones like xlsxwriter.

As a general rule, manipulations of files stored on a stage using Python is not possible.

huangapple
  • 本文由 发表于 2023年6月15日 20:17:45
  • 转载请务必保留本文链接:https://go.coder-hub.com/76482383.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定