Download templates/distributed.html from hengds2f/capstone: direct link, hf CLI and curl.
- Browser
- Download file 5.16 kB
-
https://huggingface.co/spaces/hengds2f/capstone/resolve/main/templates/distributed.html
- Command line
-
hf download hf://spaces/hengds2f/capstone/templates/distributed.html
-
curl -L -o distributed.html https://huggingface.co/spaces/hengds2f/capstone/resolve/main/templates/distributed.html
5.16 kB
| {% extends "base.html" %} | |
| {% block title %}Distributed Processing - Data Science Lab{% endblock %} | |
| {% block content %} | |
| <div class="page-header"> | |
| <h1>π Distributed Batch Processing</h1> | |
| <p class="breadcrumb">Home / Distributed β Scaling with Dask, PySpark, and MapReduce</p> | |
| </div> | |
| <div class="concept-section"> | |
| <div class="concept-header"><h3>π Why Distributed Processing?</h3></div> | |
| <div class="concept-body"> | |
| <p>When a single machine can't handle the data or compute, we distribute work across a cluster of machines.</p> | |
| <table class="table"> | |
| <tr><th>Framework</th><th>Best For</th><th>Use Case</th></tr> | |
| <tr><td>Dask</td><td>Scaling pandas workflows</td><td>Processing 10 years of housing data</td></tr> | |
| <tr><td>PySpark</td><td>Petabyte-scale ETL</td><td>Transport Authority taxi GPS data (billions of rows)</td></tr> | |
| <tr><td>MapReduce</td><td>Batch processing concept</td><td>Counting word frequency across documents</td></tr> | |
| </table> | |
| </div> | |
| </div> | |
| <div class="concept-section"> | |
| <div class="concept-header"><h3>π Dask β Parallel Pandas</h3></div> | |
| <div class="concept-body"> | |
| <div class="code-block"><span class="lang-label">Python</span><pre>import dask.dataframe as dd | |
| # Dask reads data lazily β no memory overflow | |
| ddf = dd.read_csv('housing_data_*.csv') # Multiple files as partitions | |
| # Same pandas API β but executed in parallel | |
| result = ( | |
| ddf[ddf['town'] == 'DOWNTOWN'] | |
| .groupby('property_type')['price'] | |
| .mean() | |
| .compute() # β Triggers actual computation | |
| ) | |
| print(result) | |
| # Key Dask concepts: | |
| # 1. Lazy evaluation β nothing runs until .compute() | |
| # 2. Partitions β data is split across workers | |
| # 3. Task graph β Dask builds a DAG of operations</pre></div> | |
| </div> | |
| </div> | |
| <div class="concept-section"> | |
| <div class="concept-header"><h3>π PySpark β Big Data at Scale</h3></div> | |
| <div class="concept-body"> | |
| <div class="code-block"><span class="lang-label">Python</span><pre>from pyspark.sql import SparkSession | |
| from pyspark.sql.functions import avg, count, col | |
| spark = SparkSession.builder.appName("SG_housing").getOrCreate() | |
| # Read data into a distributed DataFrame | |
| df = spark.read.csv("housing_data_full.csv", header=True, inferSchema=True) | |
| # Transformations (lazy β builds execution plan) | |
| result = ( | |
| df.filter(col("town") == "DOWNTOWN") | |
| .groupBy("property_type") | |
| .agg( | |
| avg("price").alias("avg_price"), | |
| count("*").alias("count") | |
| ) | |
| .orderBy("avg_price", ascending=False) | |
| ) | |
| # Action (triggers execution across cluster) | |
| result.show() | |
| # +----------+----------+-----+ | |
| # | property_type| avg_price|count| | |
| # +----------+----------+-----+ | |
| # |PREMIUM | 820000.0 | 45 | | |
| # | LARGE | 650000.0 | 120 | | |
| # | MEDIUM | 480000.0 | 350 | | |
| # | SMALL | 350000.0 | 200 | | |
| # +----------+----------+-----+</pre></div> | |
| </div> | |
| </div> | |
| <div class="concept-section"> | |
| <div class="concept-header"><h3>π MapReduce Pattern</h3></div> | |
| <div class="concept-body"> | |
| <div class="code-block"><span class="lang-label">Python</span><pre>from functools import reduce | |
| from collections import Counter | |
| # Simulating MapReduce for housing price analysis | |
| data_partitions = [ | |
| [("DOWNTOWN", 500000), ("MIDTOWN", 420000), ("DOWNTOWN", 520000)], | |
| [("MIDTOWN", 430000), ("DOWNTOWN", 510000), ("GREENFIELD", 350000)], | |
| [("GREENFIELD", 360000), ("MIDTOWN", 440000), ("GREENFIELD", 340000)], | |
| ] | |
| # MAP phase: Each partition emits (key, value) pairs | |
| def map_fn(partition): | |
| return [(town, price) for town, price in partition] | |
| # SHUFFLE: Group by key (handled by framework) | |
| def shuffle(mapped_results): | |
| groups = {} | |
| for pairs in mapped_results: | |
| for town, price in pairs: | |
| groups.setdefault(town, []).append(price) | |
| return groups | |
| # REDUCE phase: Aggregate values for each key | |
| def reduce_fn(key, values): | |
| return (key, sum(values) / len(values), len(values)) | |
| # Execute MapReduce | |
| mapped = [map_fn(p) for p in data_partitions] # Parallel | |
| shuffled = shuffle(mapped) # Framework | |
| results = [reduce_fn(k, v) for k, v in shuffled.items()] # Parallel | |
| for town, avg_price, count in results: | |
| print(f"{town}: avg ${avg_price:,.0f} ({count} transactions)")</pre></div> | |
| </div> | |
| </div> | |
| <div class="common-mistake"> | |
| <h5>β οΈ Common Mistakes</h5> | |
| <ul> | |
| <li><strong>Using .collect() on large data</strong>: Pulls all data to driver β causes OOM. Use .show() or .take(10)</li> | |
| <li><strong>Not partitioning properly</strong>: Too few partitions = uneven load; too many = overhead</li> | |
| <li><strong>Ignoring data skew</strong>: If one key (e.g., "WESTSIDE") has 10x more data, that partition becomes a bottleneck</li> | |
| </ul> | |
| </div> | |
| <div class="exercise-box"> | |
| <h5>π§ͺ Exercises</h5> | |
| <p>1. Write a MapReduce function that finds the maximum price per town.</p> | |
| <p>2. When would you choose Dask over PySpark? List 3 criteria.</p> | |
| <p>3. Explain what "data skew" means using an housing resale example.</p> | |
| </div> | |
| {% endblock %} | |