-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathhandler.py
54 lines (42 loc) · 1.61 KB
/
handler.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
import snowflake.connector as sf
def lambda_handler(event, context):
snowflake_params = {
'user': 'test',
'password': 'test',
'account': 'test',
'warehouse': 'test',
'database': 'test',
'schema': 'TEST_SCHEMA',
'host': 'snowflake.localhost.localstack.cloud'
}
# Establish a connection
connection = sf.connect(**snowflake_params)
try:
# Create a cursor object
cursor = connection.cursor()
# Execute the query to create a table
cursor.execute("create or replace table ability(name string, skill string )")
# Rows to insert
rows_to_insert = [('John', 'SQL'), ('Alex', 'Java'), ('Pete', 'Snowflake')]
# Execute the insert query with executemany
cursor.executemany("insert into ability (name, skill) values (%s, %s)", rows_to_insert)
# Execute a query to select data from the table
cursor.execute("select name, skill from ability")
# Fetch the results
result = cursor.fetchall()
print("Total # of rows:", len(result))
print("Row-1 =>", result[0])
print("Row-2 =>", result[1])
# Execute a query to get the current timestamp
cursor.execute("SELECT CURRENT_TIMESTAMP()")
current_timestamp = cursor.fetchone()[0]
print("Current timestamp from Snowflake:", current_timestamp)
finally:
# Close the cursor
cursor.close()
# Close the connection
connection.close()
return {
'statusCode': 200,
'body': "Successfully connected to Snowflake and inserted rows!"
}