Created
November 28, 2019 21:57
-
-
Save qiuyujx/eba3bf1aabfdcff8b96298e37867482d to your computer and use it in GitHub Desktop.
Azure Function: HTTPTrigger Function with Snowflake Connector
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
import logging | |
import azure.functions as func | |
import snowflake.connector | |
def get_connection(): | |
return snowflake.connector.connect( | |
user='<your_user>', | |
password='<your_password>', | |
account='<your_account>', | |
warehouse='<your_virtual_warehouse>' | |
) | |
def main(req: func.HttpRequest) -> func.HttpResponse: | |
logging.info('Python HTTP trigger function processed a request.') | |
ctx = get_connection() | |
result_string = "" | |
schema = req.params.get('schema') | |
table = req.params.get('table') | |
if schema and table and ctx : | |
cursor = ctx.cursor() | |
try: | |
results = cursor.execute("SELECT * FROM SNOWFLAKE_SAMPLE_DATA.{}.{}".format(schema, table)) | |
for row in results: | |
result_string = result_string + str(row[0]) + " " + str(row[1]) + "\n" | |
result_string += "\n\nQuery ID: " + cursor.sfqid | |
except Exception as e: | |
print(e) | |
finally: | |
cursor.close() | |
ctx.close() | |
return func.HttpResponse(result_string) | |
else: | |
return func.HttpResponse( | |
"Request failed.", | |
status_code=400 | |
) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment