Snowflake Procedure Integration With Python/Fastapi
Snowflake Procedure Integration With Python/Fastapi
Python/FastAPI
www.bispsolutions.com www.alacontec.com
Table of Contents
Snowflake Procedure integration with python/fastApi .....................................................................................................3
Business Requirement......................................................................................................................................................4
Solutions: .........................................................................................................................................................................4
Steps to use python snowflake for Integration ................................................................... Error! Bookmark not defined.
Code to fetch data fromSnowflake cloud . ........................................................................... Error! Bookmark not defined.
Testing: ............................................................................................................................................................................6
Steps to test Integartion...................................................................................................................................................6
Final Result. ........................................................................................................................ Error! Bookmark not defined.
www.bispsolutions.com www.alacontec.com
Snowflake Procedure Integraion with Python/fastApi
Python is an interpreted, object-oriented, high-level programming language with dynamic semantics.
Mostly used in Data-science and machine-learning.
FastAPI is a modern, fast (high-performance), web framework for building APIs with Python 3.6+ based
on standard Python type hints.
OpenAPI for API creation, including declarations of path operations, parameters, body requests,
security, etc.
Automatic data model documentation with JSON Schema (as OpenAPI itself is based on JSON
Schema).
Designed around these standards, after a meticulous study. Instead of an afterthought layer on top.
This also allows using automatic client code generation in many languages.
Stored procedures allow you to extend Snowflake SQL by combining it with JavaScript so that
you can include standard programming constructs such as branching and looping. Stored
procedures also make code easier to maintain and re-use.
Snowflake stored procedures are implemented through JavaScript and, optionally, SQL:
www.bispsolutions.com www.alacontec.com
Business Requirement
The main objective of this project is that store data in a place where there is no chance to lose. That’s
why need to store data in a cloud or with the help of that we can reuse modules.
Solutions:
Note:In this document we explain step by step Integration between Python/FastAPI and Snowflake (To
Fetch data from stored procedure) using python connector.
Steps :
1) Login with your snowflake account
2) Now create a table named Persons with parameters as PersonID, LastName, FirstName, Address,
City
>>>create table Persons(PersonID in ,LastName varchar(20) ,FirstName varchar(20),Address
varchar(30),City varchar(15));
3) Insert some records in the table by entering the following command in your worksheet.
>>>INSERT INTO Persons (PersonID, LastName, FirstName, Address, City)
VALUES ('1', 'sons', ‘john', 'Lig', 'London');
4) To check whether we are doing right or not just enter the command in you worksheet
>>> select * from persons;
5) If you are getting the following screen then you are in the right way.
www.bispsolutions.com www.alacontec.com
6) Create a procedure by entering the following command.
>>>create or replace procedure read_person_proc()
returns String not null
language javascript
as
$$
var my_sql_command = "select * from Persons";
var statement1 = snowflake.createStatement( {sqlText: my_sql_command} );
var result_set1 = statement1.execute();
// Loop through the results, processing one row at a time...
while (result_set1.next()) {
var column1 = result_set1.getColumnValue(1);
var column2 = result_set1.getColumnValue(2);
var column3 = result_set1.getColumnValue(3);
var column4 = result_set1.getColumnValue(4);
var column5 = result_set1.getColumnValue(5);
var column = column1+' '+column2+' '+column3+' '+column4+' '+column5
}
return column;
$$
;
7) To call the procedure just enter the command
>>>call read_person_proc();
Now our requirement is we need to call that procedure in python using FastAPI
Below code will help you to fetch/call procedure form Snowflake into FastAPI.
import snowflake.connectoras sf
username='your username'
password='your password'
account='your account'
warehouse='your ware house'
database='your data base name'
ctx=sf.connect(user=username,password=password,account=account)
@app.get('/fetchdata')
asyncdeffetchdata():
cursor = ctx.cursor()
www.bispsolutions.com www.alacontec.com
cursor.execute("use warehouse your warehouse_name")
return data
You need to follow each step otherwise you may face some issues
Testing:
Steps to test Integartion
Step1: Start fastApi Server by entering the following command
>>>uvicornmain:app–reload
>>>http://127.0.0.1:8000/fetchdata
If you get something like this. Congratulations you are successfully connected.
www.bispsolutions.com www.alacontec.com