In the current data-centric environment, leveraging advanced analytics and ensuring smooth data migration is critical. Among the plethora of available tools, Python emerges as the preferred language for tasks related to data warehousing, analytics, and migration.
Python's versatility and extensive libraries make it a go-to choice for handling complex data workflows. Whether you’re managing a legacy data warehouse or implementing a modern cloud-based solution, Python’s flexibility and robust ecosystem empower data professionals to tackle diverse challenges effectively.
In this comprehensive guide, we'll delve into the effortless process of connecting to Snowflake, a leading cloud-based data warehousing solution, using Python.
The Architecture
Before we embark on our journey, let's familiarize ourselves with the architecture we'll be working with. At the core lies the Snowflake Data Warehouse, while our tool of choice will be a Python Anaconda notebook.
It's worth noting that while our demonstration notebook is hosted locally, you have the flexibility to utilize platforms like Databricks Notebooks, AWS Lambda, or Azure Functions to connect to Snowflake.
Getting Started: Installing Dependency
The official Python-Snowflake connector should be installed using the following command.
pip install snowflake-connector
Importing Necessary Modules
import snowflake.connector
import os
The os module is imported because we will be saving the password as an environment variable.
Populating Connection Paramters
Declare password as an environment variable.
os.environ["SNOWFLAKE_PASSWORD"] ='DummyPassword'
# User Credentials
user = 'cdaniel7'
password = os.environ.get('SNOWFLAKE_PASSWORD')
#Account Info
account = 'uz26261.eu-central-1'
# Data Warehouse Info
warehouse = 'COMPUTE_WH'
database = 'LANG_Chain'
schema = 'OPEN_AI_DEMO'
Creating a connection.
# Connect to Snowflake
conn = snowflake.connector.connect(
user=user,
password=password,
account=account,
warehouse=warehouse,
database=database,
schema=schema
)
# Create a cursor object
cur = conn.cursor()
Execute the Query
cur.execute("SELECT category_name, sum(order_value) as sum FROM orders_category Group by category_name ;")
# Fetch results
rows = cur.fetchall()
for row in rows:
print(row)
When you execute the query you should get an output. I am pasting a sample output below.
('Toys and Games', 5151.0)
('Health and Beauty', 2223.0)
('Food and Groceries', 5048.0)
('Sports and Outdoor Gear', 7125.0)
('Entertainment', 3089.0)
Closing the connection
# Close the cursor and connection
cur.close()
conn.close()
Within a few minutes, you should be able to connect to your Snowflake Data Warehouse.
If you need video assistance please watch this video
About Us:
We are a Data & Analytics Consulting Firm.
Analyze Agency provides an extensive array of services in data analytics and consulting like the following.
Data Modelling, Data Integration and Data Engineering
BI and Analytics Implementation
Data Integration, Modelling and Transformation (ETL/ELT)
Data Warehouse, Data Lake, Lakehouse, Data Fabric and Data Mesh
For questions reach me at Chris@Analyze.Agency
Comments