-
Notifications
You must be signed in to change notification settings - Fork 3
Expand file tree
/
Copy pathload_data.py
More file actions
39 lines (31 loc) · 1.07 KB
/
load_data.py
File metadata and controls
39 lines (31 loc) · 1.07 KB
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
from sqlalchemy import create_engine, text
from urllib.parse import quote_plus
import os
from pathlib import Path
import pandas as pd
from dotenv import load_dotenv
import logging
logging.basicConfig(level=logging.INFO, format='%(asctime)s - %(levelname)s - %(message)s')
env_path = Path(__file__).resolve().parent.parent / 'config' / '.env'
load_dotenv(env_path)
user = os.getenv('user')
password = os.getenv('password')
database = os.getenv('database')
#host = 'host.docker.internal'
host = 'localhost'
def get_engine():
logging.info(f"→ Conectando em {host}:5432/{database}")
return create_engine(
f"postgresql+psycopg2://{user}:{quote_plus(password)}@{host}:5432/{database}"
)
engine = get_engine()
def load_weather_data(table_name:str, df):
df.to_sql(
name=table_name,
con=engine,
if_exists='append',
index=False
)
logging.info(f"✅ Dados carregados com sucesso!\n")
df_check = pd.read_sql(f'SELECT * FROM {table_name}', con=engine)
logging.info(f"Total de registros na tabela: {len(df_check)}\n")