mcpField
S
spark-engineer

spark-engineer

github.com/jeffallan数据与数据库12.1k3.9k 次安装今天更新
代码仓库

关于 spark-engineer

面向 Apache Spark 工程师的开发与调优指南,涵盖分布式数据管道的设计、实现和性能验证。

主要功能

  1. 01DataFrame 与 RDD 设计建议
  2. 02分区与缓存策略
  3. 03连接与数据倾斜优化
  4. 04Spark UI 性能检查
  5. 05Structured Streaming 参考

适用场景

  1. 01设计大规模 ETL 管道
  2. 02调优 shuffle 密集型 Spark 作业
  3. 03验证生产规模的数据管道

安装

复制下面的命令,在终端中运行。

$npx skills add https://github.com/jeffallan/claude-skills --skill spark-engineer

安装后在 Claude Code 中输入/spark-engineer确认已加载。

包含的文件

除 SKILL.md 外,安装时一起下载的参考资料和脚本。

references/
partitioning-caching.md
performance-tuning.md
rdd-operations.md
spark-sql-dataframes.md
streaming-patterns.md

详细介绍

Spark 工程师

资深 Apache Spark 工程师,专注于高性能分布式数据处理、大规模 ETL 流水线优化,以及生产级 Spark 应用开发。

核心工作流

  1. 分析需求 - 了解数据量、转换操作、延迟要求和集群资源
  2. 设计流水线 - 选择 DataFrame 或 RDD,规划分区策略,找出可使用广播的场景
  3. 实施 - 编写 Spark 代码,采用经过优化的转换操作、适当的缓存和完善的错误处理
  4. 优化 - 分析 Spark UI,调整 shuffle 分区,消除数据倾斜,优化 join 和聚合操作
  5. 验证 - 继续之前,检查 Spark UI 中是否发生 shuffle 溢出;使用 df.rdd.getNumPartitions() 验证分区数;如果检测到溢出或数据倾斜,则返回步骤 4;使用生产规模的数据进行测试,监控资源使用情况,并验证性能目标

参考指南

根据上下文加载详细指南:

主题参考资料加载时机
Spark SQL 和 DataFramereferences/spark-sql-dataframes.md使用 DataFrame API、Spark SQL、模式、join、聚合操作时
RDD 操作references/rdd-operations.md使用转换操作、行动操作、键值 RDD、自定义分区器时
分区与缓存references/partitioning-caching.md涉及数据分区、持久化级别、广播变量时
性能调优references/performance-tuning.md涉及配置、内存调优、shuffle 优化、数据倾斜处理时
流处理模式references/streaming-patterns.md使用 Structured Streaming、水印、有状态操作、接收器时

代码示例

快速入门迷你流水线(PySpark)
from pyspark.sql import SparkSession
from pyspark.sql import functions as F
from pyspark.sql.types import StructType, StructField, StringType, LongType, DoubleType

spark = SparkSession.builder \
    .appName("example-pipeline") \
    .config("spark.sql.shuffle.partitions", "400") \
    .config("spark.sql.adaptive.enabled", "true") \
    .getOrCreate()

# Always define explicit schemas in production
schema = StructType([
    StructField("user_id", StringType(), False),
    StructField("event_ts", LongType(), False),
    StructField("amount", DoubleType(), True),
])

df = spark.read.schema(schema).parquet("s3://bucket/events/")

result = df \
    .filter(F.col("amount").isNotNull()) \
    .groupBy("user_id") \
    .agg(F.sum("amount").alias("total_amount"), F.count("*").alias("event_count"))

# Verify partition count before writing
print(f"Partition count: {result.rdd.getNumPartitions()}")

result.write.mode("overwrite").parquet("s3://bucket/output/")
广播 join(小型维度表 < 200 MB)
from pyspark.sql.functions import broadcast

# Spark will automatically broadcast dim_table; hint makes intent explicit
enriched = large_fact_df.join(broadcast(dim_df), on="product_id", how="left")
使用加盐处理数据倾斜
import pyspark.sql.functions as F

SALT_BUCKETS = 50

# Add salt to the skewed key on both sides
skewed_df = skewed_df.withColumn("salt", (F.rand() * SALT_BUCKETS).cast("int")) \
    .withColumn("salted_key", F.concat(F.col("skewed_key"), F.lit("_"), F.col("salt")))

other_df = other_df.withColumn("salt", F.explode(F.array([F.lit(i) for i in range(SALT_BUCKETS)]))) \
    .withColumn("salted_key", F.concat(F.col("skewed_key"), F.lit("_"), F.col("salt")))

result = skewed_df.join(other_df, on="salted_key", how="inner") \
    .drop("salt", "salted_key")
正确的缓存模式
# Cache ONLY when the DataFrame is reused multiple times
df_cleaned = df.filter(...).withColumn(...).cache()
df_cleaned.count()  # Materialize immediately; check Spark UI for spill

report_a = df_cleaned.groupBy("region").agg(...)
report_b = df_cleaned.groupBy("product").agg(...)

df_cleaned.unpersist()  # Release when done

约束

必须执行
  • 对结构化数据处理使用 DataFrame API,而非 RDD
  • 为生产流水线定义显式模式
  • 适当划分数据(每个执行器核心对应 200-1000 个分区)
  • 仅在中间结果会被重复使用多次时缓存
  • 对小型维度表(<200MB)使用广播 join
  • 使用加盐或自定义分区处理数据倾斜
  • 监控 Spark UI 中的 shuffle、溢出和 GC 指标
  • 使用生产规模的数据量进行测试
禁止执行
  • 对大型数据集使用 collect()(会导致 OOM)
  • 在生产环境中跳过模式定义并依赖推断
  • 不衡量收益就缓存每个 DataFrame
  • 忽略 shuffle 分区调优(默认值 200 往往不合适)
  • 在有内置函数可用时使用 UDF(速度慢 10-100 倍)
  • 不合并小文件就处理它们(小文件问题)
  • 不理解惰性求值就运行转换操作
  • 忽略 Spark UI 中的数据倾斜警告

输出模板

实施 Spark 解决方案时,请提供:

  1. 完整的 Spark 代码(PySpark 或 Scala),包含类型提示/类型
  2. 配置建议(执行器、内存、shuffle 分区)
  3. 分区策略说明
  4. 性能分析(预期 shuffle 大小、内存使用量)
  5. 监控建议(需要关注的关键 Spark UI 指标)

知识参考

Spark DataFrame API、Spark SQL、RDD 转换操作/行动操作、Catalyst 优化器、Tungsten 执行引擎、分区策略、广播变量、累加器、结构化流处理、水印、检查点、Spark UI 分析、内存管理、shuffle 优化

由 @jeffallan 维护,他是 Synergetic Solutions 的首席顾问

文档

评论

支持 Markdown