Estrarre dati da fonti strutturate

ETL and ELT in Python

Jake Roach

Data Engineer

Sistemi sorgente

In questo corso:

  • File CSV
  • File Parquet
  • File JSON
  • Database SQL

I dati provengono anche da:

  • API
  • Data lake
  • Data warehouse
  • Web scraping
  • ... e molto altro!
ETL and ELT in Python

Lettura di file Parquet

File Parquet:

  • Formato di file open source orientato alle colonne, progettato per archiviare e recuperare campi in modo efficiente
  • Simile al lavoro con i file CSV
import pandas as pd

# Read the parquet file into memory
raw_stock_data = pd.read_parquet("raw_stock_data.parquet", engine="fastparquet")
1 https://www.databricks.com/glossary/what-is-parquet
ETL and ELT in Python

Connessione a database SQL

  • Puoi estrarre dati da database SQL in un DataFrame pandas
  • Serve una connection URI per creare un engine e connetterti al database
import sqlalchemy
import pandas as pd

# Connection URI: schema_identifier://username:password@host:port/db
connection_uri = "postgresql+psycopg2://repl:password@localhost:5432/market"
db_engine = sqlalchemy.create_engine(connection_uri)
# Query the SQL database
raw_stock_data = pd.read_sql("SELECT * FROM raw_stock_data LIMIT 10", db_engine)
ETL and ELT in Python

Modularità

Separare la logica in funzioni

  • Aumenta la leggibilità della pipeline
  • Rispetta il principio "don't repeat yourself"
  • Velocizza la risoluzione dei problemi
def extract_from_sql(connection_uri, query):
    # Create an engine, query data and return DataFrame
    db_engine = sqlalchemy.create_engine(connection_uri)
    return pd.read_sql(query, db_engine)

extract_from_sql("postgresql+psycopg2://.../market", "SELECT ... LIMIT 10;")
ETL and ELT in Python

Esercitiamoci!

ETL and ELT in Python

Preparing Video For Download...