InfluxDB Line Protocol
GreptimeDB 支持 HTTP InfluxDB Line 协议。
写入新数据
协议
Post 指标
你可以通过兼容 InfluxDB 的写入 API 写入数据。 以下是一个示例:
- InfluxDB line protocol V2
- InfluxDB line protocol V1
curl -i -XPOST "http://localhost:4000/v1/influxdb/api/v2/write?bucket=public&precision=ms" \
-H "authorization: token <greptime_user:greptimedb_password>" \
--data-binary \
'monitor,host=127.0.0.1 cpu=0.1,memory=0.4 1667446797450
monitor,host=127.0.0.2 cpu=0.2,memory=0.3 1667446798450
monitor,host=127.0.0.1 cpu=0.5,memory=0.2 1667446798450'
curl -i -XPOST "http://localhost:4000/v1/influxdb/write?db=public&precision=ms&u=<greptime_user>&p=<greptimedb_password>" \
--data-binary \
'monitor,host=127.0.0.1 cpu=0.1,memory=0.4 1667446797450
monitor,host=127.0.0.2 cpu=0.2,memory=0.3 1667446798450
monitor,host=127.0.0.1 cpu=0.5,memory=0.2 1667446798450'
兼容 InfluxDB 的写入 API 支持以下查询参数:
bucket:使用 InfluxDB line protocol V2 API 写入时,指定要写入的数据库。默认值为public。db:指定要写入的数据库。这是 InfluxDB line protocol V1 API 的标准参数。V2 API 也接受db作为 GreptimeDB 的兼容别名,但 V2 推荐使用bucket。precision:定义请求体中提供的时间戳的精度,可接受的值为ns(纳秒)、us(微秒)、ms(毫秒)和s(秒),默认值为ns(纳秒)。该 API 写入的时间戳类型为TimestampNanosecond,因此默认精度为ns(纳秒)。如果你在请求体中使用了其他精度的时间戳,需要使用此参数指定精度。该参数确保时间戳能够被准确解释并以纳秒精度存储。
你可以通过 HTTP hints 为自动创建的表设置选项。
例如,将 append_mode hint 设置为 true 以创 建 append-only 表:
curl -i -XPOST "http://localhost:4000/v1/influxdb/write?db=public&precision=ms&u=<greptime_user>&p=<greptimedb_password>" \
-H 'x-greptime-hint-append_mode: true' \
--data-binary \
'monitor,host=127.0.0.1 cpu=0.1,memory=0.4 1667446797450'
当显式提供 append_mode=true 时,GreptimeDB 会使用 append_mode = 'true' 和 merge_mode = 'last_row' 创建表。
否则,通过 InfluxDB 行协议自动创建的表会使用 HTTP merge_mode hint 指定的 merge 模式。如果未提供该 hint,GreptimeDB 会使用配置项 influxdb.default_merge_mode 的值,该配置默认值为 last_non_null。
如需修改通过 InfluxDB 行协议自动创建表时使用的集群级默认值,请在 standalone 或 frontend 配置文件中设置该选项:
[influxdb]
default_merge_mode = "last_row"
可选值为 last_non_null 和 last_row。
你还可以在发送请求时省略 timestamp,GreptimeDB 将使用主机机器的当前系统时间(UTC 时间)作为 timestamp。例如:
- InfluxDB line protocol V2
- InfluxDB line protocol V1
curl -i -XPOST "http://localhost:4000/v1/influxdb/api/v2/write?bucket=public" \
-H "authorization: token <greptime_user:greptimedb_password>" \
--data-binary \
'monitor,host=127.0.0.1 cpu=0.1,memory=0.4
monitor,host=127.0.0.2 cpu=0.2,memory=0.3
monitor,host=127.0.0.1 cpu=0.5,memory=0.2'
curl -i -XPOST "http://localhost:4000/v1/influxdb/write?db=public&u=<greptime_user>&p=<greptimedb_password>" \
--data-binary \
'monitor,host=127.0.0.1 cpu=0.1,memory=0.4
monitor,host=127.0.0.2 cpu=0.2,memory=0.3
monitor,host=127.0.0.1 cpu=0.5,memory=0.2'