Reference library

Big data & Spark

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

5 matches
Big data & Spark easy

How to Broadcast a Small Lookup Table in Python

Simulates broadcasting a small lookup table by iterating key-value pairs and emitting packed rows to subscribers with deterministic output.

broadcast lookup-table dictionary
Python
import random

# Generate a deterministic mock broadcast of a small lookup table
# with 5 keys and random integer values (seeded for reproducibility)

data = {
    "sensor_a": 22,
    "sensor_b": 87,
    "sensor_c": 43,
    "sensor_d": 65,
    "sensor_e": 31,
}

# Simulate a broadcast to subscribers by iterating and p…
14 0 Open
Big data & Spark easy

How to Explode an Array Column in Python

This code demonstrates a mock explode operation that converts an array column into multiple rows, similar to Spark's explode function.

explode arrays pyspark
Python
import json 

def explode_array_column(data, column):
    """Mock explode: split array column into multiple rows."""
    exploded = []
    for row in data:
        values = row.get(column, [])
        for value in values:
            new_row = dict(row)
            new_row[column] = value
            exploded.append(n…
13 0 Open
Big data & Spark easy

How to Filter and Project Spark DataFrames with PySpark SQL

Simulate a SQL SELECT with WHERE using PySpark DataFrame select and filter to project columns and apply conditions.

pyspark dataframe filter
Python
from pyspark.sql import SparkSession
from pyspark.sql.functions import col

spark = SparkSession.builder.appName("QueryFilterMock").master("local[2]").getOrCreate()

data = [
    ("Alice", 28, "Engineering"),
    ("Bob", 35, "Sales"),
    ("Carol", 32, "Engineering"),
    ("David", 25, "Marketing"),
    ("Eve", 29, "E…
14 0 Open
Big data & Spark easy

How to Mock Hive Support in PySpark with unittest.mock

This code demonstrates how to mock Hive support in a PySpark environment using unittest.mock to simulate SQL queries returning fixed data.

pyspark hive mock
Python
from unittest.mock import Mock, patch


def get_hive_tables(spark):
    """Mock Hive support by returning a fixed list of tables."""
    return spark.sql("SHOW TABLES").collect()


class HiveTable:
    """Simple class that mimics a Hive table row."""
    def __init__(self, database, tableName):
        self.database =…
14 0 Open
Big data & Spark easy

How to Use Broadcast Variables as Read-Only in PySpark (Mock Example)

Share a lookup dict across Spark executors with a broadcast variable and verify its read-only behavior in a local mock.

pyspark broadcast spark
Python
from pyspark import SparkContext, SparkConf

def main():
    conf = SparkConf().setAppName("BroadcastMock").setMaster("local[2]")
    sc = SparkContext(conf=conf)
    
    lookup = {"a": 1, "b": 2, "c": 3}
    broadcast_lookup = sc.broadcast(lookup)
    
    data = ["a", "b", "c", "a", "unknown"]
    rdd = sc.parallel…
13 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.