I'm retrieving data from an API and converting the data into a pandas dataframe. I'm using python-snowflake connector to send this data into my snowflake schema as a table.
I want to use merge instead of sending the duplicate data into my snowflake table.
Sample data I'm retrieving from API:
|------------|-------------|------------|
| log_in_ID | user_id | date |
|------------|-------------|------------|
| 1 | 21 | 02/21/2021 |
| 2 | 22 | 02/24/2021 |
| 3 | 23 | 02/27/2021 |
| 4 | 21 | 02/29/2021 |
|------------|-------------|------------|
The log_in_ID is unique
Here is my code:
import requests
import json
import snowflake.connector
import pandas as pd
from sqlalchemy import create_engine
engine = create_engine(URL(
account='my_snowflake_account',
user='user',
password='password',
database='my_database'
schema='my_schema',
warehouse='warehouse',
role='ADMIN'))
pandas_df = 'Some code to get API data and convert into pandas dataframe'
def send_to_snowflake(pandas_df, target_table):
connect = engine.connect()
data.tosql(target_table, con=engine, index=False, if_exists='append')
connection.close()
engine.dispose()
if __name__ == "__main__":
send_to_snowflake(pandas_df, target_table)
How can I use merge statement with log_in_id as unique key?
How can I use a pandas dataframe inside the merge query in snowflake-python?
merge into target_table using {pandas_dataframe}
on target_table.log_in_id = {pandas_dataframe}.log_in_id
when matched then
update set target_table.user_id = {pandas_dataframe}.user_id and
set target_table.date = {pandas_dataframe}.date