Spark日志分析AI代理

一个集成Apache Spark历史服务器和LangGraph AI代理的工具,用于通过自然语言分析Spark应用日志,诊断性能问题并识别瓶颈。

  • by Kacper0199
  • Python
  • 本地部署
  • 更新于
在 GitHub 查看 Kacper0199/Spark-History-MCP-LangGraph-AI-Agent

使用说明

Spark历史MCP服务器和LangGraph AI代理集成

仓库概览

该存储库提供了一个完整的基础设施和一个AI代理,用于使用自然语言分析Apache Spark应用程序日志。

它集成了:

  1. Apache Spark历史服务器:用于托管和提供事件日志。
  2. Spark历史MCP服务器:通过模型上下文协议(MCP)将Spark指标暴露给AI模型的桥梁。
  3. LangGraph代理:一个本地AI助手(由Ollama提供支持),可以查询MCP服务器以诊断性能问题、识别瓶颈并总结作业状态。
  4. Spark作业运行器:一个实用程序容器,用于从真实图算法(PageRank、Connected Components)生成示例事件日志。

整个堆栈在Docker中运行,保持本地环境的干净,而AI Agent在本地Python虚拟环境中运行,以提高速度和灵活性。


项目结构

Repo
├── docker-compose.yaml              # Infrastructure definition (Spark HS, MCP Server, Job Runner)
├── main.py                          # Main entry point for the AI Agent
├── requirements_agent.txt           # Python dependencies for the local AI Agent
├── requirements_spark.txt           # Python dependencies for the Dockerized Spark Job Runner
├── config/
│   ├── agent_config.yaml            # Agent settings (model selection, parameters, URLs)
│   ├── paths_config.yaml            # Data and log paths
│   └── prompts.yaml                 # System prompts and tool definitions for the LLM
├── data/
│   └── facebook_large/              # Input dataset (Download required)
│       ├── musae_facebook_edges.csv
│       └── musae_facebook_target.csv
├── scripts/
│   ├── close_infrastructure.sh      # Stops all Docker containers
│   ├── generate_data.sh             # Helper script to trigger data generation manually
│   ├── run_agent.sh                 # Sets up venv and runs the AI Agent
│   └── start_infrastructure.sh      # Starts Docker containers and data generation
├── spark-events/                    # Output folder for Spark Event Logs (mounted to Docker)
└── src/
    ├── agent/                       # Agent logic (LangGraph, Tools)
    │   ├── __init__.py
    │   ├── graph.py
    │   └── tools.py
    ├── spark_job/                   # PySpark script for generating sample logs
    │   ├── __init__.py
    │   └── generate_logs.py
    └── utils/                       # Helper functions (logging, config loading)
        ├── __init__.py
        ├── config_loader.py
        └── loggers.py

安装和设置

1.先决条件

  • Docker&Docker编写:确保Docker桌面正在运行。
  • Python 3.10+:已安装在本地计算机上。
  • 奥拉玛:已安装并正在运行。

2.准备数据

下载 Facebook Large Page-Page Network 数据集,并将CSV文件放置在 data/facebook_large/ 文件夹。

点击此处下载: snap.stanford.edu/data/facebook-large-page-network.html

  • 输入: data/facebook_large/musae_facebook_edges.csv
  • 输出(自动): spark-events/ (在运行基础设施后创建)。

3.准备本地AI模型

打开一个单独的终端,确保Ollama正在提供中指定的型号 config/agent_config.yaml (默认值为 llama3.1:8b).

# 1. Start Ollama (if not running in background app)
ollama serve

# 2. Pull the model (in a new terminal tab)
ollama pull llama3.1:8b

4.使脚本可执行

为辅助脚本授予执行权限:

chmod +x scripts/*.sh

执行

第一步:启动基础设施

此脚本初始化Docker容器。

  • 如果 spark-events 是空的,它运行 spark-job-runner 容器生成数据(这需要几分钟的时间)。
  • 然后,它开始 spark-history-server 和 mcp-server.
./scripts/start_infrastructure.sh

*等到你看到“基础设施就绪!”和网址*

步骤2:运行AI代理

此脚本处理本地Python环境。

  • 它检查a .venv 文件夹。
  • 如果缺少,它将创建它并从安装依赖项 requirements.txt.
  • 然后启动交互式Agent。
./scripts/run_agent.sh

交互示例:

用户: “列出历史服务器中可用的所有应用程序”

代理人: (返回带有ID的应用程序列表)

用户: “分析应用程序本地-17…找出最慢的前3个阶段。”

步骤3:停止基础设施

完成后,使用此脚本停止并删除Docker容器。

./scripts/close_infrastructure.sh

脚本参考

  • scripts/start_infrastructure.sh:协调启动。检查现有数据,以避免不必要地重新运行繁重的Spark作业。
  • scripts/run_agent.sh:包装材料 main.py.管理虚拟环境(.venv)自动,这样你就不会污染你的全局Python。
  • scripts/close_infrastructure.sh:快捷方式 docker-compose down 以确保干净关机。

配置和定制

代理配置(config/agent_config.yaml)

此文件控制本地AI模型设置。切换Ollama提供的LLM(例如,使用 qwen2.5:14b 而不是 llama3.1:8b),只需更新 model_name 参数。

model_name: "llama3.1:8b"  # Change this to your pulled Ollama model
temperature: 0.0           # Keep low for deterministic tool usage
base_url: "http://localhost:11434"

系统提示(config/prompts.yaml)

此文件定义了代理使用的系统提示。它充当LLM的备忘单,列出了可用的工具及其所需的JSON参数模式。

如果你需要验证可用的工具或在提示说明中添加新的工具,你可以在这里参考官方的源代码定义: github/kubeflow/mcp-apache-stark历史-服务器/blob/main/src/spark-history_mcp/tools/tools.py