从 InfluxDB 迁移
本文档将帮助你了解 GreptimeDB 和 InfluxDB 的数据模型之间的区别,并指导你完成迁移过程。
数据模型的区别
要了解 InfluxDB 和 GreptimeDB 的数据模型之间的差异,请参考写入数据文档中的数据模型。
数据库连接信息
在写入或查询数据之前,需要了解 InfluxDB 和 GreptimeDB 之间的数据库连接信息的差异。
- Token:InfluxDB API 中的 token 用于身份验证,与 GreptimeDB 身份验证相同。
当使用 InfluxDB 的客户端库或 HTTP API 与 GreptimeDB 交互时,你可以使用
<greptimedb_user:greptimedb_password>作为 token。 - Organization:GreptimeDB 中没有组织。
- Bucket:在 InfluxDB 中,bucket 是时间序列数据的容器,与 GreptimeDB 中的数据库名称相同。
写入数据
GreptimeDB 兼容 InfluxDB 的行协议格式,包括 v1 和 v2。 这意味着你可以轻松地从 InfluxDB 迁移到 GreptimeDB。
HTTP API
你可以使用以下 HTTP API 请求将 measurement 写入 GreptimeDB:
- InfluxDB line protocol v2
- InfluxDB line protocol v1
curl -X POST 'http://{{host}}:4000/v1/influxdb/api/v2/write?db={{db-name}}' \
-H 'authorization: token {{greptime_user:greptimedb_password}}' \
-d 'census,location=klamath,scientist=anderson bees=23 1566086400000000000'
curl 'http://{{host}}:4000/v1/influxdb/write?db={{db-name}}&u={{greptime_user}}&p={{greptimedb_password}}' \
-d 'census,location=klamath,scientist=anderson bees=23 1566086400000000000'
Telegraf
GreptimeDB 支持 InfluxDB 行协议也意味着 GreptimeDB 与 Telegraf 兼容。 要配置 Telegraf,只需将 GreptimeDB 的 URL 添加到 Telegraf 配置中:
有关详细的配置说明,请参考 通过 Telegraf 写入数据 文档。
客户端库
使用 InfluxDB 客户端库写入数据到 GreptimeDB 非常直接且简单。 你只需在客户端配置中包含 URL 和身份验证信息。
例如:
- Node.js
- Python
- Go
- Java
- PHP
'use strict'
/** @module write
**/
import { InfluxDB, Point } from '@influxdata/influxdb-client'
/** Environment variables **/
const url = 'http://<host>:4000/v1/influxdb'
const token = '<greptime_user>:<greptimedb_password>'
const org = ''
const bucket = '<db-name>'
const influxDB = new InfluxDB({ url, token })
const writeApi = influxDB.getWriteApi(org, bucket)
writeApi.useDefaultTags({ region: 'west' })
const point1 = new Point('temperature')
.tag('sensor_id', 'TLM01')
.floatField('value', 24.0)
writeApi.writePoint(point1)
import influxdb_client
from influxdb_client.client.write_api import SYNCHRONOUS
bucket = "<db-name>"
org = ""
token = "<greptime_user>:<greptimedb_password>"
url="http://<host>:4000/v1/influxdb"
client = influxdb_client.InfluxDBClient(
url=url,
token=token,
org=org
)
# Write script
write_api = client.write_api(write_options=SYNCHRONOUS)
p = influxdb_client.Point("my_measurement").tag("location", "Prague").field("temperature", 25.3)
write_api.write(bucket=bucket, org=org, record=p)
bucket := "<db-name>"
org := ""
token := "<greptime_user>:<greptimedb_password>"
url := "http://<host>:4000/v1/influxdb"
client := influxdb2.NewClient(url, token)
writeAPI := client.WriteAPIBlocking(org, bucket)
p := influxdb2.NewPoint("stat",
map[string]string{"unit": "temperature"},
map[string]interface{}{"avg": 24.5, "max": 45},
time.Now())
writeAPI.WritePoint(context.Background(), p)
client.Close()
private static String url = "http://<host>:4000/v1/influxdb";
private static String org = "";
private static String bucket = "<db-name>";
private static char[] token = "<greptime_user>:<greptimedb_password>".toCharArray();
public static void main(final String[] args) {
InfluxDBClient influxDBClient = InfluxDBClientFactory.create(url, token, org, bucket);
WriteApiBlocking writeApi = influxDBClient.getWriteApiBlocking();
Point point = Point.measurement("temperature")
.addTag("location", "west")
.addField("value", 55D)
.time(Instant.now().toEpochMilli(), WritePrecision.MS);
writeApi.writePoint(point);
influxDBClient.close();
}
$client = new Client([
"url" => "http://<host>:4000/v1/influxdb",
"token" => "<greptime_user>:<greptimedb_password>",
"bucket" => "<db-name>",
"org" => "",
"precision" => InfluxDB2\Model\WritePrecision::S
]);
$writeApi = $client->createWriteApi();
$dateTimeNow = new DateTime('NOW');
$point = Point::measurement("weather")
->addTag("location", "Denver")
->addField("temperature", rand(0, 20))
->time($dateTimeNow->getTimestamp());
$writeApi->write($point);
除了上述语言之外,GreptimeDB 还支持其他 InfluxDB 支持的客户端库。 你可以通过参考上面提供的连接信息代码片段,使用你喜欢的语言编写代码。
查询数据
GreptimeDB 不支持 Flux 和 InfluxQL,而是使用 SQL 和 PromQL。
SQL 是一种通用的用于管理和操作关系数据库的语言。 具有灵活的数据检索、操作和分析功能, 减少了已经熟悉 SQL 的用户的学习曲 线。
PromQL(Prometheus 查询语言)允许用户实时选择和聚合时间序列数据, 表达式的结果可以显示为图形,也可以在 Prometheus 的表达式浏览器中以表格数据的形式查看, 或通过 HTTP API 传递给外部系统。
假设你要查询过去 24 小时内记录的 monitor 表中的最大 CPU。
在 InfluxQL 中,查询如下:
SELECT
MAX("cpu")
FROM
"monitor"
WHERE
time > now() - 24h
GROUP BY
time(1h)
此 InfluxQL 查询计算 monitor 表中 cpu字段的最大值,
其中时间大于当前时间减去 24 小时,结果以一小时为间隔进行分组。
该查询在 Flux 中的表 达如下:
from(bucket: "public")
|> range(start: -24h)
|> filter(fn: (r) => r._measurement == "monitor")
|> aggregateWindow(every: 1h, fn: max)
在 GreptimeDB SQL 中,类似的查询为:
SELECT
greptime_timestamp,
host,
AVG(cpu) RANGE '1h' as mean_cpu
FROM
monitor
WHERE
greptime_timestamp > NOW() - '24 hours'::INTERVAL
ALIGN '1h' TO NOW
ORDER BY greptime_timestamp DESC;
在该 SQL 查询中,
RANGE 子句确定了 AVG(cpu) 聚合函数的时间窗口,
而 ALIGN 子句设置了时间序列数据的对齐时间。
有关按时间窗口分组的更多详细信息,请参考按时间窗口聚合数据文档。
在 PromQL 中,类似的查询为:
avg_over_time(monitor[1h])
要查询最后 24 小时的时间序列数据,
你需要执行此 PromQL 并使用 HTTP API 的 start 和 end 参数定义时间范围。
有关 PromQL 的更多信息,请参考 PromQL 文档。
可视化数据
推荐使用 Grafana 可视化 GreptimeDB 数据, 请参考 Grafana 文档 了解如何配置 GreptimeDB。
迁移数据
你可以通过以下步骤实现从 InfluxDB 到 GreptimeDB 的数据无缝迁移:
- 同时将数据写入 GreptimeDB 和 InfluxDB,以避免迁移过程中的数据丢失。
- 从 InfluxDB 导出所有历史数据,并将数据导入 GreptimeDB。
- 停止向 InfluxDB 写入数据,并移除 InfluxDB 服务器。
双写 GreptimeDB 和 InfluxDB
将数据双写 GreptimeDB 和 InfluxDB 是迁移过程中防止数据丢失的有效策略。 当使用 InfluxDB 的客户端库时,你可以建立两个客户端实例,一个用于 GreptimeDB,另一个用于 InfluxDB。 有关如何使用 InfluxDB 行协议将数据写入 GreptimeDB 的操作,请参考写入数据部分。
如果无需保留所有历史数据, 你可以双写一段时间以积累所需的最新数据, 然后停止向 InfluxDB 写入数据并仅使用 GreptimeDB。 如果需要完整迁移所有历史数据,请按照接下来的步骤操作。