Read Table
Tags: #snowflake #database #snippet
Author: Sanjay Sabu

Input

Import libraries

1
import pandas as pd
2
from snowflakeconnector import SnowflakeConnector
3
import snowflake.connector as snow
Copied!

Insert your credentials

1
username = "sanjaynaas"
2
password = "Password123"
Copied!

Specify your account details

1
account = "iz84541.europe-west4.gcp"
2
database = "DEMO_DB"
3
table_name = "NAAS"
4
warehouse_name = "COMPUTE_WH"
5
schema_name = "PUBLIC"
Copied!

Model

Connecting to Snowflake

1
conn = snow.connect(user=username,password=password,account=account)
2
cur = conn.cursor()
3
instance = SnowflakeConnector(username,password,account,database)
Copied!

Setting the environment

1
admin = "USE ROLE SYSADMIN"
2
cur.execute(admin)
3
warehouse_selection = "USE WAREHOUSE " + warehouse_name
4
cur.execute(warehouse_selection)
5
database_selection = "USE DATABASE " + database
6
cur.execute(database_selection)
7
schema_selection = "USE SCHEMA " + schema_name
8
cur.execute(schema_selection)
Copied!

To see contents of table

1
table_display_query = "select * from " + table_name
2
#Fetch records from Snowflake database
3
instance.execute_query(table_display_query,query_type="pull")
Copied!

Reading data from the table

1
data = instance.execute_query(table_display_query,query_type="pull")
Copied!

Output

Fetching table details

1
table_description_query = "DESCRIBE " + table_name
2
table_details = instance.execute_query(table_description_query,query_type="pull")
3
header = table_details[0]
Copied!

Preparing csv file

1
data = pd.DataFrame(data)
2
data.columns=[header]
3
data.to_csv('naas_output.csv')
4
data
Copied!

Import library

Display result

Copy link
Edit on GitHub