Categories
Connecting to Data Stores Guides and Code Samples

Connecting to Snowflake

Connecting to Snowflake

Here’s a quick example of how to connect to Snowflake using JetML workflows. 

Workflow Setup
  1. Add the snowflake-connector-python package to enable Python to connect to Snowflake. 
  2. Add your Snowflake credentials as environment variables to keep them separate than your code.
  3. Run the workflow.
  4. Copy over the example code below into a notebook on the running workflow. 
Example Notebook
import snowflake.connector
import logging
import os

logger = logging.getLogger(__name__)

def snowflake_connection():

    ACCOUNT = os.environ['SNOWFLAKE_ACCOUNT']
    USER = os.environ['SNOWFLAKE_USER']
    PASSWORD =  os.environ['SNOWFLAKE_PASSWORD']
    WAREHOUSE = os.environ['SNOWFLAKE_WAREHOUSE']
    ROLE = os.environ['SNOWFLAKE_ROLE']

    conn = snowflake.connector.connect(
      user=USER,
      password=PASSWORD,
      account=ACCOUNT,
      warehouse=WAREHOUSE,
      role=ROLE
    )

    cur = conn.cursor()

    return cur,conn


def close_connection(cur,conn):

    cur.close()
    conn.close()

    return
cur,conn = snowflake.snowflake_connection()

cur.execute("""use warehouse EXAMPLEWAREHOUSE""")

query = """
          select * from "exampletable";
"""

cur.execute(query)