Connecting to Snowflake from a Jupyter notebook involves the use of Snowflake's Python connector.
Below is a general step-by-step guide:
- Install the Snowflake connector. In your Jupyter notebook, install the connector using the following command:
!pip install snowflake-connector-python
2. Import the connector. At the beginning of your script, import the connector with the import statement:
import snowflake.connector
3. Establish a connection. Create a connection object with your Snowflake account credentials:
conn = snowflake.connector.connect(
user='<your_username>',
password='<your_password>',
account='<your_account_url>',
warehouse='<your_warehouse>',
database='<your_database>',
schema='<your_schema>'
)
4. Execute queries. With the connection established, you can execute reads and writes to your Snowflake instance:
cursor = conn.cursor()
try:
cursor.execute('<YOUR_SQL_QUERY>')
for row in cursor:
print(row)
finally:
cursor.close()
5. Close the connection. It's important to close the connection once your operations are complete:
conn.close()
Always ensure that your credentials are stored securely and not exposed in the Jupyter notebook to prevent unauthorized access to your Snowflake account.