# 使用指南 | GreptimeDB 数据如何写入 AWS S3

Source: https://greptime.cn/blogs/2023-10-17-aws

## 下载 GreptimeDB

首先，你需要下载一个 GreptimeDB 实例，可以根据你的运行平台从 [Greptime 资源页面](https://greptime.cn/resources) 下载打包好的二进制文件，也可以使用 docker 镜像。

<figure>
<img src="/zh/blogs/2023-10-17-aws/image1.png" alt="(图 1：下载 GreptimeDB 实例)" style="width: 80%; margin: 0 auto"/>
<figcaption>(图 1：下载 GreptimeDB 实例)</figcaption>
</figure>

更多安装方式请参考[安装文档](https://docs.greptime.cn/getting-started/try-out-greptimedb)。

本文将以我们提供的安装脚本来演示。首先打开命令行，进入一个临时工作目录，执行下列脚本将会自动下载并安装一个 greptime 实例到本地：

```bash
curl -fsSL \
  https://raw.githubusercontent.com/greptimeteam/greptimedb/develop/scripts/install.sh | sh
```

这里下载的是 nightly 构建版本，仅用于测试，如果需要生产稳定运行，还请下载正式发布的版本（截止本文编写时间是 v0.4.0）。

如果一切顺利的话，你应该可以看到类似下列的输出：

```sql
x greptime-darwin-arm64-v0.4.0-nightly-20231002/
x greptime-darwin-arm64-v0.4.0-nightly-20231002/greptime
Run './greptime --help' to get started
```

在当前目录可以看到 `greptime` 这个二进制文件：

```bash
-rwxr-xr-x  1 dennis  staff   134M Oct  2 10:08 greptime
```

执行 `./greptime --help` 命令，查看帮助信息：

```sql
greptimedb
branch: develop
commit: 201acd152db2a961d804b367e23d2224b4397de8
dirty: false
version: 0.4.0-nightly

USAGE:
    greptime [OPTIONS] <SUBCOMMAND>

OPTIONS:
    -h, --help                     Print help information
        --log-dir <LOG_DIR>
        --log-level <LOG_LEVEL>
    -V, --version                  Print version information

SUBCOMMANDS:
    cli
    datanode
    frontend
    help          Print this message or the help of the given subcommand(s)
    metasrv
    standalone
```

## 配置 S3 信息

假设你已经创建了一个 AWS S3 的 bucket ，并且已经有对应的 Access Key ID/Secret 等鉴权信息，接下来就进入配置环节。  

首先，可以从我们的 GitHub 仓库下载 standalone 单机模式的样例配置文件 [standalone.example.toml](https://github.com/GreptimeTeam/greptimedb/blob/develop/config/standalone.example.toml)，假设我们保存为 `config.toml`：

```shell
curl https://raw.githubusercontent.com/GreptimeTeam/greptimedb/develop/config/standalone.example.toml > config.toml
```

接下来编辑 `config.toml`，重点是其中的 `[storage]` 部分，样例展示如下：

```toml
[storage]
# The working home directory.
data_home = "/tmp/greptimedb/"
# Storage type.
type = "File"
# TTL for all tables. Disabled by default.
# global_ttl = "7d"
# Cache configuration for object storage such as 'S3' etc.
# cache_path = "/path/local_cache"
# The local file cache capacity in bytes.
# cache_capacity = "256Mib"
```

配置本地磁盘模式，数据库根目录设置为 `/tmp/greptimedb/`（如果是生产环境，请修改为非临时目录）。

参照[配置文档](https://docs.greptime.cn/user-guide/operations/configuration)，可以修改成将数据保存在 AWS S3 上：

```toml
[storage]
# The working home directory.
data_home = "/tmp/greptimedb/"
# Storage type.
type = "S3"
bucket = "greptimedb-test"
root = "/s3test"
region = "ap-southeast-1"
access_key_id = "***********"
secret_access_key = "*********************"
# endpoint = ""
```

这里有一些关键参数，可以根据你的具体使用替换成你的 S3 bucket 信息：

- `bucket`：创建的 bucket 名称；
    
- `root`：数据库数据的存储目录，这里设定为 `/s3test`，相对于 bucket 的根目录开始；
    
- `access_key_id`: S3 的访问 Access key；
    
- `secret_access_key`: S3 的访问 Secret key；
    
- 必要的话设置 `region` 或者 `endpoint`，我们测试的 `bucket` 创建在东南亚，所以 `region` 设置为 `ap-southeast-1`。

## 启动并测试

正确配置之后，就可以启动测试了。执行下列命令启动单机版 GreptimeDB：

```shell
./greptime standalone start -c config.toml
```

我们通过 `-c` 选项指定了配置文件。如果正常启动可以在最后看到类似如下的日志：

```sql
2023-10-11T07:33:34.832355Z  INFO frontend::server: Starting HTTP_SERVER at 127.0.0.1:4000
2023-10-11T07:33:34.855511Z  INFO servers::http: Enable dashboard service at '/dashboard'
2023-10-11T07:33:34.856408Z  INFO servers::http: HTTP server is bound to 127.0.0.1:4000
2023-10-11T07:33:34.856742Z  INFO frontend::server: Starting GRPC_SERVER at 127.0.0.1:4001
2023-10-11T07:33:34.857168Z  INFO servers::grpc: gRPC server is bound to 127.0.0.1:4001
2023-10-11T07:33:34.859433Z  INFO frontend::server: Starting MYSQL_SERVER at 127.0.0.1:4002
2023-10-11T07:33:34.859453Z  INFO servers::server: MySQL server started at 127.0.0.1:4002
2023-10-11T07:33:34.859465Z  INFO frontend::server: Starting OPENTSDB_SERVER at 127.0.0.1:4242
2023-10-11T07:33:34.859485Z  INFO servers::server: OpenTSDB server started at 127.0.0.1:4242
2023-10-11T07:33:34.859490Z  INFO frontend::server: Starting POSTGRES_SERVER at 127.0.0.1:4003
2023-10-11T07:33:34.859500Z  INFO servers::server: Postgres server started at 127.0.0.1:4003
```

GreptimeDB 支持各类协议端口将正常绑定并监听。我们可以通过浏览器地址 http://localhost:4000/dashboard/  访问 GreptimeDB 自身携带的 Dashboard，如下所示：  

<figure>
<img src="/zh/blogs/2023-10-17-aws/image2.png" alt="(图 2：访问 GreptimeDB Dashboard)" style="width: 80%; margin: 0 auto"/>
<figcaption>(图 2：访问 GreptimeDB Dashboard)</figcaption>
</figure>

也可以在这里执行 SQL/Python/PromQL 等查询语句。

接下来我们尝试创建表，再写入数据并查询。你可以直接在 dashboard 里窗口执行这些 SQL 语句：

```sql
CREATE TABLE IF NOT EXISTS system_metrics (
    host STRING,
    idc STRING,
    cpu_util DOUBLE,
    memory_util DOUBLE,
    disk_util DOUBLE,
    ts TIMESTAMP DEFAULT CURRENT_TIMESTAMP,
    PRIMARY KEY(host, idc),
    TIME INDEX(ts)
);

INSERT INTO system_metrics
VALUES
    ("host1", "idc_a", 11.8, 10.3, 10.3, 1667446797450),
    ("host1", "idc_a", 80.1, 70.3, 90.0, 1667446797550),
    ("host1", "idc_b", 50.0, 66.7, 40.6, 1667446797650),
    ("host1", "idc_b", 51.0, 66.5, 39.6, 1667446797750),
    ("host1", "idc_b", 52.0, 66.9, 70.6, 1667446797850),
    ("host1", "idc_b", 53.0, 63.0, 50.6, 1667446797950),
    ("host1", "idc_b", 78.0, 66.7, 20.6, 1667446798050),
    ("host1", "idc_b", 68.0, 63.9, 50.6, 1667446798150),
    ("host1", "idc_b", 90.0, 39.9, 60.6, 1667446798250);
    
SELECT * FROM system_metrics;    
```

一切正常的话，你将可以看到查询的结果，示例如下：

<figure>
<img src="/zh/blogs/2023-10-17-aws/image3.png" alt="(图 3：查询结果)" style="width: 80%; margin: 0 auto"/>
<figcaption>(图 3：查询结果)</figcaption>
</figure>

我们可以登录 AWS S3 的控制台查看写入的数据：

<figure>
<img src="/zh/blogs/2023-10-17-aws/image4.png" alt="(图 4：查看写入数据)" style="width: 80%; margin: 0 auto"/>
<figcaption>(图 4：查看写入数据)</figcaption>
</figure>

为了提升查询性能，建议将本地文件缓存打开：

```toml
[storage]

cache_path = "/path/local_cache"
cache_capacity = "256MiB"
```

`cache_path` 指定本地缓存的目录，`cache_capacity` 指定本地缓存使用的最大大小。 

## 关于 Greptime

[Greptime 格睿科技](https://greptime.cn/)专注于为可观测、物联网及车联网等领域提供实时、高效的数据存储和分析服务，帮助客户挖掘数据的深层价值。目前基于云原生的时序数据库 [GreptimeDB](https://github.com/GreptimeTeam/greptimedb) 已经衍生出多款适合不同用户的解决方案，更多信息或 demo 展示请联系下方小助手（微信号：greptime）。

欢迎对开源感兴趣的朋友们参与贡献和讨论，从带有 good first issue 标签的 issue 开始你的开源之旅吧～期待在开源社群里遇见你！添加小助手微信即可加入“技术交流群”与志同道合的朋友们面对面交流哦~

Star us on GitHub Now: https://github.com/GreptimeTeam/greptimedb

官网：https://greptime.cn/

文档：https://docs.greptime.cn/

Twitter: https://twitter.com/Greptime

Slack: https://greptime.com/slack

LinkedIn: https://www.linkedin.com/company/greptime/
