Export Snowflake Table using Python – Example

  • Post author:
  • Post last modified:September 30, 2022
  • Post category:Snowflake
  • Reading time:4 mins read

Snowflake cloud data warehouse provides support for many connectors. For example, Python connector, Spark connector, etc. In my previous articles, we have seen how to use Python connectors, JDBC and ODBC drivers to connect to Snowflake. In this article, we will check how to export Snowflake table using Python with an example.

Export Snowflake Table using Python

As mentioned, there are many methods such as Python connector, JDBC or ODBC drivers get data from Snowflake tables.

Related Articles,

Use Snowflake Python Connector to Export Table

The Python connector is a pure python package that can be used to connect your application to the cloud data warehouse. The connector supports all standard operations. For example, query execution, loading, accessing data from external source (S3), and many more.

Using Pandas with Snowflake Python Connector

Pandas is a library for data analysis. A Python program can retrieve data from Snowflake, store it in a DataFrame, and use the Pandas library to analyze and manipulate the data in the DataFrame.

You need to install pandas compatible Snowflake python connector:

snowflake-connector-python[pandas]

Snowflake Python connector supports two Python Pandas methods, such as,

  • fetch_pandas_all() :
    • This method fetches all the rows in a cursor and loads them into a Pandas DataFrame
  • fetch_pandas_batch()
    • This method fetches a subset of the rows in a cursor and delivers them to a Pandas DataFrame.

You can use these methods to create dataFrame.

Once the data is copied to Pandas dataFame, you can use the Pandas to_csv feature to export the content.

Following example provides the code to export your Snowflake table using Python connector and Pandas library.

import snowflake.connector

# Connectio string
conn = snowflake.connector.connect(
                user='snuser',
                password='password@123',
                account='xyz12345.us-east-2',
                #warehouse='COMPUTE_WH',
                database='DEMO_DB',
                schema='public'
                )

# Create cursor
cur = conn.cursor()

# Execute SQL statement
cur.execute("select * from S_STUDENTS;")

pd_df = cur.fetch_pandas_all()

pyarrow package is missing

There may be a situation where you may encounter the package missing error.

For example,

“snowflake.connector.errors.ProgrammingError: 255002: pyarrow package is missing. Install using pip if the platform is supported.”

Install pyarow package using conda or pip installer.

Related Articles,

Hope this helps 🙂