开始写入数据
- 2 / 3
本教程将引导您完成创建 Line Protocol 数据并将其写入 InfluxDB 的基本步骤。
InfluxDB 提供了许多不同的选项用于摄取或写入数据,包括以下内容:
- InfluxDB HTTP API (v1 和 v2)
- Telegraf
influxctl
CLIinflux3
数据 CLI- InfluxDB 客户端库
如果使用 Telegraf 或 InfluxDB 客户端库等工具,它们可以为您构建 Line Protocol,但最好了解 Line Protocol 的工作原理。
Line Protocol
所有写入 InfluxDB 的数据都使用 Line Protocol 写入,这是一种基于文本的格式,可让您提供将数据点写入 InfluxDB 所需的信息。本教程涵盖了 Line Protocol 的基础知识,但有关详细信息,请参阅 Line Protocol 参考。
Line Protocol 元素
Line Protocol 的每一行都包含以下元素:
* 必需- * measurement(测量):一个字符串,用于标识存储数据的表。
- tag set(标签集):逗号分隔的键值对列表,每个键值对代表一个标签。标签键和值是不带引号的字符串。空格、逗号和等号字符必须转义。
- * field set(字段集):逗号分隔的键值对列表,每个键值对代表一个字段。字段键是不带引号的字符串。空格和逗号必须转义。字段值可以是 字符串(带引号)、浮点数、整数、无符号整数或 布尔值。
- timestamp(时间戳):与数据关联的 Unix 时间戳。 InfluxDB 支持纳秒级精度。如果时间戳的精度不是纳秒,则在将数据写入 InfluxDB 时,您必须指定精度。
Line Protocol 元素解析
- measurement(测量):第一个未转义的逗号之前,第一个空格之前的所有内容。
- tag set(标签集):第一个未转义的逗号和第一个未转义的空格之间的键值对。
- field set(字段集):第一个和第二个未转义的空格之间的键值对。
- timestamp(时间戳):第二个未转义的空格之后的整数值。
- 行由换行符 (
\n
) 分隔。 Line Protocol 对空格敏感。
myMeasurement,tag1=val1,tag2=val2 field1="v1",field2=1i 0000000000000000000
有关模式设计建议,请参阅 InfluxDB 模式设计。
构建 Line Protocol
在基本了解 Line Protocol 之后,您现在可以构建 Line Protocol 并将数据写入 InfluxDB。考虑一个用例,您从家中的传感器收集数据。每个传感器收集温度、湿度和一氧化碳读数。要收集此数据,请使用以下模式:
- measurement(测量):
home
- 标签
room
:客厅或厨房
- 字段
temp
:温度,单位 °C(浮点数)hum
:湿度百分比(浮点数)co
:一氧化碳,单位百万分之几 (ppm)(整数)
- timestamp(时间戳):Unix 时间戳,精度为秒
- 标签
以下 Line Protocol 示例表示从 2022-01-01T08:00:00Z (UTC) 到 2022-01-01T20:00:00Z (UTC) 每小时收集的数据。
家庭传感器数据 Line Protocol
home,room=Living\ Room temp=21.1,hum=35.9,co=0i 1641024000
home,room=Kitchen temp=21.0,hum=35.9,co=0i 1641024000
home,room=Living\ Room temp=21.4,hum=35.9,co=0i 1641027600
home,room=Kitchen temp=23.0,hum=36.2,co=0i 1641027600
home,room=Living\ Room temp=21.8,hum=36.0,co=0i 1641031200
home,room=Kitchen temp=22.7,hum=36.1,co=0i 1641031200
home,room=Living\ Room temp=22.2,hum=36.0,co=0i 1641034800
home,room=Kitchen temp=22.4,hum=36.0,co=0i 1641034800
home,room=Living\ Room temp=22.2,hum=35.9,co=0i 1641038400
home,room=Kitchen temp=22.5,hum=36.0,co=0i 1641038400
home,room=Living\ Room temp=22.4,hum=36.0,co=0i 1641042000
home,room=Kitchen temp=22.8,hum=36.5,co=1i 1641042000
home,room=Living\ Room temp=22.3,hum=36.1,co=0i 1641045600
home,room=Kitchen temp=22.8,hum=36.3,co=1i 1641045600
home,room=Living\ Room temp=22.3,hum=36.1,co=1i 1641049200
home,room=Kitchen temp=22.7,hum=36.2,co=3i 1641049200
home,room=Living\ Room temp=22.4,hum=36.0,co=4i 1641052800
home,room=Kitchen temp=22.4,hum=36.0,co=7i 1641052800
home,room=Living\ Room temp=22.6,hum=35.9,co=5i 1641056400
home,room=Kitchen temp=22.7,hum=36.0,co=9i 1641056400
home,room=Living\ Room temp=22.8,hum=36.2,co=9i 1641060000
home,room=Kitchen temp=23.3,hum=36.9,co=18i 1641060000
home,room=Living\ Room temp=22.5,hum=36.3,co=14i 1641063600
home,room=Kitchen temp=23.1,hum=36.6,co=22i 1641063600
home,room=Living\ Room temp=22.2,hum=36.4,co=17i 1641067200
home,room=Kitchen temp=22.7,hum=36.5,co=26i 1641067200
将 Line Protocol 写入 InfluxDB
以下示例展示了如何将前面的 示例数据(已采用 Line Protocol 格式)写入 InfluxDB Clustered 数据库。
要了解有关可用工具和选项的更多信息,请参阅写入数据。
本入门教程中的某些示例假设您的 InfluxDB 凭据(URL、组织和 token(令牌))由 环境变量 提供。
使用 influxctl write
命令 将 家庭传感器示例数据 写入您的 InfluxDB 集群。提供以下信息:
- 使用
--database
标志指定数据库名称 - 使用
--token
标志指定数据库令牌(使用在 入门 - 设置 InfluxDB Clustered 中创建的INFLUX_TOKEN
环境变量) - 使用
--precision
标志将时间戳精度设置为秒 (s
) - 家庭传感器数据 Line Protocol
influxctl write \
--database get-started \
--token $INFLUX_TOKEN \
--precision s \
'home,room=Living\ Room temp=21.1,hum=35.9,co=0i 1641024000
home,room=Kitchen temp=21.0,hum=35.9,co=0i 1641024000
home,room=Living\ Room temp=21.4,hum=35.9,co=0i 1641027600
home,room=Kitchen temp=23.0,hum=36.2,co=0i 1641027600
home,room=Living\ Room temp=21.8,hum=36.0,co=0i 1641031200
home,room=Kitchen temp=22.7,hum=36.1,co=0i 1641031200
home,room=Living\ Room temp=22.2,hum=36.0,co=0i 1641034800
home,room=Kitchen temp=22.4,hum=36.0,co=0i 1641034800
home,room=Living\ Room temp=22.2,hum=35.9,co=0i 1641038400
home,room=Kitchen temp=22.5,hum=36.0,co=0i 1641038400
home,room=Living\ Room temp=22.4,hum=36.0,co=0i 1641042000
home,room=Kitchen temp=22.8,hum=36.5,co=1i 1641042000
home,room=Living\ Room temp=22.3,hum=36.1,co=0i 1641045600
home,room=Kitchen temp=22.8,hum=36.3,co=1i 1641045600
home,room=Living\ Room temp=22.3,hum=36.1,co=1i 1641049200
home,room=Kitchen temp=22.7,hum=36.2,co=3i 1641049200
home,room=Living\ Room temp=22.4,hum=36.0,co=4i 1641052800
home,room=Kitchen temp=22.4,hum=36.0,co=7i 1641052800
home,room=Living\ Room temp=22.6,hum=35.9,co=5i 1641056400
home,room=Kitchen temp=22.7,hum=36.0,co=9i 1641056400
home,room=Living\ Room temp=22.8,hum=36.2,co=9i 1641060000
home,room=Kitchen temp=23.3,hum=36.9,co=18i 1641060000
home,room=Living\ Room temp=22.5,hum=36.3,co=14i 1641063600
home,room=Kitchen temp=23.1,hum=36.6,co=22i 1641063600
home,room=Living\ Room temp=22.2,hum=36.4,co=17i 1641067200
home,room=Kitchen temp=22.7,hum=36.5,co=26i 1641067200'
如果成功,输出将是成功消息;否则,输出将是错误详细信息和失败消息。
使用 Telegraf 消费 Line Protocol,然后将其写入 InfluxDB Clustered。
如果您尚未执行此操作,请按照说明 下载并安装 Telegraf。
复制 家庭传感器数据示例 并将其保存到本地系统上的文件中,例如
home.lp
。cat <<- EOF > home.lp home,room=Living\ Room temp=21.1,hum=35.9,co=0i 1641024000 home,room=Kitchen temp=21.0,hum=35.9,co=0i 1641024000 home,room=Living\ Room temp=21.4,hum=35.9,co=0i 1641027600 home,room=Kitchen temp=23.0,hum=36.2,co=0i 1641027600 home,room=Living\ Room temp=21.8,hum=36.0,co=0i 1641031200 home,room=Kitchen temp=22.7,hum=36.1,co=0i 1641031200 home,room=Living\ Room temp=22.2,hum=36.0,co=0i 1641034800 home,room=Kitchen temp=22.4,hum=36.0,co=0i 1641034800 home,room=Living\ Room temp=22.2,hum=35.9,co=0i 1641038400 home,room=Kitchen temp=22.5,hum=36.0,co=0i 1641038400 home,room=Living\ Room temp=22.4,hum=36.0,co=0i 1641042000 home,room=Kitchen temp=22.8,hum=36.5,co=1i 1641042000 home,room=Living\ Room temp=22.3,hum=36.1,co=0i 1641045600 home,room=Kitchen temp=22.8,hum=36.3,co=1i 1641045600 home,room=Living\ Room temp=22.3,hum=36.1,co=1i 1641049200 home,room=Kitchen temp=22.7,hum=36.2,co=3i 1641049200 home,room=Living\ Room temp=22.4,hum=36.0,co=4i 1641052800 home,room=Kitchen temp=22.4,hum=36.0,co=7i 1641052800 home,room=Living\ Room temp=22.6,hum=35.9,co=5i 1641056400 home,room=Kitchen temp=22.7,hum=36.0,co=9i 1641056400 home,room=Living\ Room temp=22.8,hum=36.2,co=9i 1641060000 home,room=Kitchen temp=23.3,hum=36.9,co=18i 1641060000 home,room=Living\ Room temp=22.5,hum=36.3,co=14i 1641063600 home,room=Kitchen temp=23.1,hum=36.6,co=22i 1641063600 home,room=Living\ Room temp=22.2,hum=36.4,co=17i 1641067200 home,room=Kitchen temp=22.7,hum=36.5,co=26i 1641067200 EOF
运行以下命令以生成 Telegraf 配置文件 (
telegraf.conf
),该文件启用inputs.file
和outputs.influxdb_v2
插件mkdir -p iot-project \ && telegraf --sample-config \ --input-filter file \ --output-filter influxdb_v2 \ > iot-project/telegraf.conf
在编辑器中,打开
iot-project/telegraf.conf
并配置以下内容:file
输入插件:在[[inputs.file]].files
列表中,将"/tmp/metrics.out"
替换为您的示例数据文件名。如果 Telegraf 在启动时找不到文件,它将停止处理并退出。[[inputs.file]] ## Files to parse each interval. Accept standard unix glob matching rules, ## as well as ** to match recursive files and directories. files = ["home.lp"] # Set the timestamp precision to the precision in your data. influx_timestamp_precision = '1s' ## Optionally, use the newer, more efficient line protocol parser influx_parser_type = 'upstream'
使用您已有的、已使用 InfluxDB v1 /write
API 端点的工作负载写入数据。
如果要从 InfluxDB 1.x 迁移数据,请参阅 从 InfluxDB 1.x 迁移到 InfluxDB InfluxDB Clustered 指南。
要使用 InfluxDB v1 HTTP API 将数据写入 InfluxDB,请使用 POST
请求方法向 InfluxDB API /write
端点 发送请求。
POST https://cluster-host.com/write
在您的请求中包含以下内容:
- 标头:
- Authorization(授权):Bearer <INFLUX_TOKEN>
- Content-Type(内容类型):text/plain; charset=utf-8
- Accept(接受):application/json
- 查询参数:
- db:InfluxDB 数据库名称
- precision(精度):时间戳精度(默认为
ns
)
- 请求正文:Line Protocol,纯文本格式
使用 InfluxDB Clustered v1 API /write
端点,Authorization: Bearer
和 Authorization: Token
是等效的,您可以使用任一方案在请求中传递数据库令牌。包含单词 Bearer
或 Token
、一个空格和您的 token(令牌) 值(所有值都区分大小写)。有关 HTTP API 令牌方案的更多信息,请参阅如何验证 API 请求。
以下示例使用 cURL 和 InfluxDB v1 API 将 Line Protocol 写入 InfluxDB
response=$(curl --silent --write-out "%{response_code}:-%{errormsg}" \
"https://cluster-host.com/write?db=get-started&precision=s" \
--header "Authorization: Bearer DATABASE_TOKEN" \
--header "Content-type: text/plain; charset=utf-8" \
--header "Accept: application/json" \
--data-binary "
home,room=Living\ Room temp=21.1,hum=35.9,co=0i 1641024000
home,room=Kitchen temp=21.0,hum=35.9,co=0i 1641024000
home,room=Living\ Room temp=21.4,hum=35.9,co=0i 1641027600
home,room=Kitchen temp=23.0,hum=36.2,co=0i 1641027600
home,room=Living\ Room temp=21.8,hum=36.0,co=0i 1641031200
home,room=Kitchen temp=22.7,hum=36.1,co=0i 1641031200
home,room=Living\ Room temp=22.2,hum=36.0,co=0i 1641034800
home,room=Kitchen temp=22.4,hum=36.0,co=0i 1641034800
home,room=Living\ Room temp=22.2,hum=35.9,co=0i 1641038400
home,room=Kitchen temp=22.5,hum=36.0,co=0i 1641038400
home,room=Living\ Room temp=22.4,hum=36.0,co=0i 1641042000
home,room=Kitchen temp=22.8,hum=36.5,co=1i 1641042000
home,room=Living\ Room temp=22.3,hum=36.1,co=0i 1641045600
home,room=Kitchen temp=22.8,hum=36.3,co=1i 1641045600
home,room=Living\ Room temp=22.3,hum=36.1,co=1i 1641049200
home,room=Kitchen temp=22.7,hum=36.2,co=3i 1641049200
home,room=Living\ Room temp=22.4,hum=36.0,co=4i 1641052800
home,room=Kitchen temp=22.4,hum=36.0,co=7i 1641052800
home,room=Living\ Room temp=22.6,hum=35.9,co=5i 1641056400
home,room=Kitchen temp=22.7,hum=36.0,co=9i 1641056400
home,room=Living\ Room temp=22.8,hum=36.2,co=9i 1641060000
home,room=Kitchen temp=23.3,hum=36.9,co=18i 1641060000
home,room=Living\ Room temp=22.5,hum=36.3,co=14i 1641063600
home,room=Kitchen temp=23.1,hum=36.6,co=22i 1641063600
home,room=Living\ Room temp=22.2,hum=36.4,co=17i 1641067200
home,room=Kitchen temp=22.7,hum=36.5,co=26i 1641067200
")
# Format the response code and error message output.
response_code=${response%%:-*}
errormsg=${response#*:-}
# Remove leading and trailing whitespace from errormsg
errormsg=$(echo "${errormsg}" | tr -d '[:space:]')
echo "$response_code"
if [[ $errormsg ]]; then
echo "$errormsg"
fi
替换以下内容:
DATABASE_TOKEN
:具有足够权限访问指定数据库的数据库令牌
如果成功,输出将是 HTTP 204 No Content
状态代码;否则,输出将是错误状态代码和失败消息。
204
要使用 InfluxDB v2 HTTP API 将数据写入 InfluxDB,请使用 POST
请求方法向 InfluxDB API /api/v2/write
端点发送请求。
POST https://cluster-host.com/api/v2/write
在您的请求中包含以下内容:
- 标头:
- Authorization(授权):Bearer <INFLUX_TOKEN>
- Content-Type(内容类型):text/plain; charset=utf-8
- Accept(接受):application/json
- 查询参数:
- bucket(存储桶):InfluxDB 数据库名称
- precision(精度):时间戳精度(默认为
ns
)
- 请求正文:Line Protocol,纯文本格式
InfluxDB Clustered v2 API /api/v2/write
端点支持 Bearer
和 Token
授权方案,您可以使用任一方案在请求中传递数据库令牌。有关 HTTP API 令牌方案的更多信息,请参阅如何验证 API 请求。
以下示例使用 cURL 和 InfluxDB v2 API 将 Line Protocol 写入 InfluxDB
response=$(curl --silent --write-out "%{response_code}:-%{errormsg}" \
"https://cluster-host.com/api/v2/write?bucket=get-started&precision=s" \
--header "Authorization: Bearer DATABASE_TOKEN" \
--header "Content-Type: text/plain; charset=utf-8" \
--header "Accept: application/json" \
--data-binary "
home,room=Living\ Room temp=21.1,hum=35.9,co=0i 1641024000
home,room=Kitchen temp=21.0,hum=35.9,co=0i 1641024000
home,room=Living\ Room temp=21.4,hum=35.9,co=0i 1641027600
home,room=Kitchen temp=23.0,hum=36.2,co=0i 1641027600
home,room=Living\ Room temp=21.8,hum=36.0,co=0i 1641031200
home,room=Kitchen temp=22.7,hum=36.1,co=0i 1641031200
home,room=Living\ Room temp=22.2,hum=36.0,co=0i 1641034800
home,room=Kitchen temp=22.4,hum=36.0,co=0i 1641034800
home,room=Living\ Room temp=22.2,hum=35.9,co=0i 1641038400
home,room=Kitchen temp=22.5,hum=36.0,co=0i 1641038400
home,room=Living\ Room temp=22.4,hum=36.0,co=0i 1641042000
home,room=Kitchen temp=22.8,hum=36.5,co=1i 1641042000
home,room=Living\ Room temp=22.3,hum=36.1,co=0i 1641045600
home,room=Kitchen temp=22.8,hum=36.3,co=1i 1641045600
home,room=Living\ Room temp=22.3,hum=36.1,co=1i 1641049200
home,room=Kitchen temp=22.7,hum=36.2,co=3i 1641049200
home,room=Living\ Room temp=22.4,hum=36.0,co=4i 1641052800
home,room=Kitchen temp=22.4,hum=36.0,co=7i 1641052800
home,room=Living\ Room temp=22.6,hum=35.9,co=5i 1641056400
home,room=Kitchen temp=22.7,hum=36.0,co=9i 1641056400
home,room=Living\ Room temp=22.8,hum=36.2,co=9i 1641060000
home,room=Kitchen temp=23.3,hum=36.9,co=18i 1641060000
home,room=Living\ Room temp=22.5,hum=36.3,co=14i 1641063600
home,room=Kitchen temp=23.1,hum=36.6,co=22i 1641063600
home,room=Living\ Room temp=22.2,hum=36.4,co=17i 1641067200
home,room=Kitchen temp=22.7,hum=36.5,co=26i 1641067200
")
# Format the response code and error message output.
response_code=${response%%:-*}
errormsg=${response#*:-}
# Remove leading and trailing whitespace from errormsg
errormsg=$(echo "${errormsg}" | tr -d '[:space:]')
echo "$response_code"
if [[ $errormsg ]]; then
echo "$errormsg"
fi
替换以下内容:
DATABASE_TOKEN
:具有足够权限访问指定数据库的数据库令牌
如果成功,输出将是 HTTP 204 No Content
状态代码;否则,输出将是错误状态代码和失败消息。
204
要使用 Python 将数据写入 InfluxDB Clustered,请使用 influxdb_client_3
模块。以下步骤包括设置 Python 虚拟环境,以便将依赖项限定在当前项目中。
创建一个模块目录并导航到该目录,例如:
mkdir -p influxdb_py_client && cd influxdb_py_client
设置您的 Python 虚拟环境。在您的模块目录中,输入以下命令:
python -m venv envs/virtual-env
激活虚拟环境。
source ./envs/virtual-env/bin/activate
安装客户端库包
pip install influxdb3-python
influxdb3-python
包提供influxdb_client_3
模块,并且还安装了pyarrow
包,用于处理从查询返回的 Arrow 数据。在您的终端或编辑器中,为您的代码创建一个新文件,例如:
write.py
。touch write.py
在
write.py
中,输入以下示例代码:from influxdb_client_3 import InfluxDBClient3 import os # INFLUX_TOKEN is an environment variable you assigned to your # database WRITE token value. token = os.getenv('INFLUX_TOKEN') # host is the URL hostname without protocol or trailing slash client = InfluxDBClient3( host='cluster-host.com', org='', token=token, database='get-started' ) lines = [ "home,room=Living\ Room temp=21.1,hum=35.9,co=0i 1641024000", "home,room=Kitchen temp=21.0,hum=35.9,co=0i 1641024000", "home,room=Living\ Room temp=21.4,hum=35.9,co=0i 1641027600", "home,room=Kitchen temp=23.0,hum=36.2,co=0i 1641027600", "home,room=Living\ Room temp=21.8,hum=36.0,co=0i 1641031200", "home,room=Kitchen temp=22.7,hum=36.1,co=0i 1641031200", "home,room=Living\ Room temp=22.2,hum=36.0,co=0i 1641034800", "home,room=Kitchen temp=22.4,hum=36.0,co=0i 1641034800", "home,room=Living\ Room temp=22.2,hum=35.9,co=0i 1641038400", "home,room=Kitchen temp=22.5,hum=36.0,co=0i 1641038400", "home,room=Living\ Room temp=22.4,hum=36.0,co=0i 1641042000", "home,room=Kitchen temp=22.8,hum=36.5,co=1i 1641042000", "home,room=Living\ Room temp=22.3,hum=36.1,co=0i 1641045600", "home,room=Kitchen temp=22.8,hum=36.3,co=1i 1641045600", "home,room=Living\ Room temp=22.3,hum=36.1,co=1i 1641049200", "home,room=Kitchen temp=22.7,hum=36.2,co=3i 1641049200", "home,room=Living\ Room temp=22.4,hum=36.0,co=4i 1641052800", "home,room=Kitchen temp=22.4,hum=36.0,co=7i 1641052800", "home,room=Living\ Room temp=22.6,hum=35.9,co=5i 1641056400", "home,room=Kitchen temp=22.7,hum=36.0,co=9i 1641056400", "home,room=Living\ Room temp=22.8,hum=36.2,co=9i 1641060000", "home,room=Kitchen temp=23.3,hum=36.9,co=18i 1641060000", "home,room=Living\ Room temp=22.5,hum=36.3,co=14i 1641063600", "home,room=Kitchen temp=23.1,hum=36.6,co=22i 1641063600", "home,room=Living\ Room temp=22.2,hum=36.4,co=17i 1641067200", "home,room=Kitchen temp=22.7,hum=36.5,co=26i 1641067200" ] client.write(lines,write_precision='s')
该示例执行以下操作:
从
influxdb_client_3
模块导入InfluxDBClient3
对象。调用
InfluxDBClient3()
构造函数以实例化配置了以下凭据的 InfluxDB 客户端:host
:InfluxDB 集群主机名(不带协议或尾部斜杠的 URL)org
:一个空字符串或任意字符串(InfluxDB 忽略此参数)token
:具有写入访问指定数据库权限的数据库令牌。将其存储在密钥存储或环境变量中,以避免暴露原始令牌字符串。database
:要写入的 InfluxDB Clustered 数据库的名称
定义 Line Protocol 字符串列表,其中每个字符串代表一条数据记录。
使用 Line Protocol 记录列表和写入选项调用
client.write()
方法。由于示例 Line Protocol 中的时间戳精度为秒,因此示例传递
write_precision='s'
选项以将时间戳精度设置为秒。
要执行模块并将 Line Protocol 写入您的 InfluxDB Clustered 数据库,请在您的终端中输入以下命令:
python write.py
如果成功,输出将是成功消息;否则,输出将是错误详细信息和失败消息。
要使用 Go 将数据写入 InfluxDB Clustered,请使用 InfluxDB 3 influxdb3-go 客户端库包。
在您的项目目录中,创建一个新的模块目录并导航到该目录。
mkdir -p influxdb_go_client && cd influxdb_go_client
在该目录中初始化一个新的 Go 模块。
go mod init influxdb_go_client
在您的终端或编辑器中,为您的代码创建一个新文件,例如:
write.go
。touch write.go
在
write.go
中,输入以下示例代码:package main import ( "context" "os" "fmt" "log" "github.com/InfluxCommunity/influxdb3-go/v2/influxdb3" ) // Write line protocol data to InfluxDB func WriteLineProtocol() error { // INFLUX_TOKEN is an environment variable you assigned to your // database WRITE token value. token := os.Getenv("INFLUX_TOKEN") database := os.Getenv("INFLUX_DATABASE") // Initialize a client with URL and token, // and set the timestamp precision for writes. client, err := influxdb3.New(influxdb3.ClientConfig{ Host: "https://cluster-host.com", Token: token, Database: database, WriteOptions: &influxdb3.WriteOptions{Precision: lineprotocol.Second}, }) // Close the client when the function returns. defer func(client *influxdb3.Client) { err := client.Close() if err != nil { panic(err) } }(client) // Define line protocol records to write. // Use a raw string literal (denoted by backticks) // to preserve backslashes and prevent interpretation // of escape sequences--for example, escaped spaces in tag values. lines := [...]string{ `home,room=Living\ Room temp=21.1,hum=35.9,co=0i 1641124000`, `home,room=Kitchen temp=21.0,hum=35.9,co=0i 1641124000`, `home,room=Living\ Room temp=21.4,hum=35.9,co=0i 1641127600`, `home,room=Kitchen temp=23.0,hum=36.2,co=0i 1641127600`, `home,room=Living\ Room temp=21.8,hum=36.0,co=0i 1641131200`, `home,room=Kitchen temp=22.7,hum=36.1,co=0i 1641131200`, `home,room=Living\ Room temp=22.2,hum=36.0,co=0i 1641134800`, `home,room=Kitchen temp=22.4,hum=36.0,co=0i 1641134800`, `home,room=Living\ Room temp=22.2,hum=35.9,co=0i 1641138400`, `home,room=Kitchen temp=22.5,hum=36.0,co=0i 1641138400`, `home,room=Living\ Room temp=22.4,hum=36.0,co=0i 1641142000`, `home,room=Kitchen temp=22.8,hum=36.5,co=1i 1641142000`, `home,room=Living\ Room temp=22.3,hum=36.1,co=0i 1641145600`, `home,room=Kitchen temp=22.8,hum=36.3,co=1i 1641145600`, `home,room=Living\ Room temp=22.3,hum=36.1,co=1i 1641149200`, `home,room=Kitchen temp=22.7,hum=36.2,co=3i 1641149200`, `home,room=Living\ Room temp=22.4,hum=36.0,co=4i 1641152800`, `home,room=Kitchen temp=22.4,hum=36.0,co=7i 1641152800`, `home,room=Living\ Room temp=22.6,hum=35.9,co=5i 1641156400`, `home,room=Kitchen temp=22.7,hum=36.0,co=9i 1641156400`, `home,room=Living\ Room temp=22.8,hum=36.2,co=9i 1641160000`, `home,room=Kitchen temp=23.3,hum=36.9,co=18i 1641160000`, `home,room=Living\ Room temp=22.5,hum=36.3,co=14i 1641163600`, `home,room=Kitchen temp=23.1,hum=36.6,co=22i 1641163600`, `home,room=Living\ Room temp=22.2,hum=36.4,co=17i 1641167200`, `home,room=Kitchen temp=22.7,hum=36.5,co=26i 1641167200`, } // Iterate over the lines array and write each line // separately to InfluxDB for _, record := range lines { err = client.Write(context.Background(), []byte(record)) if err != nil { log.Fatalf("Error writing line protocol: %v", err) } } if err != nil { panic(err) } fmt.Println("Data has been written successfully.") return nil }
该示例执行以下操作:
导入所需的包。
定义一个
WriteLineProtocol()
函数,该函数执行以下操作:要实例化客户端,请调用
influxdb3.New(influxdb3.ClientConfig)
函数并传递以下内容:定义一个延迟函数,该函数在函数返回时关闭客户端。
定义 Line Protocol 字符串数组,其中每个字符串代表一条数据记录。
迭代 Line Protocol 数组,并调用写入客户端的
Write()
方法,将 Line Protocol 的每一行分别写入 InfluxDB。
在编辑器中,创建一个
main.go
文件,并输入以下示例代码来调用WriteLineProtocol()
函数:package main // Module main function func main() { WriteLineProtocol() }
要安装依赖项并将数据写入您的 InfluxDB Clustered 数据库,请在终端中输入以下命令:
go mod tidy && go run influxdb_go_client
如果成功,输出将是成功消息;否则,输出将是错误详细信息和失败消息。
如果您尚未执行此操作,请按照 下载并安装 Node.js 和 npm 的说明进行操作。
在您的终端中,输入以下命令为您的项目创建一个
influxdb_js_client
目录:mkdir influxdb_js_client && cd influxdb_js_client
在
influxdb_js_client
中,输入以下命令以初始化包。此示例将包配置为使用 ECMAScript 模块 (ESM)。npm init -y; npm pkg set type="module"
安装
@influxdata/influxdb3-client
JavaScript 客户端库作为您项目的依赖项。npm install --save @influxdata/influxdb3-client
在您的终端或编辑器中,创建一个
write.js
文件。touch write.js
在
write.js
中,输入以下示例代码:// write.js import { InfluxDBClient } from '@influxdata/influxdb3-client'; /** * Set InfluxDB credentials. */ const host = 'cluster-host.com'; const database = 'get-started'; /** * INFLUX_TOKEN is an environment variable you assigned to your * WRITE token value. */ const token = process.env.INFLUX_TOKEN; /** * Write line protocol to InfluxDB using the JavaScript client library. */ export async function writeLineProtocol() { /** * Instantiate an InfluxDBClient */ const client = new InfluxDBClient({ host, token }); /** * Define line protocol records to write. */ const records = [ `home,room=Living\\ Room temp=21.1,hum=35.9,co=0i 1641124000`, `home,room=Kitchen temp=21.0,hum=35.9,co=0i 1641124000`, `home,room=Living\\ Room temp=21.4,hum=35.9,co=0i 1641127600`, `home,room=Kitchen temp=23.0,hum=36.2,co=0 1641127600`, `home,room=Living\\ Room temp=21.8,hum=36.0,co=0i 1641131200`, `home,room=Kitchen temp=22.7,hum=36.1,co=0i 1641131200`, `home,room=Living\\ Room temp=22.2,hum=36.0,co=0i 1641134800`, `home,room=Kitchen temp=22.4,hum=36.0,co=0i 1641134800`, `home,room=Living\\ Room temp=22.2,hum=35.9,co=0i 1641138400`, `home,room=Kitchen temp=22.5,hum=36.0,co=0i 1641138400`, `home,room=Living\\ Room temp=22.4,hum=36.0,co=0i 1641142000`, `home,room=Kitchen temp=22.8,hum=36.5,co=1i 1641142000`, `home,room=Living\\ Room temp=22.3,hum=36.1,co=0i 1641145600`, `home,room=Kitchen temp=22.8,hum=36.3,co=1i 1641145600`, `home,room=Living\\ Room temp=22.3,hum=36.1,co=1i 1641149200`, `home,room=Kitchen temp=22.7,hum=36.2,co=3i 1641149200`, `home,room=Living\\ Room temp=22.4,hum=36.0,co=4i 1641152800`, `home,room=Kitchen temp=22.4,hum=36.0,co=7i 1641152800`, `home,room=Living\\ Room temp=22.6,hum=35.9,co=5i 1641156400`, `home,room=Kitchen temp=22.7,hum=36.0,co=9i 1641156400`, `home,room=Living\\ Room temp=22.8,hum=36.2,co=9i 1641160000`, `home,room=Kitchen temp=23.3,hum=36.9,co=18i 1641160000`, `home,room=Living\\ Room temp=22.5,hum=36.3,co=14i 1641163600`, `home,room=Kitchen temp=23.1,hum=36.6,co=22i 1641163600`, `home,room=Living\\ Room temp=22.2,hum=36.4,co=17i 1641167200`, `home,room=Kitchen temp=22.7,hum=36.5,co=26i 1641167200`, ]; /** * Creates an array that contains separate write request promises * for all the records. */ const writePromises = records.map((record) => { return client.write(record, database, '', { precision: 's' }).then( () => `Data has been written successfully: ${record}`, () => `Failed writing data: ${record}` ); }); /** * Wait for all the write promises to settle, and then output the results. */ const writeResults = await Promise.allSettled(writePromises); writeResults.forEach((write) => console.log(write.value)); /** Close the client to release resources. */ await client.close(); }
示例代码执行以下操作:
导入
InfluxDBClient
类。调用
new InfluxDBClient()
构造函数并传递ClientOptions
对象,以实例化配置了 InfluxDB 凭据的客户端。host
:您的 InfluxDB 集群 URLtoken
:具有对指定数据库写入权限的数据库令牌。将其存储在密钥存储或环境变量中,以避免暴露原始令牌字符串。
定义 Line Protocol 字符串列表,其中每个字符串代表一条数据记录。
为每个记录调用客户端的
write()
方法,定义要返回的成功或失败消息,并将挂起的 Promise 收集到writePromises
数组中。每次调用write()
都会传递以下参数:record
:Line Protocol 记录database
:要写入的 InfluxDB Clustered 数据库的名称{precision}
:一个WriteOptions
对象,用于设置precision
值。
由于示例 Line Protocol 中的时间戳精度为秒,因此示例传递
s
作为precision
值,以将写入时间戳精度设置为秒。使用 Promise 数组调用
Promise.allSettled()
以暂停执行,直到 Promise 完成,然后将包含成功和失败消息的数组分配给writeResults
常量。迭代并打印
writeResults
中的消息。关闭客户端以释放资源。
在您的终端或编辑器中,创建一个
index.js
文件。在
index.js
中,输入以下示例代码以导入和调用writeLineProtocol()
:// index.js import { writeLineProtocol } from './write.js'; /** * Execute the client functions. */ async function main() { /** Write line protocol data to InfluxDB. */ await writeLineProtocol(); } main();
在您的终端中,执行
index.js
以写入 InfluxDB Clusterednode index.js
如果成功,输出将是成功消息;否则,输出将是错误详细信息和失败消息。
如果您尚未执行此操作,请按照 Microsoft.com 下载说明 安装 .NET 和
dotnet
CLI。在您的终端中,使用 .NET 控制台 模板创建一个可执行的 C# 项目。
dotnet new console --name influxdb_csharp_client
更改到生成的
influxdb_csharp_client
目录。cd influxdb_csharp_client
运行以下命令以安装最新版本的 InfluxDB 3 C# 客户端库。
dotnet add package InfluxDB3.Client
在您的编辑器中,创建一个
Write.cs
文件,并输入以下示例代码:// Write.cs using System; using System.Threading.Tasks; using InfluxDB3.Client; using InfluxDB3.Client.Query; namespace InfluxDBv3; public class Write { /** * Writes line protocol to InfluxDB using the C# .NET client * library. */ public static async Task WriteLines() { // Set InfluxDB credentials const string host = "https://cluster-host.com"; string? database = "get-started"; /** * INFLUX_TOKEN is an environment variable you assigned to your * WRITE token value. */ string? token = System.Environment .GetEnvironmentVariable("INFLUX_TOKEN"); // Instantiate the InfluxDB client with credentials. using var client = new InfluxDBClient( host, token: token, database: database); /** * Define an array of line protocol strings to write. * Include an additional backslash to preserve backslashes * and prevent interpretation of escape sequences---for example, * escaped spaces in tag values. */ string[] lines = new string[] { "home,room=Living\\ Room temp=21.1,hum=35.9,co=0i 1641024000", "home,room=Kitchen temp=21.0,hum=35.9,co=0i 1641024000", "home,room=Living\\ Room temp=21.4,hum=35.9,co=0i 1641027600", "home,room=Kitchen temp=23.0,hum=36.2,co=0i 1641027600", "home,room=Living\\ Room temp=21.8,hum=36.0,co=0i 1641031200", "home,room=Kitchen temp=22.7,hum=36.1,co=0i 1641031200", "home,room=Living\\ Room temp=22.2,hum=36.0,co=0i 1641034800", "home,room=Kitchen temp=22.4,hum=36.0,co=0i 1641034800", "home,room=Living\\ Room temp=22.2,hum=35.9,co=0i 1641038400", "home,room=Kitchen temp=22.5,hum=36.0,co=0i 1641038400", "home,room=Living\\ Room temp=22.4,hum=36.0,co=0i 1641042000", "home,room=Kitchen temp=22.8,hum=36.5,co=1i 1641042000", "home,room=Living\\ Room temp=22.3,hum=36.1,co=0i 1641045600", "home,room=Kitchen temp=22.8,hum=36.3,co=1i 1641045600", "home,room=Living\\ Room temp=22.3,hum=36.1,co=1i 1641049200", "home,room=Kitchen temp=22.7,hum=36.2,co=3i 1641049200", "home,room=Living\\ Room temp=22.4,hum=36.0,co=4i 1641052800", "home,room=Kitchen temp=22.4,hum=36.0,co=7i 1641052800", "home,room=Living\\ Room temp=22.6,hum=35.9,co=5i 1641056400", "home,room=Kitchen temp=22.7,hum=36.0,co=9i 1641056400", "home,room=Living\\ Room temp=22.8,hum=36.2,co=9i 1641060000", "home,room=Kitchen temp=23.3,hum=36.9,co=18i 1641060000", "home,room=Living\\ Room temp=22.5,hum=36.3,co=14i 1641063600", "home,room=Kitchen temp=23.1,hum=36.6,co=22i 1641063600", "home,room=Living\\ Room temp=22.2,hum=36.4,co=17i 1641067200", "home,room=Kitchen temp=22.7,hum=36.5,co=26i 1641067200" }; // Write each record separately. foreach (string line in lines) { // Write the record to InfluxDB with timestamp precision in seconds. await client.WriteRecordAsync( record: line, precision: WritePrecision.S); Console.WriteLine( "Data has been written successfully: {0,-30}", line); } } }
该示例执行以下操作:
调用
new InfluxDBClient()
构造函数以实例化配置了 InfluxDB 凭据的客户端。host
:您的 InfluxDB 集群 URLdatabase
:要写入的 InfluxDB Clustered 数据库的名称token
:具有对指定数据库写入权限的数据库令牌。将其存储在密钥存储或环境变量中,以避免暴露原始令牌字符串。
using
语句确保程序在不再需要客户端时将其释放。定义 Line Protocol 字符串数组,其中每个字符串代表一条数据记录。
调用客户端的
WriteRecordAsync()
方法以将每个 Line Protocol 记录写入 InfluxDB。由于示例 Line Protocol 中的时间戳精度为秒,因此示例将
WritePrecision.S
枚举值 传递给precision:
选项,以将时间戳精度设置为秒。
在您的编辑器中,打开
Program.cs
文件,并将其内容替换为以下内容:// Program.cs using System; using System.Threading.Tasks; namespace InfluxDBv3; public class Program { public static async Task Main() { await Write.WriteLineProtocol(); } }
Program
类与您在上一步中定义的Write
类共享相同的InfluxDBv3
命名空间,并定义了一个调用Write.WriteLineProtocol()
的Main()
函数。dotnet
CLI 将Program.Main()
识别为程序的入口点。要构建和执行程序,并将 Line Protocol 写入您的 InfluxDB Clustered 数据库,请在终端中输入以下命令:
dotnet run
如果成功,输出将是成功消息;否则,输出将是错误详细信息和失败消息。
本教程假设使用 Maven 版本 3.9 和 Java 版本 >= 15。
在您的终端或编辑器中,使用 Maven 生成一个项目,例如:
mvn org.apache.maven.plugins:maven-archetype-plugin:3.1.2:generate \ -DarchetypeArtifactId="maven-archetype-quickstart" \ -DarchetypeGroupId="org.apache.maven.archetypes" -DarchetypeVersion="1.4" \ -DgroupId="com.influxdbv3" -DartifactId="influxdb_java_client" -Dversion="1.0"
Maven 创建
<artifactId>
目录 (./influxdb_java_client
),其中包含pom.xml
和您的com.influxdbv3.influxdb_java_client
Java 应用程序的支架。在您的终端或编辑器中,更改到
./influxdb_java_client
目录,例如:cd ./influxdb_java_client
在编辑器中,打开
pom.xml
Maven 配置文件,并将com.influxdb.influxdb3-java
客户端库添加到dependencies
中。... <dependencies> ... <dependency> <groupId>com.influxdb</groupId> <artifactId>influxdb3-java</artifactId> <version>0.1.0</version> </dependency> ... </dependencies>
要检查您的
pom.xml
是否有问题,请运行 Maven 的validate
命令,例如,在您的终端中输入以下内容:mvn validate
在编辑器中,导航到
./influxdb_java_client/src/main/java/com/influxdbv3
目录并创建一个Write.java
文件。在
Write.java
中,输入以下示例代码:// Write.java package com.influxdbv3; import java.util.List; import com.influxdb.v3.client.InfluxDBClient; import com.influxdb.v3.client.write.WriteOptions; import com.influxdb.v3.client.write.WritePrecision; /** * Writes line protocol to InfluxDB using the Java client * library. */ public final class Write { /** * Write data to InfluxDB 3. */ private Write() { //not called } /** * @throws Exception */ public static void writeLineProtocol() throws Exception { // Set InfluxDB credentials final String host = "https://cluster-host.com"; final String database = "get-started"; /** * INFLUX_TOKEN is an environment variable you assigned to your * WRITE token value. */ final char[] token = (System.getenv("INFLUX_TOKEN")). toCharArray(); // Instantiate the InfluxDB client. try (InfluxDBClient client = InfluxDBClient.getInstance(host, token, database)) { // Create a list of line protocol records. final List<String> records = List.of( "home,room=Living\\ Room temp=21.1,hum=35.9,co=0i 1641024000", "home,room=Kitchen temp=21.0,hum=35.9,co=0i 1641024000", "home,room=Living\\ Room temp=21.4,hum=35.9,co=0i 1641027600", "home,room=Kitchen temp=23.0,hum=36.2,co=0i 1641027600", "home,room=Living\\ Room temp=21.8,hum=36.0,co=0i 1641031200", "home,room=Kitchen temp=22.7,hum=36.1,co=0i 1641031200", "home,room=Living\\ Room temp=22.2,hum=36.0,co=0i 1641034800", "home,room=Kitchen temp=22.4,hum=36.0,co=0i 1641034800", "home,room=Living\\ Room temp=22.2,hum=35.9,co=0i 1641038400", "home,room=Kitchen temp=22.5,hum=36.0,co=0i 1641038400", "home,room=Living\\ Room temp=22.4,hum=36.0,co=0i 1641042000", "home,room=Kitchen temp=22.8,hum=36.5,co=1i 1641042000", "home,room=Living\\ Room temp=22.3,hum=36.1,co=0i 1641045600", "home,room=Kitchen temp=22.8,hum=36.3,co=1i 1641045600", "home,room=Living\\ Room temp=22.3,hum=36.1,co=1i 1641049200", "home,room=Kitchen temp=22.7,hum=36.2,co=3i 1641049200", "home,room=Living\\ Room temp=22.4,hum=36.0,co=4i 1641052800", "home,room=Kitchen temp=22.4,hum=36.0,co=7i 1641052800", "home,room=Living\\ Room temp=22.6,hum=35.9,co=5i 1641056400", "home,room=Kitchen temp=22.7,hum=36.0,co=9i 1641056400", "home,room=Living\\ Room temp=22.8,hum=36.2,co=9i 1641060000", "home,room=Kitchen temp=23.3,hum=36.9,co=18i 1641060000", "home,room=Living\\ Room temp=22.5,hum=36.3,co=14i 1641063600", "home,room=Kitchen temp=23.1,hum=36.6,co=22i 1641063600", "home,room=Living\\ Room temp=22.2,hum=36.4,co=17i 1641067200", "home,room=Kitchen temp=22.7,hum=36.5,co=26i 1641067200" ); /** * Write each record separately to InfluxDB with timestamp * precision in seconds. * If no error occurs, print a success message. * */ for (String record : records) { client.writeRecord(record, new WriteOptions(null, null, WritePrecision.S)); System.out.printf("Data has been written successfully: %s%n", record); } } } }
示例代码执行以下操作:
导入以下类:
java.util.List
;com.influxdb.v3.client.InfluxDBClient
com.influxdb.v3.client.write.WriteParameters
com.influxdb.v3.client.write.WritePrecision
调用
InfluxDBClient.getInstance()
以实例化配置了 InfluxDB 凭据的客户端。host
:您的 InfluxDB 集群 URLdatabase
:要写入的 InfluxDB Clustered 数据库的名称token
:具有对指定数据库写入权限的数据库令牌。将其存储在密钥存储或环境变量中,以避免暴露原始令牌字符串。
定义 Line Protocol 字符串列表,其中每个字符串代表一条数据记录。
调用客户端的
writeRecord()
方法以将每个记录分别写入 InfluxDB。由于示例 Line Protocol 中的时间戳精度为秒,因此示例将
WritePrecision.S
枚举值 作为precision
参数传递,以将写入时间戳精度设置为秒。
在您的编辑器中,打开
App.java
文件(由 Maven 创建),并将其内容替换为以下示例代码:// App.java package com.influxdbv3; /** * Execute the client functions. * */ public class App { /** * @param args * @throws Exception */ public static void main(final String[] args) throws Exception { // Write data to InfluxDB 3. Write.writeLineProtocol(); } }
App
类和Write
类是同一com.influxdbv3
包(您的项目 groupId)的一部分。App
定义了一个调用Write.writeLineProtocol()
的main()
函数。
在您的终端或编辑器中,使用 Maven 安装依赖项并编译项目代码,例如:
mvn compile
在您的终端或编辑器中,执行
App.main()
以写入 InfluxDB,例如使用 Maven:mvn exec:java -Dexec.mainClass="com.influxdbv3.App"
如果成功,输出将是成功消息;否则,输出将是错误详细信息和失败消息。
查看写入的数据
time | room | co | hum | temp |
---|---|---|---|---|
2022-01-01T08:00:00Z | Kitchen | 0 | 35.9 | 21 |
2022-01-01T09:00:00Z | Kitchen | 0 | 36.2 | 23 |
2022-01-01T10:00:00Z | Kitchen | 0 | 36.1 | 22.7 |
2022-01-01T11:00:00Z | Kitchen | 0 | 36 | 22.4 |
2022-01-01T12:00:00Z | Kitchen | 0 | 36 | 22.5 |
2022-01-01T13:00:00Z | Kitchen | 1 | 36.5 | 22.8 |
2022-01-01T14:00:00Z | Kitchen | 1 | 36.3 | 22.8 |
2022-01-01T15:00:00Z | Kitchen | 3 | 36.2 | 22.7 |
2022-01-01T16:00:00Z | Kitchen | 7 | 36 | 22.4 |
2022-01-01T17:00:00Z | Kitchen | 9 | 36 | 22.7 |
2022-01-01T18:00:00Z | Kitchen | 18 | 36.9 | 23.3 |
2022-01-01T19:00:00Z | Kitchen | 22 | 36.6 | 23.1 |
2022-01-01T20:00:00Z | Kitchen | 26 | 36.5 | 22.7 |
2022-01-01T08:00:00Z | Living Room | 0 | 35.9 | 21.1 |
2022-01-01T09:00:00Z | Living Room | 0 | 35.9 | 21.4 |
2022-01-01T10:00:00Z | Living Room | 0 | 36 | 21.8 |
2022-01-01T11:00:00Z | Living Room | 0 | 36 | 22.2 |
2022-01-01T12:00:00Z | Living Room | 0 | 35.9 | 22.2 |
2022-01-01T13:00:00Z | Living Room | 0 | 36 | 22.4 |
2022-01-01T14:00:00Z | Living Room | 0 | 36.1 | 22.3 |
2022-01-01T15:00:00Z | Living Room | 1 | 36.1 | 22.3 |
2022-01-01T16:00:00Z | Living Room | 4 | 36 | 22.4 |
2022-01-01T17:00:00Z | Living Room | 5 | 35.9 | 22.6 |
2022-01-01T18:00:00Z | Living Room | 9 | 36.2 | 22.8 |
2022-01-01T19:00:00Z | Living Room | 14 | 36.3 | 22.5 |
2022-01-01T20:00:00Z | Living Room | 17 | 36.4 | 22.2 |
恭喜! 您已将数据写入 InfluxDB。接下来,了解如何查询您的数据。
相关内容
此页面是否对您有帮助?
感谢您的反馈!
支持和反馈
感谢您成为我们社区的一份子!我们欢迎并鼓励您提供关于 InfluxDB Clustered 和本文档的反馈和错误报告。要获得支持,请使用以下资源:
拥有年度合同或支持合同的客户可以联系 InfluxData 支持。