Save Snowpark DataFrame as text file in Snowflake Stage

388 views Asked by At

My requirement is to take tabular data in Snowflake and save it as a csv file to Snowflake External Stage, where data has header and footer.

Is this doable using Snowpark(Python)? Result should look something like below where col1, col2, col3 are columns in Snowflake table

        Australia Sales as at 2023-8-31. Version is 2021.01
        Output_Format, CSV
        NUMLINES,      6781
        ---------------------------------
        col1, col2, col3
        xxx,yyy,zzz
        .....
        xxx,yyy,zzz
        ---------------------------------
        ##END##
        ##COLOR####ENDCOLOR##
        ##FORMULA####ENDFORMULA##
1

There are 1 answers

4
Sergiu On

This is possible with Snowpark Python API DataFrameWriter.copy_into_location as documented here

Here is an example:

df = session.create_dataframe(
            [["John", "Berry"], ["Rick", "Berry"], ["Anthony", "Davis"]],
            schema=["FIRST_NAME", "LAST_NAME"],
        )
df.write.copy_into_location(
            ext_stage,
            partition_by=partition_by,
            file_format_type="csv",
            format_type_options={"COMPRESSION": "GZIP"},
            header=True,
            overwrite=False,
        )