Connecting to Snowflake#

Anaconda Enterprise enables you to connect to the Snowflake SQL cloud data warehouse, and work with data stored in it while in a notebook session.

To access Snowflake within the platform, Anaconda recommends you pip install the required connector first:

pip install snowflake-connector-python

NOTE: Any packages you install from the command line are available during the current session only. If you want them to persist, add them to the project’s anaconda-project.yml file. For more information, see Developing a project.

After you’ve installed the connector, you can then use code such as this to access Snowflake from within a notebook session:

import snowflake.connector
import json

# Get credentials from Kubernetes. The credentials were setup as a dictionary, so will use JSON to load.
credentials = None
with open('/var/run/secrets/user_credentials/snowflake_credentials') as f:
    credentials = json.load(f)

# Check and make sure the credentials were pulled correctly
if credentials:
    # Connect to snowflake
    ctx = snowflake.connector.connect(
        user=credentials.get('username'),
        password=credentials.get('password'),
        account=credentials.get('account')
    )

    # Establish a cursor and execute a query
    cs = ctx.cursor()
    ret = []
    try:
        test = cs.execute("USE database SNOWFLAKE_SAMPLE_DATA")
        cs.execute("SHOW TABLES")
        ret = cs.fetchmany(30)
    finally:
        # Always close connections if there is an error
        cs.close()

    # Close the connection
    ctx.close()

    # Print out the return values for the data
    for data in ret:
        print(data)

See Storing secrets for information about adding credentials to the platform, to make them available in your projects. Any secrets you add will be available across all sessions and deployments associated with your user account.