在 Managed Service for Apache Spark 上使用 Gemini 和 MCP 进行平台工程

1. 简介

管理大规模数据处理工作负载通常需要在编写复杂的 ETL 代码和调整基础架构之间取得平衡。使用 Managed Service for Apache Spark,您将了解不断提升的 AI 上下文如何将复杂的 Spark 调试转变为可自我修复的操作工作流。

此过渡表明,AI 功能直接取决于环境背景。此 Codelab 涵盖了运营成熟度的六个关键阶段,展示了数据工程师如何从手动日志解析发展到自主 AI 智能体技能和云原生可观测性,而这些都由底层云架构提供支持。

架构组件

如需了解 Google Antigravity CLI 如何与云环境互动,请考虑此设置的三个核心架构组件:

  • 本地环境:本地开发者终端或 Cloud Shell 将 Google Antigravity CLI 作为主要界面运行。此终端客户端会捕获您的自然语言命令,并收集静态文件,例如您的 PySpark 脚本或批处理参数。它充当安全客户端,可将用户提示和本地文件转换为标准化的 Model Context Protocol 请求。
  • Google Cloud 项目:您的 Google Cloud 项目包含大数据资产、正在运行的无服务器工作负载和集中式日志记录。您可以在 Managed Service for Apache Spark 上执行 PySpark 批处理,该服务会管理底层计算基础设施。原始日志会流式传输到 Cloud Logging,数据输入位于 Cloud Storage 中,从而形成需要监控的实时环境。
  • Google 远程 MCP 服务器架构:远程 MCP 服务器充当本地客户端与 Google Cloud API 之间的受管理代理。您的 Google Antigravity CLI 通过区域性 HTTP 端点(例如 https://dataproc-$REGION.googleapis.com/mcp)连接到 Managed Service for Apache Spark API (dataproc.googleapis.com)。通过这种连接,模型可以安全地执行 get_batch 或 list_batches 等标准化工具,而无需进行自定义 SDK 开发。

下图定义了系统拓扑和互动流程:

Spark MCP 架构和系统拓扑

您将执行的操作

  • 手动问题排查:提交一个损坏的 Spark 作业,并使用日志和控制台命令手动找到故障。
  • Web AI 诊断:查询 Gemini 网页界面,检查语法错误并了解零上下文限制。
  • 终端 AI 调试:使用 Google Antigravity CLI 和 Apache Spark Model Context Protocol (MCP) 服务器,大幅提升您的开发者工作流程效率。
  • 自定义自动化:将诊断工作流打包到代理技能中,该技能可以自动验证、检查和恢复失败的作业。
  • 云原生控制台诊断:在 Cloud Logging 和 Google Cloud 控制台中使用 Gemini 来检查实时批处理日志并查询操作指南。

所需条件

开始之前,请确保您已具备以下条件:

  • 已启用结算功能的 Google Cloud 账号。
  • 对 gcloud CLI 和终端使用有基本的了解。
  • 熟悉 Python 和 Apache Spark。

2. 设置:项目和环境

如需运行此 Codelab,您需要一个启用了结算功能的 Google Cloud 项目。您可以创建新项目,也可以选择现有项目。

设置环境变量并创建 Cloud Storage 存储分区

您可以在 Cloud Shell 中运行此 Codelab。

将有效项目 ID、区域和唯一的存储分区名称设置为环境变量。

在 Cloud Shell 中,验证您的项目 ID 是否已配置:

PROJECT_ID=$(gcloud config get-value project)
echo $PROJECT_ID

如果您的项目 ID 未定义,请进行配置:

export PROJECT_ID=<YOUR_PROJECT_ID>
gcloud config set project $PROJECT_ID

设置区域,例如 us-central1。

export REGION="<REGION>"

设置存储分区名称。

export BUCKET_NAME="${PROJECT_ID}-spark"

Managed Service for Apache Spark 需要一个 Cloud Storage 存储分区来暂存作业依赖项和运行时日志。创建存储分区:

gcloud storage buckets create gs://$BUCKET_NAME --location=$REGION

启用必要的 API

为 Managed Service for Apache Spark、Cloud Storage、Compute Engine 和 Cloud Resource Manager 启用所需的 API。在 Cloud Shell 或终端中运行以下命令:

gcloud services enable \
    dataproc.googleapis.com \
    storage.googleapis.com \
    compute.googleapis.com \
    aiplatform.googleapis.com \
    cloudresourcemanager.googleapis.com

以下是您需要这些 API 的原因:

  • dataproc.googleapis.com:启用 Managed Service for Apache Spark,以便您可以运行无服务器批处理。
  • storage.googleapis.com:启用 Cloud Storage 以托管 PySpark 脚本、依赖项和数据。
  • compute.googleapis.com:为 Managed Service for Apache Spark 执行提供底层 VPC 网络。
  • aiplatform.googleapis.com:为 Gemini 模型推荐和 Gemini Enterprise Agent Platform 集成提供支持。
  • cloudresourcemanager.googleapis.com:启用 Cloud Resource Manager 以检查项目级元数据并验证项目配置。

在子网中启用专用 Google 访问通道

Managed Service for Apache Spark 要求您的 VPC 网络子网已启用专用 Google 访问通道,以便无服务器 Spark 驱动程序和执行程序可以与 Google API 通信。通过运行以下命令,在 default 子网上启用该功能:

gcloud compute networks subnets update default \
    --region=$REGION \
    --enable-private-ip-google-access

设置凭证

使用应用默认凭证 (ADC) 对本地终端环境进行身份验证,以便 Google Cloud SDK 和 Spark MCP 服务器可以访问您的项目资源:

gcloud auth application-default login

3. 在没有 AI 的情况下手动排查 Spark 问题

在探讨 Gemini 如何提供帮助之前,我们先来体验一下手动排查问题的痛苦。您编写一个有问题的 PySpark 脚本,将其提交给 Managed Service for Apache Spark,并尝试使用传统方法查找错误。

编写有问题的 PySpark 脚本

在终端中创建一个名为 broken_spark_job.py 的文件。此脚本包含多个不同的错误:它尝试使用名为 user_schema 的未定义变量、使用拼写错误的 withColunm 方法名称,并引用不存在的 Cloud Storage 存储分区。

如需写入此文件,请运行以下命令:

cat << 'EOF' > broken_spark_job.py
import sys
from pyspark.sql import SparkSession
from pyspark.sql.types import StructType, StructField, StringType, IntegerType
from pyspark.sql.functions import lit

def main():
    # Initialize the Spark Session
    spark = SparkSession.builder \
        .appName("BrokenSparkJob") \
        .getOrCreate()

    print("Spark session started successfully!")

    # Define the actual schema
    schema = StructType([
        StructField("name", StringType(), True),
        StructField("age", IntegerType(), True)
    ])

    data = [("Alice", 30), ("Bob", 25)]
    
    # This will fail first because 'user_schema' is not defined!
    df = spark.createDataFrame(data, schema=user_schema)
    
    # This will fail second because of a misspelled method typo!
    df_with_status = df.withColunm("status", lit("active"))
    df_with_status.show()

    # This will fail third because the bucket does not exist!
    df_gcs = spark.read.csv("gs://my-missing-bucket/input.csv", header=True)
    df_gcs.show()

    spark.stop()

if __name__ == "__main__":
    main()
EOF

将作业提交到 Managed Service for Apache Spark

将此 PySpark 作业提交到 Managed Service for Apache Spark:

gcloud dataproc batches submit pyspark broken_spark_job.py \
    --region=$REGION \
    --version=2.3 \
    --deps-bucket=gs://$BUCKET_NAME

该命令会启动无服务器批处理。片刻之后,作业状态会从 PENDING 转换为 RUNNING,最终转换为 FAILED。

查找失败(繁琐的方法)

虽然在此示例中,您可能在终端中看到了错误消息,但假设这是一个自动化作业,您想事后进行调试。

  1. 在浏览器中打开 Managed Service for Apache Spark 批次控制台。
  2. 点击相应批次,然后在“摘要”标签页中滚动到作业日志。
  3. 过滤系统日志以查找 Python 堆栈轨迹。

最终,您找到了 Python 异常:NameError: name 'user_schema' is not defined。

使用 sed 修正第一个拼写错误:

sed -i 's/schema=user_schema/schema=schema/g' broken_spark_job.py

再次提交作业以触发第二次失败:

gcloud dataproc batches submit pyspark broken_spark_job.py \
    --region=$REGION \
    --version=2.3 \
    --deps-bucket=gs://$BUCKET_NAME

第二次执行也会失败。这次的错误有所不同。也许 AI 可以提供帮助?

4. 查询 Gemini Web 界面

在浏览器中打开 Gemini Web 界面,从终端复制 PySpark 轨迹,然后将其粘贴到提示框中:

py4j.protocol.Py4JJavaError: An error occurred while calling o80.load.
org.apache.spark.SparkException: Job aborted due to stage failure.
Traceback (most recent call last):
  File "broken_spark_job.py", line 26, in main
    df_with_status = df.withColunm("status", lit("active"))
AttributeError: 'DataFrame' object has no attribute 'withColunm'

提示 Gemini:“导致此错误的原因是什么?如何修复此错误?”

了解零上下文的局限性

Gemini Web 界面响应迅速。它会正确识别拼写错误,并建议将 withColunm 替换为 withColumn。

不过,此方法在运行时完全不了解您的实际环境:

  • 无法访问本地文件:网页界面无法检查或修改本地 broken_spark_job.py 文件。您必须手动找到该文件,前往相应行,应用更正,然后保存。
  • 无法实时感知运行时:Web 界面无法查询实时无服务器 Spark 批量作业的状态,也无法提取实时执行日志。
  • 无法查看基础设施:网页界面无法验证您的 Cloud Storage 路径、IAM 权限或 Spark 作业配置。

如果您的脚本因出现常规 SparkException 而失败,网页界面只能提供可能不适用于您环境的宽泛建议。

5. 使用 Google Antigravity CLI 检查和修复代码

为了弥合代码与 AI 辅助功能之间的差距,请使用直接在终端中运行的助理:Google Antigravity CLI。

介绍 Google Antigravity CLI

Google Antigravity CLI (agy) 是一款以终端为先的智能体编码助理。它将 AI 集成到命令行中,让您无需离开开发环境即可运行查询、分析本地代码和执行任务。

如需了解详情,请参阅 Google Antigravity CLI 文档。

安装并配置 CLI

安装 Google Antigravity CLI (agy) 或验证它是否已安装在您的终端中:

agy --version

确保在环境中设置了有效项目 ID:

export GOOGLE_CLOUD_PROJECT="$PROJECT_ID"

配置 CLI 工具权限

默认情况下,Google Antigravity CLI 以安全模式运行,在读取或修改文件之前会提示用户确认。在非交互式打印模式 (-p) 下调用 agy 时,需要确认的工具会自动被软拒绝,除非配置了自动批准。

将 settings.json 配置为将 toolPermission 设置为 always-proceed,并允许列入许可名单的文件操作,以便代理可以检查和修复工作区中的脚本:

mkdir -p ~/.gemini/antigravity-cli
cat << 'EOF' | tee ~/.gemini/antigravity-cli/settings.json ~/.gemini/jetski/cli/settings.json ~/.gemini/antigravity/settings.json ~/.gemini/settings.json
{
  "toolPermission": "always-proceed",
  "permissions": {
    "allow": [
      "read_file(*)",
      "write_file(*)",
      "mcp(*)"
    ]
  }
}
EOF

让 Gemini 使用 Google Antigravity CLI 修复本地脚本

与只能在聊天框中提供建议的基于 Web 的 LLM 不同,Google Antigravity CLI (agy) 具有本地文件级上下文,可以直接在您的工作区中进行更改。指示 Gemini 检查 broken_spark_job.py 并修正 DataFrame 方法的拼写错误:

agy -p "examine broken_spark_job.py, fix the DataFrame method typo, and save the file"

Gemini 扫描 broken_spark_job.py,在第 26 行找到拼错的 withColunm 方法,然后直接将修复应用到该文件:

The DataFrame method typo in [broken_spark_job.py](file:///home/$USER/broken_spark_job.py#L26) has been verified and fixed:

- **File**: [broken_spark_job.py](file:///home/$USER/broken_spark_job.py#L26)
- **Method**: Corrected typo to [`DataFrame.withColumn(...)`](file:///home/$USER/broken_spark_job.py#L26) (`df.withColumn("status", lit("active"))`).
- **Status**: Saved and syntax verified.

确认修复已应用于您的文件:

grep -n "withColumn" broken_spark_job.py

了解为何仅有本地背景信息是不够的

现在,语法拼写错误已解决,请第三次将作业提交到 Managed Service for Apache Spark:

gcloud dataproc batches submit pyspark broken_spark_job.py \
    --region=$REGION \
    --version=2.3 \
    --deps-bucket=gs://$BUCKET_NAME

查看批次的状态。作业仍然失败!

这次的错误不是语法或架构拼写错误。脚本尝试从 Cloud Storage 存储分区中读取内容,但因 gs://my-missing-bucket/input.csv 不存在而失败。

虽然本地文件上下文功能强大,但也有局限性:

  • CLI 可以看到您的本地代码,但看不到您的 Cloud Storage 存储分区配置。
  • 它无法确定您的 Managed Service for Apache Spark 服务账号是否拥有必要的权限。
  • 如果没有外部工具,它就无法检查实时 Spark 批处理配置或运行时日志。

如需解决基础架构级问题,请将 CLI 连接到实时 Spark 环境,以便进行全上下文调试。

6. 使用 Spark MCP 调试实时批处理

在此步骤中,您将使用 Model Context Protocol (MCP) 将 Google Antigravity CLI 连接到 Managed Service for Apache Spark API。通过这种实时连接,模型可以检查活跃的批处理作业、检索日志并动态解决问题。

什么是 Model Context Protocol?

Model Context Protocol (MCP) 是一种开放标准,可让大语言模型 (LLM) 安全地与外部资源进行交互。MCP 定义了一种用于公开数据和工具的统一协议,而不是硬编码自定义 API 集成。通过将 Google Antigravity CLI 连接到 MCP 服务器,您可以为模型提供对实时环境、数据库和 Cloud API 的安全访问权限。

了解 Spark MCP 服务器

Google 提供了一个远程 Spark MCP 服务器,可作为 Managed Service for Apache Spark 环境的安全网关。此服务器将标准 API 公开为 Google Antigravity CLI 可调用的工具集:

  • 端点网址:https://dataproc-$REGION.googleapis.com/mcp
  • 传输类型:HTTP
  • 可用工具:list_batches、get_batch、list_jobs、get_job、get_cluster 等。

默认注册 Spark MCP 服务器

Google Antigravity CLI 默认从 ~/.gemini/config/mcp_config.json 加载全局 MCP 服务器。如需将 CLI 连接到远程 Spark MCP 服务器,请运行以下命令,以在默认配置中注册该服务器:

mkdir -p ~/.gemini/config
cat << EOF | tee ~/.gemini/config/mcp_config.json
{
  "mcpServers": {
    "spark": {
      "serverUrl": "https://dataproc-${REGION}.googleapis.com/mcp"
    }
  }
}
EOF

此命令会在 https://dataproc-$REGION.googleapis.com/mcp 注册远程 Spark MCP 服务器,使 Google Antigravity CLI 能够在您运行任务时默认自动发现和调用 Spark API 工具。

让 Gemini 诊断批次并应用修复

现在,您的 CLI 已连接到 Spark MCP 服务器,请让 Gemini 通过 MCP 检查实时批处理失败情况,诊断根本原因,并将修复直接应用到您的 PySpark 脚本。此过程最多可能需要 10 分钟,如果因超时而失败,请重复执行该命令。

agy -p "inspect my latest failed Spark batch via MCP, identify the root cause, and fix broken_spark_job.py so it succeeds"

在后台,Gemini 和 Google Antigravity CLI 会执行完整的诊断和修复流程:

  1. 发现实时工具:Google Antigravity CLI 在远程 Spark MCP 服务器上调用 list_batches 以获取最近的作业。
  2. 检查运行时详情:CLI 会检索最近失败的批次 ID,并调用 get_batch 来提取其详细状态和 Cloud Logging 错误轨迹。
  3. 解析错误:CLI 从返回的批处理状态中提取 java.io.FileNotFoundException 异常消息。
  4. 交叉引用代码:模型读取本地 broken_spark_job.py 文件,并识别出第 30 行尝试从 gs://my-missing-bucket/input.csv 读取数据。
  5. 应用修复:Gemini 不会让您手动编辑或猜测,而是会更新 broken_spark_job.py 以移除不存在的 Cloud Storage 路径,并确保通过 spark.stop() 干净地释放 JVM 资源。

几分钟后,您应该会看到类似以下内容:

I am locating `broken_spark_job.py` and checking your GCP Dataproc environment configuration. I will proceed as soon as the background search finishes.
### 1. Spark Batch Inspection via MCP

Using the Dataproc MCP tools (`list_batches`, `get_batch`, `analyze_batch_service`, and `get_operation`) against project `spark-test-0831` in `us-central1`, the batch history was inspected:

| Batch ID | State | Created Time | Failure Message / Summary |
| :--- | :--- | :--- | :--- |
| `7d126c18d180466cae0c7ab0a5325616` | **`FAILED`** | `2026-08-31T18:44:07Z` | `AnalysisException: [PATH_NOT_FOUND] Path does not exist: gs://my-missing-bucket/input.csv.` |
| `61d7fda310724883aeeda53fcc261817` | **`FAILED`** | `2026-08-31T18:09:39Z` | `AttributeError: 'DataFrame' object has no attribute 'withColunm'. Did you mean: 'withColumn'?` |
| `188e204baa3d487a98204068ef26a1b9` | **`FAILED`** | `2026-08-31T18:00:15Z` | `NameError: name 'user_schema' is not defined` |

---

### 2. Root Cause Analysis

* **Latest Failed Batch (`7d126c18d180466cae0c7ab0a5325616`)**:
  * **Error**: `AnalysisException: [PATH_NOT_FOUND]`
  * **Root Cause**: The job attempted to read from an invalid/non-existent Cloud Storage path (`gs://my-missing-bucket/input.csv`).
* **Prior Failures**:
  * `61d7fda310724883aeeda53fcc261817`: Typo in method name `withColunm` instead of `withColumn`.
  * `188e204baa3d487a98204068ef26a1b9`: Undefined variable reference `schema=user_schema` instead of `schema=schema`.

---

### 3. Verified Fix in [broken_spark_job.py](file:///home/bmiro/broken_spark_job.py)

The script at [broken_spark_job.py](file:///home/bmiro/broken_spark_job.py) has been resolved:

'''python
import sys
from pyspark.sql import SparkSession
from pyspark.sql.types import StructType, StructField, StringType, IntegerType
from pyspark.sql.functions import lit

def main():
    # Initialize the Spark Session
    spark = SparkSession.builder \
        .appName("BrokenSparkJob") \
        .getOrCreate()

    print("Spark session started successfully!")

    # Define schema and inline DataFrame data
    schema = StructType([
        StructField("name", StringType(), True),
        StructField("age", IntegerType(), True)
    ])

    data = [("Alice", 30), ("Bob", 25)]
    df = spark.createDataFrame(data, schema=schema)
    df_with_status = df.withColumn("status", lit("active"))
    df_with_status.show()

    spark.stop()

if __name__ == "__main__":
    main()
'''

#### Key Corrections:
1. **Removed Non-Existent Path Reference**: Eliminated the call reading `gs://my-missing-bucket/input.csv`.
2. **Fixed Method Name Typo**: Corrected `withColunm` to `withColumn`.
3. **Defined Schema Correctly**: Explicitly initialized `schema` as `StructType` and passed `schema=schema` to `createDataFrame`.

提交修复后的作业

将修复后的 PySpark 脚本提交到 Managed Service for Apache Spark:

gcloud dataproc batches submit pyspark broken_spark_job.py \
    --region=$REGION \
    --version=2.3 \
    --deps-bucket=gs://$BUCKET_NAME

几分钟后,作业应该会成功完成!

Spark session started successfully!
+-----+---+------+
| name|age|status|
+-----+---+------+
|Alice| 30|active|
|  Bob| 25|active|
+-----+---+------+

Batch [e14c5599a9bb4e8f9f53faab0ade0507] finished.

7. 利用智能体技能自动排查问题

在此步骤中,将问题排查工作流打包到可重用的代理技能中。这样,团队中的任何成员都可以通过单个命令运行复杂的诊断工作流。

什么是智能体技能?

智能体技能是模块化目录,其中包含指令、脚本和上下文文件(至少包含一个 SKILL.md 文件),用于指导 AI 智能体如何处理特定的重复性运营任务。通过在本地保存这些指令,您可以为 Google Antigravity CLI 提供用于诊断、自动化和修复的编纂好的剧本。

创建自定义技能

在 .agents/skills/spark-troubleshooter/ 中为技能创建一个专用目录,并写入 SKILL.md 文件:

mkdir -p .agents/skills/spark-troubleshooter
cat << 'EOF' > .agents/skills/spark-troubleshooter/SKILL.md
---
name: spark-troubleshooter
description: Diagnoses failed Apache Spark batches on Managed Service for Apache Spark, inspects live batch logs via the Spark MCP server, and recommends resolution commands. Use when troubleshooting Spark job failures.
---

# Spark Troubleshooter Skill

This skill diagnoses failed Apache Spark batches on Managed Service for Apache Spark and offers rapid solutions.

## Instructions
1. **Verify local syntax**: Locate the PySpark script in the current directory and check for compilation or syntax issues.
2. **Fetch live batch state**: Call the Spark MCP server tool `list_batches` and inspect the status of the most recent batch.
3. **Check JVM and PySpark logs**: Look for standard Spark exceptions, such as `FileNotFoundException`, `AnalysisException`, or out-of-memory errors in the batch logs.
4. **Recommend action**:
    * If a Cloud Storage path is invalid, recommend the exact `gcloud storage buckets create` command or update the script path.
    * If the job fails due to configuration, generate the correct `gcloud dataproc batches submit` command with the appropriate parameters.
    * If a syntax error is detected, fix the code in-place.
    * For other errors, recommend a fix.
EOF

调用自定义技能

由于技能位于 .agents/skills/ 中,因此 Google Antigravity CLI 会自动发现该技能。运行 CLI 并提供可触发问题排查技能的提示:

agy -p "Diagnose why my last Spark batch failed and recommend a fix"

Google Antigravity CLI 会自动执行技能中定义的 playbook。它会检查您的本地代码,使用 MCP 服务器查询实时 Spark API,并输出分步问题解决方案:

[Spark Troubleshooter] Running diagnostic playbook...
- Local Syntax: OK (broken_spark_job.py has valid python syntax)
- Spark Batch Status: FAILED (batch-928f1)
- Log Exception: java.io.FileNotFoundException for gs://my-missing-bucket/input.csv

Recommendation:
The bucket gs://my-missing-bucket does not exist. Run this command to create it:

  gcloud storage buckets create gs://my-missing-bucket --location=$REGION

Once created, submit the batch again with:

  gcloud dataproc batches submit pyspark broken_spark_job.py \
      --region=$REGION \
      --deps-bucket=gs://$BUCKET_NAME

8. 在 Cloud 控制台中利用 Gemini 排查问题

虽然 Google Antigravity CLI 和代理技能可为以终端为中心的工作流和自动化工作流提供支持,但 Google Cloud 还直接在 Google Cloud 控制台和 Cloud Logging 中为偏好基于 Web 的界面的开发者和运维人员提供了强大的原生 AI 问题排查工具。

在 Cloud Logging 中使用 Gemini 说明日志

当无服务器 Spark 批处理作业失败时,其 stdout、stderr 和 JVM 异常轨迹会流式传输到 Cloud Logging。您无需手动将堆栈轨迹复制到外部浏览器窗口中,而是可以使用 Gemini 就地分析错误:

  1. 在浏览器中打开 Managed Service for Apache Spark 批次控制台。
  2. 在列表中点击失败的批次(尝试从 gs://my-missing-bucket 读取时失败的运行),然后向下滚动到作业日志。
  3. 找到包含 AnalysisException: [PATH_NOT_FOUND] Path does not exist: gs://my-missing-bucket/input.csv(或 FileNotFoundException)的错误日志。
  4. 点击相应日志行即可展开显示其详细信息。
  5. 点击调查日志(或 Gemini 星光图标 ✨)。

Gemini 会打开一个说明窗格,其中包含以下内容:

  • 解码异常:说明在访问 Cloud Storage 时,Apache Spark 中的 AnalysisException 和 PATH_NOT_FOUND 分别表示什么。
  • 指明缺失的资源:突出显示 Cloud Storage 存储分区或路径 gs://my-missing-bucket/input.csv 不存在。
  • 提供情境感知建议:建议您创建具有 gcloud storage buckets create 的存储分区或更新脚本中的文件路径,而无需切换窗口。

在 Google Cloud 控制台中查询 Gemini

您还可以使用 Google Cloud 控制台中的 Gemini 侧边栏进行对话式问题排查和架构查询:

  1. 点击 Google Cloud 控制台右上角导航栏中的 Gemini 图标。
  2. 在对话提示中,提出以下问题:
    Why did my latest serverless Spark batch fail, and how do I prevent out-of-memory errors in PySpark?
    
  3. Gemini 会根据项目的有效服务和资源配额提供量身定制的指导。

了解控制台原生 AI 的优势和局限性

Google Cloud 控制台中的 Gemini 可为控制台用户带来显著优势:

  • 集成式上下文:它会自动访问日志载荷、Google Cloud 错误元数据和服务遥测数据。
  • 无需手动转移:您无需在各个标签页之间清理或复制粘贴敏感的错误轨迹。

将控制台原生智能与基于终端的智能体自动化相结合,可让您的团队在界面和 CLI 工作流中实现端到端运营覆盖。

9. 清理资源

为避免系统向您的 Google Cloud 结算账号持续收取费用,请清理本实验期间创建的所有资源。

删除 Cloud Storage 存储桶

删除 Cloud Storage 存储分区会移除所有已存储的 PySpark 作业文件、执行日志和脚本依赖项。在终端中运行以下命令:

gcloud storage rm -r gs://$BUCKET_NAME

清理无服务器 Spark 批量日志

由于 Managed Service for Apache Spark 是无状态的,因此您只需为批处理的有效执行时间付费。批处理元数据和执行日志会保留在 Cloud Logging 中。如果您想清理这些日志,可以在 Cloud Logging 中删除日志条目或调整保留政策。

10. 恭喜

恭喜您完成此 Codelab!您已成功从传统的手动日志诊断过渡到现代的 AI 驱动型问题排查工作流。

所学内容

在本实验中,您体验了全面的 AI 问题排查流程:

  1. 您通过手动翻阅原始日志这种老式方法诊断了失败的作业。
  2. 您已了解为何将错误复制并粘贴到网络浏览器中会失败,因为模型不了解您的项目环境。
  3. 您使用 Google Antigravity CLI 自动扫描了本地 Python 代码,并就地修正了语法拼写错误。
  4. 您已将 CLI 连接到远程 Apache Spark Model Context Protocol (MCP) 服务器,从而允许 Google Antigravity CLI 查询实时批处理状态和执行错误。
  5. 您将此诊断清单打包到自定义的可重复使用的 Spark 故障排除技能中,以便进行即时自动健康检查。
  6. 您使用了 Cloud Logging 中的解释此日志条目和 Cloud 控制台中的 Gemini 等原生 Google Cloud AI 功能,通过实时日志遥测数据诊断错误。

此进展情况表明,增加 AI 智能体可用的上下文信息可使复杂的云数据操作更快、更安全、更可靠。

了解详情

如需深入了解本实验中使用的技术,请查看以下资源: