agentleFS
G1JoshiG1Joshi/Agent-Skills/skills/ai-ml/spark/SKILL.md

spark

Skill15 starsChanged 43 days agoMITView on GitHub

Expert Apache Spark assistance covering PySpark, Spark SQL, DataFrame transformations, broadcast joins, and cluster optimization. Use when processing petabyte distributed data and running big data ETL pipelines.

12 other projects use a version of this.

Adopted by

Most recently taken up by

Used with

Projects with it also use Forge.

Other files people use alongside this one.

Files found in the same public projects as a version of this one, more often than they turn up anywhere else. Each project counts once, large collections count for less, and this file's own repository is left out.
  • ikennaokpalaSkill

    Forge

    Autonomous quality engineering swarm that forges production-ready code through continuous behavioral verification, exhaustive E2E testing, and self-healing fix loops. Combines DDD+ADR+TDD methodology with BDD/Gherkin specifications, 7 quality gates, defect prediction, chaos testing, and cross-context dependency awareness. Architecture-agnostic - works with monoliths, microservices, modular monoliths, and any bounded-context topology.

    Also in 3 projects that use this

The file

We split this skill into the rules it gives an agent

4 rules. 0 are common, 0 are distinctive, and 4 are very specific to this project.

  • 0

    Common

    Most of the 11 edited versions we read keep these.

  • 0

    Distinctive

    This author's own choices. Worth a look.

  • 4

    Very specific

    Tied to their project's names, files or tools. Probably skip.

  • Common
  • Distinctive
  • Very specific
  1. Very specific:

    Use F.broadcast() when joining a large DataFrame with a small table (< 100MB) to eliminate shuffle overhead.

    line 128
  2. Very specific:

    Always specify an explicit checkpointLocation on durable cloud storage for Structured Streaming jobs.

    line 129
  3. Very specific:

    Use Python UDFs (@udf) if native pyspark.sql.functions exist.

    line 135
  4. Very specific:

    Leave small file problems unaddressed.

    line 136

“Common” is measured: kept by at least 6 in 10 of the 11 edited versions of this file we could read. “Very specific” is our guess from the words: it names a file, a command, a setting or the project itself.

Keep what fits and drop the rest. Your version, ready for your agents.
Files
SKILL.md

Apache Spark

Apache Spark is a multi-language engine for executing data engineering, data science, and machine learning on single-node machines or large distributed clusters with Spark Connect.

When to Use

  • Petabyte-Scale Distributed Data Processing: Transforming massive datasets across cloud clusters with Apache Spark 3.5+.
  • Real-Time Streaming Pipelines: Low-latency stream processing with Spark Structured Streaming.
  • Enterprise Lakehouse Architectures: Querying and updating Delta Lake, Apache Iceberg, and Apache Hudi tables.
  • Distributed SQL & Complex Analytical Aggregations: Distributed joins, window functions, and analytics with Catalyst Optimizer.

Quick Start

from pyspark.sql import SparkSession
from pyspark.sql.functions import col, avg

spark = SparkSession.builder \
    .appName("AnalyticsETL") \
    .config("spark.sql.shuffle.partitions", "200") \
    .getOrCreate()

df = spark.read.parquet("s3a://data-lake/sales/")
summary = df.groupBy("country").agg(avg("amount").alias("avg_spent"))
summary.write.mode("overwrite").parquet("s3a://data-lake/reports/summary/")

Core Concepts

Structured DataFrame Transformations & Window Functions

Distributed analytical querying with PySpark:

from pyspark.sql import SparkSession
from pyspark.sql import functions as F
from pyspark.sql.window import Window

spark = SparkSession.builder \
    .appName("CustomerAnalytics2026") \
    .config("spark.sql.adaptive.enabled", "true") \
    .config("spark.sql.shuffle.partitions", "200") \
    .getOrCreate()

# Read partitioned Parquet from cloud storage
df = spark.read.parquet("s3a://data-lake/transactions/")

# Define window partitioned by customer ordered by timestamp
customer_window = Window.partitionBy("customer_id").orderBy("transaction_time")

# Compute cumulative spend and previous transaction date
enhanced_df = df.filter(F.col("status") == "COMPLETED") \
    .withColumn("running_total", F.sum("amount").over(customer_window)) \
    .withColumn("prev_transaction_time", F.lag("transaction_time", 1).over(customer_window)) \
    .withColumn("days_since_last", F.datediff(F.col("transaction_time"), F.col("prev_transaction_time")))

enhanced_df.write \
    .mode("overwrite") \
    .partitionBy("region") \
    .parquet("s3a://data-lake/marts/customer_summary/")

Broadcast Joins for Skewed Dimensions

Optimizing distributed joins between large fact tables and small dimension lookup tables:

# Broadcast small dimension table to all executors, eliminating expensive shuffles
dimension_df = spark.read.parquet("s3a://data-lake/dimensions/merchant_tiers/")

joined_df = df.join(
    F.broadcast(dimension_df),
    on="merchant_id",
    how="inner"
)

Spark Structured Streaming from Kafka

Continuous stream processing with checkpointing:

stream_df = spark.readStream \
    .format("kafka") \
    .option("kafka.bootstrap.servers", "kafka:9092") \
    .option("subscribe", "telemetry-events") \
    .load()

parsed_stream = stream_df.selectExpr("CAST(value AS STRING) as json_payload")

query = parsed_stream.writeStream \
    .format("parquet") \
    .option("path", "s3a://data-lake/raw_stream/") \
    .option("checkpointLocation", "s3a://data-lake/checkpoints/telemetry/") \
    .trigger(processingTime="1 minute") \
    .start()

Common Patterns

Broadcast Join for Skewed Dimensions

Problem: Joining a small lookup table with a massive distributed fact table causes slow shuffle exchanges across executors.

Solution: Broadcast the small table to all worker nodes:

from pyspark.sql.functions import broadcast

large_fact_df = spark.read.parquet("events/")
small_dim_df = spark.read.csv("country_codes.csv", header=True)

# Broadcast join eliminates shuffle network overhead
result_df = large_fact_df.join(broadcast(small_dim_df), "country_id")

Best Practices

Do:

  • Enable Adaptive Query Execution (spark.sql.adaptive.enabled=true) for automatic partition coalescing and join optimization.
  • Use F.broadcast() when joining a large DataFrame with a small table (< 100MB) to eliminate shuffle overhead.
  • Always specify an explicit checkpointLocation on durable cloud storage for Structured Streaming jobs.
  • Cache or persist (df.persist()) intermediate DataFrames only when reused multiple times in the same job.

Don't:

  • Collect massive DataFrames to the driver node (df.collect()); write directly to cloud storage.
  • Use Python UDFs (@udf) if native pyspark.sql.functions exist; native functions avoid JVM-Python serialization overhead.
  • Leave small file problems unaddressed; use Delta Lake OPTIMIZE or coalesce partitions before writing.

Troubleshooting

ErrorCauseSolution
java.lang.OutOfMemoryError: Java heap space on ExecutorData skew or partition sizes too large for executor memory.Increase spark.executor.memory or salt skewed keys before grouping.
Container killed by YARN for exceeding memory limitsOff-heap overhead or Python process exceeding memory threshold.Increase spark.yarn.executor.memoryOverhead to 1GB+.
Total size of serialized results is bigger than spark.driver.maxResultSizeCalling .collect() on massive distributed DataFrame.Write output to disk or use .take(100) instead of collecting entire dataset.

References

Download now (ZIP)

Does it work?

Reviews from people and from AI agents that ran it: what they used it for, and whether it worked.

No reviews yet

People and AI agents can review whether this works in practice. Say what you used it for, and whether it worked.

  • It worked
  • Partly
  • It didn't
What did you use it for? e.g. Reviewing pull requests in a Rails app

Using an agent? It can post a review for you after it has run the file: the MCP tool registry_write, action report. How to connect one.

Your trail

Files you open will collect here, so you can find them again.

Open two or more, and you can remix the rules you like from each into one list. Kept in this browser only.

Now do this for your own.

Bring your CLAUDE.md, AGENTS.md or skills. agentleFS shows which of your rules are common, which are yours and which only fit one project, and keeps one version your agents read everywhere.