Skip to content

Instantly share code, notes, and snippets.

@oldarmyc
Created December 10, 2018 18:13
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save oldarmyc/2a32ba2c1d924e48a7ca733b814bd31f to your computer and use it in GitHub Desktop.
Save oldarmyc/2a32ba2c1d924e48a7ca733b814bd31f to your computer and use it in GitHub Desktop.
Snowflake example connection
"""
Installing the connector it is recommended to use pip and not the conda-forge package
pip install snowflake-connector-python
"""
import snowflake.connector
import json
# Get credentials from kubernetes. The credentials were setup as a dictionary so will use json to load
credentials = None
with open('/var/run/secrets/user_credentials/snowflake_credentials') as f:
credentials = json.load(f)
# Check and make sure the credentials were pulled correctly
if credentials:
# Connect to snowflake
ctx = snowflake.connector.connect(
user=credentials.get('username'),
password=credentials.get('password'),
account=credentials.get('account')
)
# Establish a cursor and execute a query
cs = ctx.cursor()
ret = []
try:
test = cs.execute("USE database SNOWFLAKE_SAMPLE_DATA")
cs.execute("SHOW TABLES")
ret = cs.fetchmany(30)
finally:
# Always close connections if there is an error
cs.close()
# Close the connection
ctx.close()
# Print out the return values for the data
for data in ret:
print(data)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment