Algorithms can easily access databases hosted by Snowflake Computing using the Snowflake Connector for Python .
Begin by creating a collection named “SnowflakeCredentials”, and uploading a file “credentials.json” with the following structure (see their docs to find your Account Name):
{
"user": "<YOUR_SNOWFLAKE_USERNAME>",
"password": "<YOUR_SNOWFLAKE_PASSWORD>",
"account": "<YOUR_SNOWFLAKE_ACCOUNT>"
}
Next, create a Python algorithm. Click “Dependencies” in the Web IDE (or edit your requirements.txt file) and add the dependency snowflake-connector-python
.
Now paste the following code in as your algorithm:
import Algorithmia
import snowflake.connector
# Get Snowflake creds
client = Algorithmia.client()
creds = client.file("data://.my/SnowflakeCredentials/credentials.json").getJson()
def apply(input):
ctx = snowflake.connector.connect(
user=creds.get("user"),
password=creds.get("password"),
account=creds.get("account")
)
cs = ctx.cursor()
try:
cs.execute("USE SNOWFLAKE_SAMPLE_DATA")
cs.execute("SELECT * FROM TPCDS_SF100TCL.CALL_CENTER")
one_row = cs.fetchone()
return [str(i) for i in one_row]
finally:
cs.close()
ctx.close()
Build and test this algorithm. Assuming you have not deleted the SNOWFLAKE_SAMPLE_DATA database, it should return the first row. Otherwise, alter it to try pulling data from your own database.