Created
December 10, 2018 18:13
-
-
Save oldarmyc/2a32ba2c1d924e48a7ca733b814bd31f to your computer and use it in GitHub Desktop.
Snowflake example connection
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
""" | |
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