mcpField
S
spark-engineer

spark-engineer

github.com/farmage数据与数据库223269 次安装5 周前更新
代码仓库

关于 spark-engineer

spark-engineer 是面向 Apache Spark 工程开发的技能,专注于分布式数据处理、ETL 流水线和性能调优。它结合 Spark UI 分析、分区策略与数据倾斜处理指导,并要求用生产规模数据验证。

主要功能

  1. 01DataFrame 与 RDD 设计指导
  2. 02分区和缓存策略
  3. 03连接与数据倾斜优化
  4. 04Spark UI 性能分析
  5. 05Structured Streaming 指导

适用场景

  1. 01设计大规模 ETL 流水线
  2. 02调优 Spark 连接与聚合
  3. 03排查数据倾斜或 shuffle 溢写
  4. 04构建并验证流处理任务

安装

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

$npx skills add https://github.com/farmage/opencode-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 分区,消除数据倾斜,优化联接和聚合
  5. 验证 - 继续操作前检查 Spark UI 中的 shuffle spill;使用 df.rdd.getNumPartitions() 验证分区数量;如果检测到 spill 或数据倾斜,则返回第 4 步;使用生产规模的数据进行测试,监控资源使用情况,并验证性能目标

参考指南

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

主题参考资料加载时机
Spark SQL 和 DataFramereferences/spark-sql-dataframes.md使用 DataFrame API、Spark SQL、模式、联接和聚合时
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/")
广播联接(小型维度表 < 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)使用广播联接
  • 使用加盐或自定义分区处理数据倾斜
  • 通过 Spark UI 监控 shuffle、spill 和 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 执行引擎、分区策略、广播变量、累加器、Structured Streaming、水位线、检查点、Spark UI 分析、内存管理、shuffle 优化

文档

评论

支持 Markdown