# 用户指南 | 快速迁移 InfluxDB v1 数据至 GreptimeDB

Source: https://greptime.cn/blogs/2024-05-30-influxdb

GreptimeDB 以其强大的可扩展性和经济适用的云上存储能力，可以作为 InfluxDB 高效可行的替代方案。那么，如何将数据从 InfluxDB v1 迁移到 GreptimeDB？本文详细介绍了具体的迁移方法。

GreptimeDB 具有基于云的弹性和强大经济的存储能力，因此可以作为 InfluxDB 的绝佳替代品。它也可被视为 InfluxDB v3 的生产就绪实施方案。

## 前置条件

在开始之前，你需要能够访问部署 InfluxDB 的服务器，以直接操作数据文件。如果你使用 InfluxDB 的 v1.8 官方 Docker 镜像运行服务器，存储数据文件的引擎路径为 `/var/lib/influxdb/`。

本教程将指导你如何迁移 InfluxDB 服务器上的数据，查看文末附录了解如何准备迁移环境。


## 创建 GreptimeDB 服务

在确定存储数据文件的引擎路径之后，首先创建一个 GreptimeDB 的服务作为数据迁移的目的地。

获取 GreptimeDB 集群最快的方式是注册 [GreptimeCloud]（https://greptime.com/product/cloud)  并启动一个 Hobby Plan Service（完全免费，不需要绑定银行卡）。


根据指示创建一个新的 GreptimeDB 服务，点击 Connection Information 按钮获取连接信息，设置所需的环境变量值：

```shell
export GREPTIME_DB="<dbname>"
export GREPTIME_HOST="<host>"
export GREPTIME_USERNAME="<username>"
export GREPTIME_PASSWORD="<password>"
```

<figure>
<img src="/zh/blogs/2024-05-30-influxdb/image1.png" alt="（图 1：创建服务后显示的示意图）" style="width: 80%; margin: 0 auto">
<figcaption>（图 1：创建服务后显示的示意图）</figcaption>
</figure>


## 从 InfluxDB v1 服务器导出数据

执行以下命令，将存储在 InfluxDB 服务中的数据以行协议格式导出：

```shell
export DATABASE="<dbname>"
export ENGINE_PATH="<engine-path>"

influx_inspect export \
    -database $DATABASE \
    -lponly \
    -datadir $ENGINE_PATH/data \
    -waldir $ENGINE_PATH/wal \
    -out /tmp/influxdb_export.lp
```


如果你是按照文末附录启动的 InfluxDB 服务，可以运行以下命令在 Docker 容器环境里导出数据：

```shell
export DATABASE="mydb"
export ENGINE_PATH="/var/lib/influxdb"

docker exec influxdb1 influx_inspect export \
    -database $DATABASE \
    -lponly \
    -datadir $ENGINE_PATH/data \
    -waldir $ENGINE_PATH/wal \
    -out /tmp/influxdb_export.lp
```


命令行输出结果示例如下：

```
writing out wal file data for mydb/autogen...complete.
```

🌟 *Tip*
*你可以指定更具体的数据集进行导出，如时间范围。
相关详细信息请参考指导手册：[influx_inspect export](https://docs.influxdata.com/influxdb/v1/tools/influx_inspect/#export)*


## 把数据导入到 GreptimeDB

复制 `influxdb_export.lp`  到工作目录。如果你是按照文末附录做的配置，可以运行以下命令从  Docker 容器中拷贝数据文件到宿主机上：

```shell
docker cp influxdb2:/influxdb_export.lp influxdb_export.lp
```


在将数据导入 GreptimeDB 之前，如果数据文件过大，建议将数据文件分割为多个片段：

```shell
split -l 1000 -d -a 10 influxdb_export.lp influxdb_export_slice.
# -l [line_count]    Create split files line_count lines in length.
# -d                 Use a numeric suffix instead of a alphabetic suffix.
# -a [suffix_length] Use suffix_length letters to form the suffix of the file name.
```


现在，通过 HTTP API 将数据导入 GreptimeDB：

```shell
for file in influxdb_export_slice.*; do
    curl -i -H "Authorization: token $GREPTIME_USERNAME:$GREPTIME_PASSWORD" \
        -X POST "https://${GREPTIME_HOST}/v1/influxdb/write?db=$GREPTIME_DB" \
        --data-binary @${file}
    # avoid rate limit in the hobby plan
    sleep 1
done
```

完成后，你可以在 GreptimeCloud 的 Web Dashboard 上检查已导入的数据。

<figure>
<img src="/zh/blogs/2024-05-30-influxdb/image2.png" alt="（图 2：Web Dashboard 示意图）" style="width: 80%; margin: 0 auto">
<figcaption>（图 2：Web Dashboard 示意图）</figcaption>
</figure>

<figure>
<img src="/zh/blogs/2024-05-30-influxdb/image3.png" alt="（图 3：数据分析示意图）" style="width: 80%; margin: 0 auto">
<figcaption>（图 3：数据分析示意图）</figcaption>
</figure>



<figure>
<img src="/zh/blogs/2024-05-30-influxdb/image4.png" alt="（图 4：数据分析示意图 2）" style="width: 80%; margin: 0 auto">
<figcaption>（图 4：数据分析示意图 2）</figcaption>
</figure>



### 附录：启动一个 InfluxDB v1 服务器

本节展示了如何启动一个 InfluxDB v1 服务器并准备示例数据。

首先进入到一个空目录，并运行以下 Docker 命令启动一个名为 `influxdb1` 的新容器：

```shell
docker run -p 8086:8086 --name influxdb1 influxdb:1.8
```


然后，启动一个交互式 influx shell 来访问刚刚部署的 InfluxDB 服务：

```shell
docker exec -it influxdb1 influx
```



通过 influx shell 创建一个数据库：


```sql
CREATE DATABASE mydb;
SHOW DATABASES;
```


然后，下载 InfluxDB 行协议格式的样例数据，并导入到刚刚创建的 `mydb` 数据库里：


```shell
curl -O https://raw.githubusercontent.com/influxdata/influxdb2-sample-data/master/air-sensor-data/air-sensor-data.lp
curl -i -XPOST "http://localhost:8086/write?db=mydb" --data-binary @air-sensor-data.lp
```

通过 influx shell 确认数据已经成功导入：


```sql
USE mydb;
SELECT COUNT(*) FROM airSensors;
```


你应该看到以下输出结果：

```sql
name: airSensors
time count_co count_humidity count_temperature
---- -------- -------------- -----------------
0    5288     5288           5288
```

## 关于 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/
