InfluxDB Line Protocol
MynaDB 使用 InfluxDB Line Protocol 进行数据写入。这种基于文本的格式简单、可读性强,且非常适合高效写入时序数据。
💡 兼容性
MynaDB 兼容 InfluxDB Line Protocol v1。如果您已经在使用 InfluxDB、Telegraf 或其他输出 Line Protocol 的工具, 只需进行极少的修改即可将数据直接发送到 MynaDB。
Line Protocol 语法
每一行代表一个数据点,格式如下:
syntax
<measurement>[,<tag_key>=<tag_value>...] <field_key>=<field_value>[,<field_key>=<field_value>...] [<timestamp>]
组成部分
| 组成部分 | 是否必需 | 说明 |
|---|---|---|
measurement |
是 | 度量名称(类似于表名) |
tags |
否 | 用于元数据的键值对(已索引,用于过滤) |
fields |
是 | 用于实际数据值的键值对 |
timestamp |
否 | 纳秒精度的 Unix 时间戳(默认使用服务器时间) |
基本示例
简单数据点
line-protocol
cpu usage=75.5
这会在 cpu 度量中创建一个数据点,包含一个字段 usage。
带标签
cpu,host=server01,region=us-west usage=75.5,temperature=45
标签是度量名称之后以逗号分隔的键值对,用于过滤和分组。
带时间戳
cpu,host=server01 usage=75.5 1609459200000000000
时间戳为自 Unix 纪元(1970 年 1 月 1 日)以来的纳秒数。
多个字段
system,host=server01 cpu_usage=75.5,memory_used=8589934592,disk_free=107374182400
数据类型
字段值类型
| 类型 | 语法 | 示例 |
|---|---|---|
| 浮点数 | 普通数字或带小数点 | value=82.5 或 value=82 |
| 整数 | 数字后加 i 后缀 |
count=100i |
| 字符串 | 双引号括起 | status="running" |
| 布尔值 | true/false、t/f、TRUE/FALSE |
active=true |
| 无符号整数 | 数字后加 u 后缀 |
bytes=1024u |
按类型示例
line-protocol
# Float (default for numbers)
temperature value=23.5
# Integer
requests count=1000i
# String
event message="Server started successfully"
# Boolean
status healthy=true
# Unsigned integer
network bytes_sent=1073741824u
# Mixed types in one line
server,host=web01 cpu=75.5,memory=8192i,status="ok",active=true
通过 API 写入数据
单行写入
bash
curl -X POST http://localhost:7000/write/mydb \
-d 'cpu,host=server01,region=us-west usage=75.5,temperature=45'
多行写入(批量)
curl -X POST http://localhost:7000/write/mydb -d '
cpu,host=server01,region=us-west usage=75.5
cpu,host=server02,region=us-east usage=82.3
cpu,host=server03,region=eu-west usage=68.9
memory,host=server01 used=8589934592,total=17179869184
memory,host=server02 used=12884901888,total=17179869184
'
带时间戳写入
curl -X POST http://localhost:7000/write/mydb -d '
cpu,host=server01 usage=75.5 1704067200000000000
cpu,host=server01 usage=76.2 1704067260000000000
cpu,host=server01 usage=74.8 1704067320000000000
'
Schema 策略
MynaDB 支持两种 Schema 策略,决定数据如何组织到 Schema 和表中。
通过 schema.strategy 进行配置。
ServiceAsSchema(默认)
service 标签成为 Schema 名称,measurement 成为表名。
# Write with service tag
curl -X POST http://localhost:7000/write/mydb \
-d 'cpu,service=webserver,host=web01 usage=75.5'
# Query: catalog.schema.table = mydb.webserver.cpu
curl -X POST http://localhost:7000/query/mydb \
-d 'SELECT * FROM mydb.webserver.cpu'
ServiceAsTable
service 标签成为表名,measurement 作为标签存储。
# Write with service tag
curl -X POST http://localhost:7000/write/mydb \
-d 'cpu,service=webserver,host=web01 usage=75.5'
# Query: catalog.schema.table = mydb.default.webserver
curl -X POST http://localhost:7000/query/mydb \
-d 'SELECT * FROM mydb.default.webserver WHERE measurement = '\''cpu'\'''
特殊字符
转义规则
| 位置 | 需转义的字符 | 转义方式 |
|---|---|---|
| 度量名称 | 逗号、空格 | 反斜杠(\, \ ) |
| 标签键/值 | 逗号、等号、空格 | 反斜杠(\, \= \ ) |
| 字段键 | 逗号、等号、空格 | 反斜杠(\, \= \ ) |
| 字符串字段值 | 双引号、反斜杠 | 反斜杠(\" \\) |
示例
# Escaped comma in measurement name
cpu\,usage,host=server01 value=75.5
# Escaped equals in tag value
event,equation=a\=b result=42
# Escaped quotes in string field
log,host=server01 message="Error: \"Connection refused\""
# Escaped backslash
path,host=server01 value="C:\\Users\\admin"
时间戳
精度
MynaDB 默认接受纳秒精度的时间戳:
| 精度 | 示例值 | 说明 |
|---|---|---|
| 纳秒 | 1704067200000000000 |
默认精度(19 位) |
| 微秒 | 1704067200000000 |
16 位 |
| 毫秒 | 1704067200000 |
13 位 |
| 秒 | 1704067200 |
10 位 |
生成时间戳
bash
# Current time in nanoseconds (Linux)
date +%s%N
# Current time in nanoseconds (macOS)
python3 -c "import time; print(int(time.time() * 1e9))"
# Specific date to nanoseconds
date -d "2024-01-01 00:00:00" +%s%N
最佳实践
⚠ 性能建议
- 批量写入:在单个请求中发送多行数据以获得更高的吞吐量
- 限制标签数量:标签用于低基数的元数据(host、region、service)
- 数值使用字段:数值型数据应作为字段而非标签
- 保持 Schema 一致性:在各数据点之间保持字段类型一致
- 合理的批量大小:每个请求 5,000-10,000 行为最佳
正确与错误示例
line-protocol
# GOOD: Low-cardinality tags, numeric fields
cpu,host=server01,region=us-west usage=75.5,cores=8i
# BAD: High-cardinality tag (user_id has millions of values)
pageview,user_id=12345678 count=1
# GOOD: User ID as field instead
pageview,page=/home user_id="12345678",count=1i
# BAD: Numeric value as tag
temperature,value=75.5 dummy=1
# GOOD: Numeric value as field
temperature,sensor=outdoor value=75.5
验证错误
写入 Line Protocol 数据时的常见错误:
| 错误 | 原因 | 解决方案 |
|---|---|---|
| 缺少字段 | 未提供字段值 | 添加至少一个字段:cpu usage=75 |
| 无效时间戳 | 时间戳不是数字 | 使用 Unix 纳秒时间戳:1704067200000000000 |
| 类型冲突 | 字段类型发生变化(例如浮点数变为字符串) | 保持字段类型一致 |
| 空度量名称 | 行以逗号或空格开头 | 首先提供度量名称 |
与 Telegraf 集成
配置 Telegraf 向 MynaDB 发送数据:
toml
[[outputs.influxdb]]
urls = ["http://localhost:7000"]
database = "mydb"
skip_database_creation = true
# Use /write endpoint
[outputs.influxdb.tagpass]
[[inputs.cpu]]
percpu = true
totalcpu = true
[[inputs.mem]]
[[inputs.disk]]
ignore_fs = ["tmpfs", "devtmpfs"]