Skip to main content

A mock Apache Spark implementation using Polars backend for learning and testing

Project description

Spark Mock 🚀

A mock Apache Spark implementation using Polars as the backend engine. Practice PySpark-like syntax without needing a real Spark cluster!

Python 3.9+ License: MIT

Features ✨

  • PySpark-like API - Familiar DataFrame, Column, and SQL syntax
  • Lazy Evaluation - Just like real Spark, operations are lazily evaluated
  • Polars Backend - Fast execution powered by Polars
  • Spark UI Simulation - HTML-based UI that mimics the real Spark UI
  • SQL Support - Execute SQL queries on DataFrames
  • Window Functions - Support for row_number, rank, lag, lead, etc.

Installation 📦

From PyPI (recommended)

pip install spark-mock

From GitHub

pip install git+https://github.com/datalearn-dev/spark-mock.git

From source

git clone https://github.com/datalearn-dev/spark-mock.git
cd spark-mock
pip install -e .

Quick Start 🎯

from spark_mock.sql import SparkSession
from spark_mock.sql import functions as F

# Create SparkSession
spark = SparkSession.builder \
    .appName("MyApp") \
    .config("spark.mock.partitions", 4) \
    .getOrCreate()

# Create DataFrame
df = spark.createDataFrame([
    (1, "Alice", 30, 50000),
    (2, "Bob", 25, 45000),
    (3, "Charlie", 35, 60000),
], ["id", "name", "age", "salary"])

# Transformations
result = df.filter(F.col("age") > 25) \
           .select("name", "salary") \
           .withColumn("bonus", F.col("salary") * 0.1)

# Show results
result.show()

# SQL queries
df.createTempView("employees")
spark.sql("SELECT name, salary FROM employees WHERE age > 30").show()

# Stop session
spark.stop()

HTML Spark UI 🖥️

Enable the HTML Spark UI to visualize your jobs:

spark = SparkSession.builder \
    .appName("SparkUIDemo") \
    .config("spark.mock.ui.html", "true") \
    .getOrCreate()

# Run some operations...
df.show()

# Open UI in browser
spark.openUI()

Supported Operations 📋

DataFrame Transformations

  • select(), filter(), where()
  • withColumn(), withColumnRenamed(), drop()
  • groupBy(), agg()
  • join(), union(), unionAll()
  • orderBy(), sort(), limit()
  • distinct(), dropDuplicates()

DataFrame Actions

  • show(), collect(), count()
  • first(), head(), take()
  • toPandas()

SQL Functions

  • Aggregation: count, sum, avg, min, max
  • String: concat, upper, lower, trim, substring
  • Math: abs, sqrt, round, floor, ceil
  • Conditional: when, coalesce, isnull
  • Window: row_number, rank, lag, lead

I/O Operations

  • Read/Write: CSV, JSON, Parquet

Configuration ⚙️

Config Key Description Default
spark.mock.partitions Number of partitions 4
spark.mock.ui.console Enable console UI true
spark.mock.ui.html Enable HTML UI false

Development 🛠️

# Clone repository
git clone https://github.com/datalearn-dev/spark-mock.git
cd spark-mock

# Install dependencies
pip install -r requirements.txt -r requirements-dev.txt
pip install -e .

# Run tests
pytest tests/ -v

# Run with coverage
pytest tests/ --cov=spark_mock --cov-report=html

License 📄

MIT License - see LICENSE for details.

Contributing 🤝

Contributions are welcome! Please feel free to submit a Pull Request.

Acknowledgments 🙏

Project details


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

spark_mock-0.1.0.tar.gz (56.5 kB view details)

Uploaded Source

Built Distribution

If you're not sure about the file name format, learn more about wheel file names.

spark_mock-0.1.0-py3-none-any.whl (49.0 kB view details)

Uploaded Python 3

File details

Details for the file spark_mock-0.1.0.tar.gz.

File metadata

  • Download URL: spark_mock-0.1.0.tar.gz
  • Upload date:
  • Size: 56.5 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/6.2.0 CPython/3.13.9

File hashes

Hashes for spark_mock-0.1.0.tar.gz
Algorithm Hash digest
SHA256 a1b20bdc7901c1477ef73d107e81e7036f4075695367c5187473700312020f51
MD5 2adaa454b64bca2ab382877188e95c70
BLAKE2b-256 b1bc3de8e64d12e0d133582e7d7cd9bb1c1e542a987fabe932b0d40c0ba212eb

See more details on using hashes here.

File details

Details for the file spark_mock-0.1.0-py3-none-any.whl.

File metadata

  • Download URL: spark_mock-0.1.0-py3-none-any.whl
  • Upload date:
  • Size: 49.0 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/6.2.0 CPython/3.13.9

File hashes

Hashes for spark_mock-0.1.0-py3-none-any.whl
Algorithm Hash digest
SHA256 e9a08e342e0cfb50b2b09d05531d331c6ae471eb2a7d79703c2b31b2e194c9aa
MD5 1dd147b1f950d59ec2a044fc7cc3b554
BLAKE2b-256 e0ea5f99aa8033cfc1b3f4078d9a860e40f35c083a10d6fea6ae857701d49c12

See more details on using hashes here.

Supported by

AWS Cloud computing and Security Sponsor Datadog Monitoring Depot Continuous Integration Fastly CDN Google Download Analytics Pingdom Monitoring Sentry Error logging StatusPage Status page