> ## Documentation Index
> Fetch the complete documentation index at: /documents/llms.txt
> Use this file to discover all available pages before exploring further.

# 基于 Apache Iceberg 的数据湖分析

> 基于 Docker 创建的 Iceberg 数据湖分析

# Apache Iceberg Lakehouse

本指南将帮助您使用 数据库™ 快速上手 Apache Iceberg™，包括一些示例代码以突出其强大功能。

### Docker-Compose

最快的入门方式是使用一个 docker-compose 文件，该文件使用包含已配置 Iceberg catalog 的本地 集群的 `starrocks/fe-ubuntu` 和 `starrocks/be-ubuntu` 镜像。要使用它，您需要安装 Docker CLI。

安装 Docker 后，将下面的 yaml 保存为名为 docker-compose.yml 的文件：

```yml theme={null}
services:

  starrocks-fe:
    image: starrocks/fe-ubuntu:3.3.5
    hostname: starrocks-fe
    container_name: starrocks-fe
    user: root
    command: |
      bash /opt/starrocks/fe/bin/start_fe.sh --host_type FQDN
    ports:
      - 8030:8030
      - 9020:9020
      - 9030:9030
    networks:
      iceberg_net:
    environment:
      - AWS_ACCESS_KEY_ID=admin
      - AWS_SECRET_ACCESS_KEY=password
      - AWS_REGION=us-east-1
    healthcheck:
      test: 'mysql -u root -h starrocks-fe -P 9030 -e "SHOW FRONTENDS\G" |grep "Alive: true"'
      interval: 10s
      timeout: 5s
      retries: 3

  starrocks-be:
    image: starrocks/be-ubuntu:3.3.5
    command:
      - /bin/bash
      - -c
      - |
        ulimit -n 65535;
        echo "# Enable data cache"  >> /opt/starrocks/be/conf/be.conf
        echo "block_cache_enable = true"  >> /opt/starrocks/be/conf/be.conf
        echo "block_cache_mem_size = 536870912" >> /opt/starrocks/be/conf/be.conf
        echo "block_cache_disk_size = 1073741824" >> /opt/starrocks/be/conf/be.conf
        sleep 15s
        mysql --connect-timeout 2 -h starrocks-fe -P 9030 -u root -e "ALTER SYSTEM ADD BACKEND \"starrocks-be:9050\";"
        bash /opt/starrocks/be/bin/start_be.sh
    ports:
      - 8040:8040
    hostname: starrocks-be
    container_name: starrocks-be
    user: root
    depends_on:
      starrocks-fe:
        condition: service_healthy
    healthcheck:
      test: 'mysql -u root -h starrocks-fe -P 9030 -e "SHOW BACKENDS\G" |grep "Alive: true"'
      interval: 10s
      timeout: 5s
      retries: 3
    networks:
      iceberg_net:
    environment:
      - HOST_TYPE=FQDN
      - AWS_EC2_METADATA_DISABLED=true

  rest:
    image: apache/iceberg-rest-fixture
    container_name: iceberg-rest
    networks:
      iceberg_net:
        aliases:
          - iceberg-rest.minio
    ports:
      - 8181:8181
    environment:
      - AWS_ACCESS_KEY_ID=admin
      - AWS_SECRET_ACCESS_KEY=password
      - AWS_REGION=us-east-1
      - CATALOG_WAREHOUSE=s3://warehouse/
      - CATALOG_IO__IMPL=org.apache.iceberg.aws.s3.S3FileIO
      - CATALOG_S3_ENDPOINT=http://minio:9000

  minio:
    image: quay.io/minio/aistor/minio:latest
    container_name: minio
    volumes:
      - ./minio.license:/minio.license:ro
    environment:
      - MINIO_ROOT_USER=admin
      - MINIO_ROOT_PASSWORD=password
      - MINIO_DOMAIN=minio
    networks:
      iceberg_net:
        aliases:
          - warehouse.minio
    ports:
      - 9001:9001
      - 9000:9000
    command: ["server", "/data", "--console-address", ":9001"]
  mc:
    depends_on:
      - minio
    image: quay.io/minio/aistor/mc:latest
    container_name: mc
    networks:
      iceberg_net:
    environment:
      - AWS_ACCESS_KEY_ID=admin
      - AWS_SECRET_ACCESS_KEY=password
      - AWS_REGION=us-east-1
    entrypoint: >
      /bin/sh -c "
      until (/usr/bin/mc alias set minio http://minio:9000 admin password) do echo '...waiting...' && sleep 1; done;
      /usr/bin/mc rm -r --force minio/warehouse;
      /usr/bin/mc mb minio/warehouse;
      /usr/bin/mc anonymous set public minio/warehouse;
      tail -f /dev/null
      "
networks:
  iceberg_net:
```

MinIO AIStor 需要许可证。在启动容器之前，请获取许可证：

MinIO AIStor 需要许可证才能运行。您可以使用现有的许可证，也可以申请新的 MinIO AIStor 免费版（Free Tier）许可证。

要申请新的 MinIO AIStor 免费版许可证，请访问 [MinIO AIStor 定价](https://min.io/pricing)页面，在 Free 版本下选择 **Get Started** 以获取免费许可证。免费版许可证允许您在单个计算资源（包含一个或多个磁盘）上运行 MinIO AIStor。

将许可证文件保存为 `minio.license`，并放在与 `docker-compose.yml` 文件相同的目录中。Docker Compose 文件会将 `minio.license` 挂载到 MinIO AIStor 容器中。如果没有有效的许可证，MinIO AIStor 可以启动，但会拒绝所有 S3 操作。

接下来，使用以下命令启动 docker 容器：

```Plain theme={null}
docker compose up --detach --wait --wait-timeout 400
```

然后，您可以运行以下任一命令来启动 数据库 会话。

```bash theme={null}
docker exec -it starrocks-fe \
mysql -P 9030 -h 127.0.0.1 -u root --prompt="StarRocks > "
```

### 添加和使用 Catalog

```SQL theme={null}
CREATE EXTERNAL CATALOG 'demo'
COMMENT "External catalog to Apache Iceberg on MinIO"
PROPERTIES
(
  "type"="iceberg",
  "iceberg.catalog.type"="rest",
  "iceberg.catalog.uri"="http://iceberg-rest:8181",
  "iceberg.catalog.warehouse"="warehouse",
  "aws.s3.access_key"="admin",
  "aws.s3.secret_key"="password",
  "aws.s3.endpoint"="http://minio:9000",
  "aws.s3.enable_path_style_access"="true"
);
```

```SQL theme={null}
SHOW CATALOGS\G
```

```SQL theme={null}
*************************** 1. row ***************************
Catalog: default_catalog
   Type: Internal
Comment: An internal catalog contains this cluster's self-managed tables.
*************************** 2. row ***************************
Catalog: demo
   Type: Iceberg
Comment: External catalog to Apache Iceberg on MinIO
2 rows in set (0.00 sec)
```

```SQL theme={null}
SET CATALOG demo;
```

### 创建和使用数据库

```SQL theme={null}
CREATE DATABASE nyc;
```

```SQL theme={null}
USE nyc;
```

### 创建表

```SQL theme={null}
CREATE TABLE demo.nyc.taxis
(
    trip_id            bigint,
    trip_distance      float,
    fare_amount double,
    store_and_fwd_flag string,
    vendor_id          bigint
) PARTITION BY (vendor_id);
```

### 写入数据到表

```SQL theme={null}
INSERT INTO demo.nyc.taxis
VALUES (1000371, 1.8, 15.32, 'N', 1),
       (1000372, 2.5, 22.15, 'N', 2),
       (1000373, 0.9, 9.01, 'N', 2),
       (1000374, 8.4, 42.13, 'Y', 1);
```

### 从表中读取数据

```SQL theme={null}
SELECT *
FROM demo.nyc.taxis;
```

### 验证数据是否存储在对象存储中

当您添加并使用 external catalog 时，数据库 开始使用 MinIO 作为 `demo.nyc.taxis` 表的对象存储。如果您导航到 [http://localhost:9001](http://localhost:9001) 然后通过对象浏览器菜单导航到 `warehouse/nyc/taxis/`，您可以确认 数据库 正在使用 MinIO 进行存储。

**Tip**

MinIO 的用户名和密码在 docker-compose.yml 文件中。系统会提示您将密码更改为更好的密码，在本教程中请忽略此建议。

![img](https://mintcdn.com/mirrorship-docs/8FakUMFrFjmoxRt5/documents/images/d6f078d44705f38fe40f105e.png?fit=max\&auto=format\&n=8FakUMFrFjmoxRt5\&q=85\&s=4a05fed4e82008f3adc182e9e3b2b8d2)

### 下一步

#### 将 Iceberg 添加到 数据库

如果您已经有一个 StarRocks 3.2.0 或更高版本的环境，它已包含 Iceberg 1.6.0。无需额外下载或 jar 文件。

#### 了解更多

现在您已经使用 Iceberg 和 数据库 运行起来了，请查看 [StarRocks-Iceberg 文档](/documents/cn/4.2/data_source/catalog/iceberg/iceberg.md) 以了解更多信息！


This documentation is built and hosted on [Mintlify](https://mintlify.com), a developer documentation platform.