admin管理员组文章数量:1397208
I have a requirement to export rows from Snowflake table into a file in an internal Snowflake stage. If there are zero rows, I still want the file to be exported with just columns and no data. My code is Python/Snowpark so I probably can write a workaround but was hoping there is solution for this problem in Snowflake. I could not find anything here:
code I am using:
df.write.copy_into_location(stage_name + file_name,
file_format_type = 'csv',
header = True,
overwrite = True ,
single = True,
format_type_options={"COMPRESSION": "NONE", "NULL_IF": (),"RECORD_DELIMITER":"\r\n", "FIELD_OPTIONALLY_ENCLOSED_BY":'"'},
)
I have a requirement to export rows from Snowflake table into a file in an internal Snowflake stage. If there are zero rows, I still want the file to be exported with just columns and no data. My code is Python/Snowpark so I probably can write a workaround but was hoping there is solution for this problem in Snowflake. I could not find anything here: https://docs.snowflake/en/sql-reference/sql/copy-into-location#label-copy-into-location-copyoptions
code I am using:
df.write.copy_into_location(stage_name + file_name,
file_format_type = 'csv',
header = True,
overwrite = True ,
single = True,
format_type_options={"COMPRESSION": "NONE", "NULL_IF": (),"RECORD_DELIMITER":"\r\n", "FIELD_OPTIONALLY_ENCLOSED_BY":'"'},
)
Share
Improve this question
asked Mar 27 at 6:04
Humble HeroHumble Hero
1471 silver badge16 bronze badges
1
|
1 Answer
Reset to default 0As Lukasz Szozda already mentioned, Snowflake's COPY command does not unload data if the souce has 0 rows, and df.write.copy_into_location method uses COPY command to unload the data.
To overcome this, you may do a trick and write the column names as data:
if ( df.count() == 0):
header_values = []
for field in df.schema.fields:
header_values.append( field.name )
df = session.createDataFrame( [header_values] )
df.write.copy_into_location(stage_name + file_name,
file_format_type = 'csv',
header = False,
overwrite = True ,
single = True,
format_type_options={"COMPRESSION": "NONE", "NULL_IF": (),"RECORD_DELIMITER":"\r\n", "FIELD_OPTIONALLY_ENCLOSED_BY":'"'},
)
else:
df.write.copy_into_location(stage_name + file_name,
file_format_type = 'csv',
header = True,
overwrite = True ,
single = True,
format_type_options={"COMPRESSION": "NONE", "NULL_IF": (),"RECORD_DELIMITER":"\r\n", "FIELD_OPTIONALLY_ENCLOSED_BY":'"'},
)
本文标签: Offloading empty Snowflake table into stageStack Overflow
版权声明:本文标题:Offloading empty Snowflake table into stage - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1744109504a2591223.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
"If the source table contains 0 rows, then the COPY operation does not unload a data file."
– Lukasz Szozda Commented Mar 27 at 8:14