Trích xuất dữ liệu từ nguồn có cấu trúc

ETL và ELT với Python

Jake Roach

Data Engineer

Hệ thống nguồn

Trong khóa học này:

  • Tệp CSV
  • Tệp Parquet
  • Tệp JSON
  • Cơ sở dữ liệu SQL

Dữ liệu cũng đến từ:

  • API
  • Data lake
  • Data warehouse
  • Web scraping
  • ... và còn rất nhiều nữa!
ETL và ELT với Python

Đọc tệp Parquet

Tệp Parquet:

  • Định dạng tệp mã nguồn mở, theo cột, tối ưu cho lưu trữ và truy xuất trường dữ liệu hiệu quả
  • Tương tự làm việc với tệp 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 và ELT với Python

Kết nối đến cơ sở dữ liệu SQL

  • Có thể lấy dữ liệu từ cơ sở dữ liệu SQL vào DataFrame của pandas
  • Cần một connection URI để tạo engine và kết nối đến cơ sở dữ liệu
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 và ELT với Python

Tính mô-đun

Tách logic thành các hàm

  • Tăng khả năng đọc trong pipeline
  • Tuân thủ nguyên tắc "don't repeat yourself"
  • Đẩy nhanh việc xử lý sự cố
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 và ELT với Python

Cùng luyện tập nào!

ETL và ELT với Python

Preparing Video For Download...