Reference library

Big data & Spark

PySpark jobs, partitioning, batch processing, and large-dataset transform patterns.

9 matches
Big data & Spark medium

How to Implement a Streaming Watermark in Python

Mock structured streaming watermarks in Python to track late event times and compute a watermark for windowed processing.

streaming watermark spark
Python
from datetime import datetime, timedelta
import time

class StreamingWatermark:
    """Mock watermark tracker for structured streaming."""

    def __init__(self, watermark_delay_seconds):
        self.watermark_delay = timedelta(seconds=watermark_delay_seconds)
        self.max_event_time = None

    def observe_even…
14 0 Open
Big data & Spark medium

How to Mock Spark Streaming Micro-Batches in Python

Simulate Spark's micro-batch streaming with a simple deque-based class that collects events over time and processes them in timed batches.

spark streaming micro-batch
Python
import time
from collections import deque
from datetime import datetime


class MicroBatchStream:
    def __init__(self, batch_interval_sec=2):
        self.batch_interval = batch_interval_sec
        self.source = deque()
        self.processed = []

    def add_events(self, events):
        self.source.extend(events…
13 0 Open
Big data & Spark medium

How to Mock a Catalyst Logical Plan in Python

Build a small Python class that mimics Spark Catalyst's logical plan tree for teaching or testing query optimizations.

apache-spark logical-plan catalyst
Python
from typing import Any, Dict, List, Optional


class CatalystLogicalPlan:
    """A minimal mock of Catalyst's logical plan for teaching purposes."""
    
    def __init__(self, node_type: str, **kwargs: Any) -> None:
        self.node_type = node_type
        self.attributes: Dict[str, Any] = kwargs
        self.child…
13 0 Open
Big data & Spark medium

How to Mock a Compute-Collect Action Trigger in Python

Mock a compute-collect action trigger using Python's unittest.mock to simulate Spark-style job execution and assert trigger behavior.

testing mock spark
Python
Here's a Python code sample for the problem title "Action trigger compute collect mock":
14 0 Open
Big data & Spark medium

How to Mock a Parquet partitionBy Sink in Python

Manually write a DataFrame to partitioned Parquet files, mimicking Spark's partitionBy sink behavior without Spark.

parquet pyarrow partition
Python
import pyarrow as pa
import pyarrow.parquet as pq
from pathlib import Path
import tempfile
import shutil


def mock_partition_by_sink(data, output_dir, partition_cols):
    table = pa.Table.from_pandas(data)
    schema = table.schema
    unique_combos = table.select(partition_cols).to_pylist()
    seen = set()
    for…
13 0 Open
Big data & Spark medium

How to Mock a UDAF Aggregate Function in Python

This code provides a minimal mock of a User-Defined Aggregate Function (UDAF), simulating the initialize-update-merge-finalize lifecycle with a defaultdict counter.

udaf aggregate mock
Python
from collections import defaultdict

class MockUDAF:
    """A minimal mock of a User-Defined Aggregate Function.

    Simulates aggregate lifecycle: initialize, update per row,
    and finalize the result.
    """

    def __init__(self):
        self._buffer = defaultdict(int)

    def initialize(self):
        """Re…
13 0 Open
Big data & Spark medium

How to use foreachBatch with a mock sink in PySpark

Demonstrates using Spark Structured Streaming's foreachBatch sink to capture and verify streaming batches by writing them into a custom mock sink object.

pyspark structured-streaming foreachbatch
Python
from pyspark.sql import SparkSession
from pyspark.sql.functions import col, lit

class MockSink:
    def __init__(self):
        self.batches = []
    
    def write_batch(self, batch_df, batch_id):
        # Collect batch data as list of dicts for verification
        records = batch_df.collect()
        self.batches…
14 0 Open
Big data & Spark medium

Mock RDD in Python: Simulate Spark RDD Lazy Transformations

Simulate Apache Spark RDD behavior in Python with lazy maps, filters, partitions, and a collect action.

spark rdd big-data
Python
import random

def mock_rdd(data, num_slices=2):
    """
    A simple simulation of Spark RDD behavior with lazy evaluation,
    transformations, and an action.
    """
    class SimpleRDD:
        def __init__(self, data, num_slices=2):
            self.data = data
            self.num_slices = num_slices
           …
13 0 Open
Big data & Spark medium

Skew Join Salting Key in Python (Demo)

Demonstrates skew join salting by expanding a smaller side with salt keys and matching rows on the larger side via random salt assignment.

skew join salting distributed
Python
import random


def skew_join_salting_key(left_df, right_df, salt_range=4):
    """
    Demonstrates skew join salting: expand the smaller side with salt keys,
    then attach a salt key to each row on the larger side.
    Returns a list of (left, right, salt) tuples.
    """
    skewed_left = []
    for row in left_d…
14 0 Open

Browse by section

Each section groups closely related Python snippets.

Big data & Spark — Python code examples

What you will find here

This page collects big data & spark snippets — short, copy-ready Python you can paste into our free online IDE and run without installing anything. Each sample includes a plain-English explanation and the full source code.

Samples vs tutorials and challenges

Samples are quick reference — one concept per page. For step-by-step teaching, use our Python tutorials. To test yourself, try quizzes or coding challenges. Clean up style with the Python formatter.