# 数据处理

本文档介绍如何使用 Alluxio 加速数据处理。

在传统的数据处理管道中，Spark、Flink 或 MapReduce 等引擎通常从存储系统读取数据，并将结果写回分布式存储（如 S3 或 HDFS）以进行持久化或下游消费。然而，这些分布式存储系统的性能瓶颈，如有限的吞吐量或高延迟，常常导致处理时间过长，降低了整个管道的效率。

为了应对这一挑战，Alluxio 提供了一个高性能、易于集成的数据加速解决方案。该解决方案可与现有计算引擎和用户应用程序无缝集成，无需任何代码修改，显著提高 I/O 性能，并加速整个数据处理工作流。

## 架构概述

该加速解决方案的整体工作流程如下图所示：

<figure><img src="/files/omTNf6mxnwsWMbgBvYsW" alt=""><figcaption></figcaption></figure>

* 原始数据存储在 HDFS 或 S3 中。Alluxio 充当缓存代理，允许计算引擎直接从缓存中读取数据，避免重复访问底层存储。
* 处理过程中生成的中间或临时文件直接写入 Alluxio 的 `CACHE_ONLY` 层，以加快写入速度并提高中间数据效率。
* 存储在 Alluxio 中的临时文件可由下游作业直接读取，从而提高读取性能并加快管道切换速度。
* 最终结果文件也首先写入 Alluxio。启用异步持久化后，这些文件将在后台上传到持久化存储系统（如 S3 或 HDFS），以确保数据持久性。

在整个管道中，Alluxio 处理所有数据读/写 I/O 操作，显著减少了底层存储系统（UFS）的负载，并实现了端到端的加速。

## 所需功能

该解决方案依赖于几个核心的 Alluxio 功能。建议在部署前熟悉这些功能：

* CACHE\_ONLY：仅将文件 I/O 限制在 Alluxio，避免与 UFS 交互，并实现高性能缓存。
* 异步持久化：支持将最终结果文件异步上传到持久化存储系统，如 S3 或 HDFS。
* [客户端路径映射](/ee-ai-cn/ai-3.7/data-access/client-virtual-path-mapping.md)：将原始文件路径透明地映射到 Alluxio 路径，无需修改应用程序代码即可实现无缝集成。

## 示例：加速 Hive 表

假设我们有以下 Hive 表：

```sql
CREATE TABLE `employee_orc_s3`(
  `name` string,
  `salary` int,
  `deptno` int,
  `doj` date)
ROW FORMAT SERDE
  'org.apache.hadoop.hive.ql.io.orc.OrcSerde'
STORED AS INPUTFORMAT
  'org.apache.hadoop.hive.ql.io.orc.OrcInputFormat'
OUTPUTFORMAT
  'org.apache.hadoop.hive.ql.io.orc.OrcOutputFormat'
LOCATION
  's3a://test_bucket/user/hive/warehouse/employee_orc_s3'
```

我们的目标是实现以下几点：

1. 通过 Alluxio 访问和操作 Hive 表，而无需修改其 LOCATION；
2. 确保中间文件仅驻留在 Alluxio 中，不持久化到 S3；
3. 将最终输出文件从 Alluxio 异步持久化到 S3，以确保数据持久性。

### Alluxio 配置

```properties
# Client path mapping
alluxio.user.virtual.path.mapping.enabled=true
alluxio.user.virtual.path.mapping.ufs.mapping.enabled=true
alluxio.user.virtual.path.mapping.rule.file.path=/opt/alluxio/conf/path_mapping.json
​
# Async upload configuration
alluxio.underfs.gemini.ufs.fallback.enabled=true
```

路径映射配置 `path_mapping.json`：

```json
{
  "rules": [
    {
      "src": "^s3a://test_bucket/user/hive/warehouse/employee_orc_s3(.*)",
      "dst": "gemini://${GEMINI_MASTER_ADDRESS}/user/hive/warehouse/employee_orc_s3{{ var1 }}"
    }
  ]
}
```

此配置表示所有以 `s3a://test_bucket/user/hive/warehouse/employee_orc_s3` 为前缀的路径都将重定向到 Alluxio 的 CACHE\_ONLY 路径 `gemini://${GEMINI_MASTER_ADDRESS}/user/hive/warehouse/employee_orc_s3`。

### CACHE\_ONLY 异步持久化配置

假设有以下挂载点：

```
/s3          ----------> s3a://test_bucket/
/cache_only  ----------> gemini://${GEMINI_MASTER_ADDRESS}/
```

在 CACHE\_ONLY 主节点上启用异步持久化：

```properties
alluxio.gemini.master.async.upload.local.file.path=/opt/alluxio/conf/async_upload.json
```

异步上传配置 `sync_upload.json`：

```json
{
  "cacheOnlyMountPoint": "/cache_only",
  "asyncUploadPathMapping": {
    "/cache_only/user/hive/warehouse/employee_orc_s3": "/s3/user/hive/warehouse/employee_orc_s3"
  },
  "blackList": [
    "_temporary",
    ".spark-staging"
  ]
}
```

此配置表示写入 `gemini://${GEMINI_MASTER_ADDRESS}/user/hive/warehouse/employee_orc_s3` 的文件将异步持久化到相应的 S3 路径 `s3://test_bucket/user/hive/warehouse/employee_orc_s3`。黑名单中列出的路径（如 `_temporary` 和 `.spark-staging`）不进行持久化，因为它们是作业完成后会自动清理的中间文件。

### 客户端应用程序中所需的 Alluxio jars

从 Alluxio 发布包中，需要 3 个 jars：

* `client/alluxio-AI-3.7-13.0.0-client.jar`：Alluxio 客户端 jar，用于连接到 Alluxio 集群；
* `client/ufs/alluxio-underfs-gemini-shaded-AI-3.7-13.0.0.jar`：Alluxio UFS jar，用于连接到 Alluxio CACHE\_ONLY 集群；
* `client/ufs/alluxio-underfs-s3a-v2-shaded-AI-3.7-13.0.0.jar`：Alluxio UFS jar，用于连接到 AWS S3；

### 提交 Spark SQL 作业

如下提交 Spark SQL 作业：

```bash
bin/spark-sql \
  --master spark://localhost:7077 \
  --deploy-mode client \
  --conf spark.executor.memory=1g \
  --conf spark.driver.memory=1g \
  --conf spark.hadoop.fs.s3a.impl=alluxio.hadoop.FileSystem \
  --conf spark.hadoop.hive.metastore.uris=thrift://localhost:9083 \
  --conf spark.hadoop.hive.metastore.client.factory.class=com.amazonaws.glue.catalog.metastore.AWSGlueDataCatalogHiveClientFactory \
  --jars alluxio-AI-3.7-13.0.0--client.jar,alluxio-underfs-s3a-v2-shaded-AI-3.7-13.0.0-.jar,alluxio-underfs-gemini-shaded-AI-3.7-13.0.0-.jar
```

关键配置：

* `spark.hadoop.fs.s3a.impl=alluxio.hadoop.FileSystem`：将 `s3a://` 路径重定向到 Alluxio；
* `--jars`：指定所需的客户端和 shaded UFS jars。

启动 Spark SQL 后，可以运行：

```sql
insert into employee_orc_s3 values('jack', 15000, 222, date '2025-06-23');
select * from employee_orc_s3;
```

### 验证缓存和异步持久化

写入数据后，验证 Alluxio 的 CACHE\_ONLY 层中的文件：

```bash
bin/alluxio fs ls /cache_only/user/hive/warehouse/employee_orc_s3
```

异步持久化完成后，验证 S3 中的文件：

```bash
bin/alluxio fs ls /s3/user/hive/warehouse/employee_orc_s3
```

### 可选：直接从 S3 读取

数据持久化后，您可以选择直接从 S3 访问 Hive 表，而无需使用 Alluxio：

```bash
bin/spark-sql \
  --master spark://localhost:7077 \
  --deploy-mode client \
  --conf spark.executor.memory=1g \
  --conf spark.driver.memory=1g \
  --conf spark.hadoop.hive.metastore.uris=thrift://localhost:9083 \
  --conf spark.hadoop.hive.metastore.client.factory.class=com.amazonaws.glue.catalog.metastore.AWSGlueDataCatalogHiveClientFactory
```

## 释放 CACHE\_ONLY 缓存空间

文件成功持久化到 UFS 后，您可以使用以下方法之一释放 Alluxio 的 CACHE\_ONLY 层中的缓存空间：

### 使用 `rm` 删除文件

此方法会删除数据和元数据。文件将从 CACHE\_ONLY 中完全删除，未来的访问必须直接访问 UFS。

### 使用 `free` 释放缓存

此方法仅清除缓存的数据，但保留元数据：

```bash
$ALLUXIO_HOME/gemini/bin/alluxio fs free $PATH
```

文件仍可通过 Alluxio 路径访问，但下一次访问将触发从 UFS 重新加载到 CACHE\_ONLY。


---

# 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://documentation.alluxio.io/ee-ai-cn/ai-3.7/data-access/performance/data-processing.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.
