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

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

Snowflake python sheet store file at stage

问题

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

  1. Traceback (most recent call last):
  2. Worksheet, line 17, in main
  3. File "pandas/util/_decorators.py", line 211, in wrapper
  4. return func(*args, **kwargs)
  5. File "pandas/util/_decorators.py", line 211, in wrapper
  6. return func(*args, **kwargs)
  7. File "pandas/core/generic.py", line 2374, in to_excel
  8. formatter.write(
  9. File "pandas/io/formats/excel.py", line 944, in write
  10. writer = ExcelWriter( # type: ignore[abstract]
  11. File "pandas/io/excel/_xlsxwriter.py", line 205, in __init__
  12. super().__init__(
  13. File "pandas/io/excel/_base.py", line 1313, in __init__
  14. self._handles = get_handle(
  15. File "pandas/io/common.py", line 734, in get_handle
  16. check_parent_directory(str(handle))
  17. File "pandas/io/common.py", line 597, in check_parent_directory
  18. raise OSError(rf"Cannot save file into a non-existent directory: '{parent}'")
  19. 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:

  1. import snowflake.snowpark as snowpark
  2. import xlsxwriter
  3. import pandas as pd
  4. from snowflake.snowpark.functions import col
  5. def main(session: snowpark.Session):
  6. # Your code goes here, inside the "main" handler.
  7. tableName = 'information_schema.packages'
  8. dataframe = session.table(tableName).filter(col("language") == 'python')
  9. x = dataframe.to_pandas()
  10. unload_location = "@JV_TEST/jv.xlsx"
  11. x.to_excel(unload_location)
  12. # Print a sample of the dataframe to standard output.
  13. dataframe.show()
  14. # Return value will appear in the Results tab.
  15. return dataframe

But I get the following error:

  1. Traceback (most recent call last):
  2. Worksheet, line 17, in main
  3. File "pandas/util/_decorators.py", line 211, in wrapper
  4. return func(*args, **kwargs)
  5. File "pandas/util/_decorators.py", line 211, in wrapper
  6. return func(*args, **kwargs)
  7. File "pandas/core/generic.py", line 2374, in to_excel
  8. formatter.write(
  9. File "pandas/io/formats/excel.py", line 944, in write
  10. writer = ExcelWriter( # type: ignore[abstract]
  11. File "pandas/io/excel/_xlsxwriter.py", line 205, in __init__
  12. super().__init__(
  13. File "pandas/io/excel/_base.py", line 1313, in __init__
  14. self._handles = get_handle(
  15. File "pandas/io/common.py", line 734, in get_handle
  16. check_parent_directory(str(handle))
  17. File "pandas/io/common.py", line 597, in check_parent_directory
  18. raise OSError(rf"Cannot save file into a non-existent directory: '{parent}'")
  19. 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:

确定