# Spark简介

## Spark 是什么

Apache Spark 是一个快速、通用的分布式计算引擎，支持批处理、流处理、机器学习和图计算。

**核心特点**：

* **速度快**：基于内存计算，比 MapReduce 快 100 倍
* **易用性**：支持 Java、Scala、Python、R 等语言
* **通用性**：统一引擎支持多种工作负载
* **兼容性**：可运行在 Hadoop、Mesos、Kubernetes 等集群管理器上

## Spark 的架构

```
┌─────────────────────────────────────────────┐
│           Spark Applications                   │
│  ┌──────────┐  ┌──────────┐  ┌──────────┐  │
│  │ Spark SQL │ │ Spark    │ │ MLlib    │  │
│  │          │ │ Streaming│ │ GraphX   │  │
│  └──────────┘  └──────────┘  └──────────┘  │
├─────────────────────────────────────────────┤
│              Spark Core                      │
│         ┌──────────────┐                     │
│         │    RDD API    │                     │
│         └──────────────┘                     │
├─────────────────────────────────────────────┤
│           Cluster Managers                   │
│  ┌──────────┐  ┌──────────┐  ┌──────────┐  │
│  │ Standalone│ │   YARN   │ │  K8s     │  │
│  └──────────┘  └──────────┘  └──────────┘  │
└─────────────────────────────────────────────┘
```

## Spark 的核心组件

### 1. Spark Core

基础执行引擎，提供：

* 内存任务调度
* 故障恢复
* 与存储系统交互
* RDD API

### 2. Spark SQL

结构化数据处理：

* DataFrame 和 Dataset API
* SQL 查询
* 支持多种数据源

### 3. Spark Streaming

微批流处理：

* 实时数据处理
* 与 Kafka、Flume 等集成
* 容错保证

### 4. MLlib

机器学习库：

* 分类、回归、聚类
* 特征提取
* 模型评估

### 5. GraphX

图计算：

* 图并行计算
* 图算法
* Pregel API

## Spark vs MapReduce

| 特性   | MapReduce    | Spark      |
| ---- | ------------ | ---------- |
| 计算模式 | 磁盘 IO        | 内存计算       |
| 延迟   | 分钟级          | 毫秒-秒级      |
| 迭代计算 | 每次写磁盘        | 内存保留       |
| 编程模型 | Map + Reduce | 丰富算子       |
| 容错   | 重新执行         | Lineage 重建 |

## 总结

Spark 是统一的大数据处理引擎，通过内存计算和丰富的算子，提供了比 MapReduce 更好的性能和易用性。


---

# Agent Instructions: Querying This Documentation

If you need additional information that is not directly available in this page, you can query the documentation dynamically by asking a question.

Perform an HTTP GET request on the current page URL with the `ask` query parameter:

```
GET https://qiangrens-organization.gitbook.io/qkd90/11spark-da-shu-ju-yin-qing/spark-jian-jie.md?ask=<question>
```

The question should be specific, self-contained, and written in natural language.
The response will contain a direct answer to the question and relevant excerpts and sources from the documentation.

Use this mechanism when the answer is not explicitly present in the current page, you need clarification or additional context, or you want to retrieve related documentation sections.
