Efektivní provádění dotazů

Scaling and Optimizing Data Pipelines with Polars

Liam Brannigan

Data Scientist & Polars Contributor

Představení instruktora

$$

$$

  • Liam Brannigan, hlavní datový vědec
  • Specialista na ML a datové inženýrství
  • Přispěvatel projektu Polars

Fotografie Liama Brannigana – instruktora tohoto kurzu

Scaling and Optimizing Data Pipelines with Polars

Od notebooku po cloud

Škálování pipeline Polars od notebooku po cloud

Scaling and Optimizing Data Pipelines with Polars

Je tento kurz pro vás?

Stránky kurzu Polars

  • Vytvoření líného dotazu pomocí scan_csv
  • Psaní výrazů s filter, select a group_by
Scaling and Optimizing Data Pipelines with Polars

Kapitola 1 – optimalizace

Optimalizace dotazů

Scaling and Optimizing Data Pipelines with Polars

Kapitola 1 – optimalizace

Optimalizace dotazů

Scaling and Optimizing Data Pipelines with Polars

Kapitola 2 – efektivní I/O

Polars načítá více typů souborů

Scaling and Optimizing Data Pipelines with Polars

Kapitola 3 – bohatší datové typy

Diagram vnořených dat transformovaných do tabulky.

Scaling and Optimizing Data Pipelines with Polars

Kapitola 4 – škálování pipeline

Ukázka streamování velké tabulky.

Scaling and Optimizing Data Pipelines with Polars

Dataset žádostí z Chicaga

Tým datové analytiky primátora Chicaga

Scaling and Optimizing Data Pipelines with Polars

Prozkoumání datasetu

requests = pl.scan_csv("311_Service_Requests.csv",try_parse_dates=True)

Rozsáhlý dataset zachycující každou žádost o službu od občanů Chicaga

Scaling and Optimizing Data Pipelines with Polars

Prozkoumání datasetu

requests.collect()
Scaling and Optimizing Data Pipelines with Polars

Prozkoumání datasetu

requests.collect().head(5)
shape: (5, 39)
| TYPE                          | STATUS    | DEPARTMENT     | CREATED_DATE        | ... |
| ---                           | ---       | ---            | ---                 | --- |
| str                           | str       | str            | str                 | ... |
|-------------------------------|-----------|----------------|---------------------|-----|
| Pothole in Street Complaint   | Completed | Transportation | 2019-12-16T10:09:08 | ... |
| Tree Trim Request             | Cancelled | Sanitation     | 2019-09-18T01:05:08 | ... |
| Garbage Cart Maintenance      | Completed | Sanitation     | 2021-01-24T09:14:58 | ... |
| Pothole in Street Complaint   | Completed | Transportation | 2019-03-21T10:41:01 | ... |
| Recycling Pick Up             | Completed | Sanitation     | 2021-02-16T08:28:59 | ... |
Scaling and Optimizing Data Pipelines with Polars

Omezení počtu zpracovaných řádků

requests.head(5)
Scaling and Optimizing Data Pipelines with Polars

Omezení počtu zpracovaných řádků

requests.head(5).collect()
shape: (5, 39)
| TYPE                            | STATUS    | DEPARTMENT     | CREATED_DATE        | ... |
| ---                             | ---       | ---            | ---                 | --- |
| str                             | str       | str            | str                 | ... |
|---------------------------------|-----------|----------------|---------------------|-----|
| Pothole in Street Complaint     | Completed | Transportation | 2019-12-16T10:09:08 | ... |
| Tree Trim Request | Completed | Sanitation     | 2019-09-18T01:05:08 | ... |
| Garbage Cart Maintenance        | Completed | Sanitation     | 2021-01-24T09:14:58 | ... |
| Pothole in Street Complaint     | Completed | Transportation | 2019-03-21T10:41:01 | ... |
| Recycling Pick Up               | Completed | Sanitation     | 2021-02-16T08:28:59 | ... |
  • Volání collect odložte na konec
Scaling and Optimizing Data Pipelines with Polars

Souhrnný dotaz podle oddělení

completed_by_department


Scaling and Optimizing Data Pipelines with Polars

Souhrnný dotaz podle oddělení

completed_by_department = requests


Scaling and Optimizing Data Pipelines with Polars

Souhrnný dotaz podle oddělení

completed_by_department = requests.filter(
    pl.col("STATUS") == "Completed"
)
Scaling and Optimizing Data Pipelines with Polars

Souhrnný dotaz podle oddělení

completed_by_department = requests.filter(
    pl.col("STATUS") == "Completed"
).collect()
Scaling and Optimizing Data Pipelines with Polars

Souhrnný dotaz podle oddělení

completed_by_department = requests.filter(
    pl.col("STATUS") == "Completed"
).collect().group_by("DEPARTMENT").len()
Scaling and Optimizing Data Pipelines with Polars

Souhrnný dotaz podle oddělení

completed_by_department = requests.filter(
    pl.col("STATUS") == "Completed"
).group_by("DEPARTMENT").len().collect()
shape: (10, 2)
| DEPARTMENT                     | len     |
| ---                            | ---     |
| str                            | u32     |
|-------------------------------|---------|
| 311 City Services              | 4859161 |
| Sanitation                     | 3406631 |
| Aviation                       | 2337842 |
| CDOT - Department of Transport | 1468240 |
Scaling and Optimizing Data Pipelines with Polars

Rozbíhající se větve dotazu

completed_by_department = requests.filter(
    pl.col("STATUS") == "Completed"
).group_by("DEPARTMENT").len()
completed_by_month = requests.filter(
    pl.col("STATUS") == "Completed"
).group_by("MONTH").len()
Scaling and Optimizing Data Pipelines with Polars

Jak tým dotazy spouští dnes

completed_by_department.collect()
completed_by_month.collect()
Scaling and Optimizing Data Pipelines with Polars

Spouštění rozbíhajících se dotazů

results = pl.collect_all(


)
Scaling and Optimizing Data Pipelines with Polars

Spouštění rozbíhajících se dotazů

results = pl.collect_all([
    completed_by_department,
    completed_by_month,
])
Scaling and Optimizing Data Pipelines with Polars

Spouštění rozbíhajících se dotazů

results[0]  # completed_by_department
shape: (10, 2)
| DEPARTMENT           | len     |
| ---                  | ---     |
| str                  | u32     |
|----------------------|---------|
| 311 City Services    | 4859161 |
| Sanitation           | 3406631 |
| Aviation             | 2337842 |
| Transport            | 1468240 |
results[1]  # completed_by_month
shape: (12, 2)
| MONTH | len  |
| ---   | ---  |
| i64   | u32  |
|-------|------|
| 1     | 2506 |
| 2     | 4566 |
| 3     | 2739 |
| 4     | 2922 |
Scaling and Optimizing Data Pipelines with Polars

Pojďme procvičovat!

Scaling and Optimizing Data Pipelines with Polars

Preparing Video For Download...