Data Science & AI Workbench 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:
Copy
Ask AI
pip install snowflake-connector-python
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 Project configurations.
After you’ve installed the connector, you can then use code such as this to access Snowflake from within a notebook session:
Copy
Ask AI
import snowflake.connectorimport json# Get credentials from Kubernetes. The credentials were setup as a dictionary, so will use JSON to load.credentials = Nonewith open('/var/run/secrets/user_credentials/snowflake_credentials') as f: credentials = json.load(f)# Check and make sure the credentials were pulled correctlyif 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 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.
Was this page helpful?
Assistant
Responses are generated using AI and may contain mistakes.