Integration between Python/FastAPI and Snowflake

pip install fastapipip install uvicorn
create or replace stage first_stage
file_format = (type = 'CSV' field_delimiter = ',' skip_header = 1);
create table Customers(CustomerID int, LastName varchar(20) ,FirstName varchar(20),Products varchar(30),Category varchar(15));
INSERT INTO Customers(CustomerID, LastName, FirstName, Products, Category)VALUES ('2', 'Maheshwarappa', 'Abhishek', 'OnePlus', 'Mobile');
select * from Customers;
create or replace procedure read_customer_proc() 
returns String not null
language javascript
as
$$
var my_sql_command = "select * from Customers";var statement1 = snowflake.createStatement( {sqlText: my_sql_command} ); var result_set1 = statement1.execute();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;
$$
;
call read_person_proc();
from fastapi import FastAPIimport snowflake.connector as sfusername='<username>'
password='<password>'
account='<account>'
warehouse='<warehouse>'
database='<database>'
ctx=sf.connect(user=username,password=password,account=account,warehouse=warehouse,database=database)
app = FastAPI()@app.get('/fetchdata')async def fetchdata():
cursor = ctx.cursor()
cursor.execute("USE WAREHOUSE COMPUTE_WH ")
cursor.execute("USE DATABASE FIRST")
cursor.execute("USE schema PUBLIC")
sql = cursor.execute("call read_customer_proc()")
for data in sql:
return data
uvicorn main:app --reload
http://127.0.0.1:8000/fetchdata
  1. https://www.snowflake.com/
  2. https://fastapi.tiangolo.com/
  3. https://www.python.org/
  4. https://www.bisptrainings.com/

--

--

Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store