InfluxDB Line Protocol

GreptimeDB 支持 HTTP InfluxDB Line 协议。

写入新数据

协议

Post 指标

你可以通过 /influxdb/write API 写入数据。 以下是一个示例:

  • InfluxDB line protocol V2
  • InfluxDB line protocol V1
1. curl -i -XPOST "http://localhost:4000/v1/influxdb/api/v2/write?db=public&precision=ms" \
2. --data-binary \
3. 'monitor,host=127.0.0.1 cpu=0.1,memory=0.4 1667446797450
4. monitor,host=127.0.0.2 cpu=0.2,memory=0.3 1667446798450
5. monitor,host=127.0.0.1 cpu=0.5,memory=0.2 1667446798450'
1. curl -i -XPOST "http://localhost:4000/v1/influxdb/write?db=public&precision=ms" \
2. --data-binary \
3. 'monitor,host=127.0.0.1 cpu=0.1,memory=0.4 1667446797450
4. monitor,host=127.0.0.2 cpu=0.2,memory=0.3 1667446798450
5. monitor,host=127.0.0.1 cpu=0.5,memory=0.2 1667446798450'

/influxdb/write 支持查询参数,包括:

  • db:指定要写入的数据库。默认值为 public。
  • precision:定义请求体中提供的时间戳的精度,可接受的值为 ns(纳秒)、us(微秒)、ms(毫秒)和 s(秒),默认值为 ns(纳秒)。该 API 写入的时间戳类型为 TimestampNanosecond,因此默认精度为 ns(纳秒)。如果你在请求体中使用了其他精度的的时间戳,需要使用此参数指定精度。该参数确保时间戳能够被准确解释并以纳秒精度存储。

你还可以在发送请求时省略 timestamp,GreptimeDB 将使用主机机器的当前系统时间(UTC 时间)作为 timestamp。例如:

  • InfluxDB line protocol V2
  • InfluxDB line protocol V1
1. curl -i -XPOST "http://localhost:4000/v1/influxdb/api/v2/write?db=public" \
2. --data-binary \
3. 'monitor,host=127.0.0.1 cpu=0.1,memory=0.4
4. monitor,host=127.0.0.2 cpu=0.2,memory=0.3
5. monitor,host=127.0.0.1 cpu=0.5,memory=0.2'
1. curl -i -XPOST "http://localhost:4000/v1/influxdb/write?db=public" \
2. --data-binary \
3. 'monitor,host=127.0.0.1 cpu=0.1,memory=0.4
4. monitor,host=127.0.0.2 cpu=0.2,memory=0.3
5. monitor,host=127.0.0.1 cpu=0.5,memory=0.2'

鉴权

GreptimeDB 与 InfluxDB 的行协议鉴权格式兼容,包括 V1 和 V2。 如果你在 GreptimeDB 中配置了鉴权,需要在 HTTP 请求中提供用户名和密码。

  • InfluxDB line protocol V2
  • InfluxDB line protocol V1

InfluxDB 的 V2 协议 使用了类似 HTTP 标准 basic 认证方案的格式。

1. curl 'http://localhost:4000/v1/influxdb/api/v2/write?db=public' \
2. -H 'authorization: token <username>:<password>' \
3. -d 'monitor,host=127.0.0.1 cpu=0.1,memory=0.4'

对于 InfluxDB 的 V1 协议 的鉴权格式。在 HTTP 查询字符串中添加 u 作为用户和 p 作为密码,如下所示:

1. curl 'http://localhost:4000/v1/influxdb/write?db=public&u=<username>&p=<password>' \
2. -d 'monitor,host=127.0.0.1 cpu=0.1,memory=0.4'

Telegraf

GreptimeDB 支持 InfluxDB 行协议也意味着 GreptimeDB 与 Telegraf 兼容。 要配置 Telegraf,只需将 GreptimeDB 的 URL 添加到 Telegraf 配置中:

  • InfluxDB line protocol v2
  • InfluxDB line protocol v1
1. [[outputs.influxdb_v2]]
2. urls = ["http://<host>:4000/v1/influxdb"]
3. token = "<greptime_user>:<greptimedb_password>"
4. bucket = "<db-name>"
5. ## Leave empty
6. organization = ""
1. [[outputs.influxdb]]
2. urls = ["http://<host>:4000/v1/influxdb"]
3. database = "<db-name>"
4. username = "<greptime_user>"
5. password = "<greptimedb_password>"

数据模型

你可能已经熟悉了 InfluxDB 的关键概念, GreptimeDB 的数据模型 是值得了解的新事物。 下方解释了 GreptimeDB 和 InfluxDB 数据模型的相似和不同之处:

  • 两者都是 schemaless 写入的解决方案,这意味着在写入数据之前无需定义表结构。
  • GreptimeDB 的表在自动创建时会设置表选项 merge_mode为 last_non_null。 这意味着表会通过保留每个字段的最新值来合并具有相同主键和时间戳的行,该行为与 InfluxDB 相同。
  • 在 InfluxDB 中,一个点代表一条数据记录,包含一个 measurement、tag 集、field 集和时间戳。 在 GreptimeDB 中,它被表示为时间序列表中的一行数据。 表名对应于 measurement,列由三种类型组成:Tag、Field 和 Timestamp。
  • GreptimeDB 使用 TimestampNanosecond 作为来自 InfluxDB 行协议 API 的时间戳数据类型。
  • GreptimeDB 使用 Float64 作为来自 InfluxDB 行协议 API 的数值数据类型。

以 InfluxDB 文档中的示例数据为例:

_time _measurement location scientist _field _value
2019-08-18T00:00:00Z census klamath anderson bees 23
2019-08-18T00:00:00Z census portland mullen ants 30
2019-08-18T00:06:00Z census klamath anderson bees 28
2019-08-18T00:06:00Z census portland mullen ants 32

上述数据的 InfluxDB 行协议格式为:

1. census,location=klamath,scientist=anderson bees=23 1566086400000000000
2. census,location=portland,scientist=mullen ants=30 1566086400000000000
3. census,location=klamath,scientist=anderson bees=28 1566086760000000000
4. census,location=portland,scientist=mullen ants=32 1566086760000000000

在 GreptimeDB 数据模型中,上述数据将被表示为 census 表中的以下内容:

1. +---------------------+----------+-----------+------+------+
2. | ts                  | location | scientist | bees | ants |
3. +---------------------+----------+-----------+------+------+
4. | 2019-08-18 00:00:00 | klamath  | anderson  |   23 | NULL |
5. | 2019-08-18 00:06:00 | klamath  | anderson  |   28 | NULL |
6. | 2019-08-18 00:00:00 | portland | mullen    | NULL |   30 |
7. | 2019-08-18 00:06:00 | portland | mullen    | NULL |   32 |
8. +---------------------+----------+-----------+------+------+

census 表结构如下:

1. +-----------+----------------------+------+------+---------+---------------+
2. | Column    | Type                 | Key  | Null | Default | Semantic Type |
3. +-----------+----------------------+------+------+---------+---------------+
4. | location  | String               | PRI  | YES  |         | TAG           |
5. | scientist | String               | PRI  | YES  |         | TAG           |
6. | bees      | Float64              |      | YES  |         | FIELD         |
7. | ts        | TimestampNanosecond  | PRI  | NO   |         | TIMESTAMP     |
8. | ants      | Float64              |      | YES  |         | FIELD         |
9. +-----------+----------------------+------+------+---------+---------------+

参考