开始编写数据
- 2 / 3
本教程将指导您创建行协议数据并将其写入InfluxDB的基本知识。
InfluxDB 提供多种不同的方法来摄取或写入数据,包括以下内容:
- InfluxDB HTTP API(v1和v2)
- Telegraf
influxctl命令行工具influx3数据 CLI- InfluxDB客户端库
如果使用像Telegraf或InfluxDB客户端库这样的工具,它们可以为您构建行协议,但了解行协议是如何工作的很好。
行协议
写入到 InfluxDB 的所有数据都是使用 行协议 写入的,这是一种基于文本的格式,让您可以提供写入数据点到 InfluxDB 所需的信息。 本教程涵盖了行协议的基础知识,但有关详细信息,请参见 行协议参考。
行协议元素
每行行协议包含以下元素:
* Required- * measurement: 一个字符串,用于识别存储数据的 table。
- 标签集: 由逗号分隔的键值对列表,每个代表一个标签。标签的键和值是未加引号的字符串。空格、逗号和等号字符必须被转义。
- * 字段集合: 以逗号分隔的键值对列表,每个代表一个字段。 字段键是未引用的字符串。 空格和逗号必须被转义。 字段值可以是 字符串 (引用的), 浮点数, 整数, 无符号整数, 或 布尔值。
- timestamp: Unix 时间戳 与数据相关。InfluxDB 支持高达纳秒的精度。 如果时间戳的精度不是纳秒,你必须在将数据写入 InfluxDB 时指定精度。
行协议元素解析
- measurement: 在第一个未转义的逗号之前的第一个空格之前的所有内容。
- 标签集: 在第一个未转义的逗号和第一个未转义的空白字符之间的键值对。
- 字段集: 在第一个和第二个未转义的空白字符之间的键值对。
- timestamp: 在第二个未转义空格之后的整数值。
- 行由换行符分隔 (
\n)。行协议对空格敏感。
myMeasurement,tag1=val1,tag2=val2 field1="v1",field2=1i 0000000000000000000
有关架构设计建议,请参阅 InfluxDB 架构设计。
构建行协议
通过对行协议的基本理解,您现在可以构建行协议并将数据写入InfluxDB。考虑一个使用案例,您从家中的传感器收集数据。每个传感器收集温度、湿度和一氧化碳读数。要收集这些数据,请使用以下模式:
- 测量:
home- 标签
room: 客厅或厨房
- 字段
temp: 摄氏度中的温度 (浮点数)hum: 湿度百分比 (浮点数)co: 一百万分之一的空气中一氧化碳含量 (整数)
- 时间戳: Unix 时间戳,单位为秒精度
- 标签
以下行协议示例表示从 2022-01-01T08:00:00Z (UTC) 到 2022-01-01T20:00:00Z (UTC) 每小时收集的数据。
家庭传感器数据线路协议
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
将行协议写入InfluxDB
以下示例展示如何将前面的 sample data,已经是行协议格式, 写入到一个InfluxDB集群数据库中。
要了解更多可用的工具和选项,请参阅 Write data。
本入门教程中的一些示例假设您的 InfluxDB 凭据(URL、organization 和 token)是通过 环境变量 提供的。
使用influxctl write命令将家庭传感器样本数据写入您的InfluxDB集群。提供以下内容:
- 使用
--database标志的数据库名称 - 使用
--token标志的数据库令牌(使用在 入门–设置 InfluxDB 集群 中创建的INFLUX_TOKEN环境变量) - 使用
--precision标志的时间戳精度为秒 (s) - 家庭传感器数据线协议
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 来处理行协议, 然后将其写入 InfluxDB 集群。
如果您还没有,请按照说明下载并安装 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 集群 指南。
要使用InfluxDB v1 HTTP API将数据写入InfluxDB,使用POST请求方法向InfluxDB API /write端点发送请求。
POST https://cluster-host.com/write
请在您的请求中包含以下内容:
- 请求头:
- 授权: Bearer
- 内容类型: text/plain; charset=utf-8
- 接受: application/json
- 授权: Bearer
- 查询参数:
- db: InfluxDB 数据库名称
- precision:时间戳精度 (默认是
ns)
- 请求体: 行协议作为纯文本
使用 InfluxDB 集群
v1 API /write 端点,
Authorization: Bearer 和 Authorization: Token 是等价的,您可以
使用任一方案在您的请求中传递数据库令牌。有关 HTTP API 令牌方案的更多信息,请参阅如何
验证 API 请求。
以下示例使用 cURL 和 InfluxDB v1 API 将行协议写入 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
请在您的请求中包含以下内容:
- 头部:
- 授权: Bearer
- 内容类型: text/plain; charset=utf-8
- 接受: application/json
- 授权: Bearer
- 查询参数:
- bucket: InfluxDB 数据库名称
- precision:时间戳精度 (默认值是
ns)
- 请求体: 行协议作为纯文本
InfluxDB 集群 v2 API /api/v2/write 端点支持
Bearer 和 Token 授权方案,您可以使用任一方案在请求中传递
数据库令牌。
有关 HTTP API 令牌
方案的更多信息,请参见如何
验证 API 请求。
以下示例使用cURL和InfluxDB v2 API将行协议写入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集群,可以使用
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-pythoninfluxdb3-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集群数据库的名称
定义了一组行协议字符串,其中每个字符串表示一个数据记录。
使用行协议记录列表和写入选项调用
client.write()方法。由于示例行协议中的时间戳为秒级精度,因此示例传递了
write_precision='s'选项以将 时间戳精度 设置为秒。
要执行模块并将行协议写入您的 InfluxDB 集群数据库,请在终端中输入以下命令:
python write.py
如果成功,输出是成功消息;否则,错误详情和失败消息。
要使用Go写入数据到InfluxDB集群,请使用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)函数并传递以下内容:定义一个延迟函数,当函数返回时关闭客户端。
定义一个行协议字符串的数组,其中每个字符串表示一个数据记录。
遍历线协议的数组,并调用写入客户端的
Write()方法将每一行线协议单独写入 InfluxDB。
在你的编辑器中,创建一个
main.go文件并输入以下示例代码,该代码调用WriteLineProtocol()函数:package main // Module main function func main() { WriteLineProtocol() }要安装依赖项并将数据写入您的 InfluxDB 集群数据库,请在终端中输入以下命令:
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-clientJavaScript客户端库安装为您项目的依赖项。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: 一个 数据库令牌 具有对指定数据库的写入访问权限。将其存储在秘密存储或环境变量中,以避免暴露原始令牌字符串。
定义了一组行协议字符串,其中每个字符串表示一个数据记录。
为每条记录调用客户端的
write()方法,定义成功或失败的消息返回,并将待处理的承诺收集到writePromises数组中。每次调用write()都传递以下参数:record: 行协议记录database: 要写入的InfluxDB集群数据库的名称{precision}: 一个WriteOptions对象,用于设置precision值。
因为示例行协议中的时间戳精度为秒,示例将
s作为precision值传递,以将写入 时间戳精度 设置为秒。调用
Promise.allSettled()与 promises 数组来暂停执行,直到 promises 完成,然后将包含成功和失败消息的数组分配给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 集群:node index.js
如果成功,输出是成功消息;否则,错误详情和失败消息。
如果您还没有,请按照Microsoft.com下载说明安装.NET和
dotnetCLI。在你的终端中,使用 .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集群数据库的名称token: 一个 数据库令牌 具有对指定数据库的写入访问权限。将其存储在秘密存储或环境变量中,以避免暴露原始令牌字符串。
使用
using语句可以确保程序在客户端不再需要时进行处理。定义一个行协议字符串的数组,其中每个字符串表示一个数据记录。
调用客户端的
WriteRecordAsync()方法将每行协议记录写入 InfluxDB。因为示例行协议中的时间戳精度为秒,因此示例将
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命名空间,并定义了一个Main()函数,该函数调用Write.WriteLineProtocol()。dotnetCLI 将Program.Main()识别为您的程序的入口点。要构建并执行该程序并将行协议写入您的 InfluxDB 集群数据库,请在终端中输入以下命令:
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 创建了
目录 (./influxdb_java_client),该目录包含一个pom.xml和用于你的com.influxdbv3.influxdb_java_clientJava 应用程序的构建框架。在您的终端或编辑器中,切换到
./influxdb_java_client目录 - 例如:cd ./influxdb_java_client在你的编辑器中,打开
pom.xmlMaven配置文件,并将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.InfluxDBClientcom.influxdb.v3.client.write.WriteParameterscom.influxdb.v3.client.write.WritePrecision
调用
InfluxDBClient.getInstance()来实例化一个配置了 InfluxDB 凭据的客户端。host: 你的 InfluxDB 集群 URLdatabase: 要写入的InfluxDB集群数据库的名称token: 一个 数据库令牌 具有对指定数据库的写入访问权限。将其存储在秘密存储或环境变量中,以避免暴露原始令牌字符串。
定义了一组行协议字符串,其中每个字符串表示一个数据记录。
调用客户端的
writeRecord()方法将每条记录单独写入 InfluxDB。因为示例行协议中的时间戳是以秒为精度的,因此该示例将
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定义了一个main()函数,它调用Write.writeLineProtocol()。
在您的终端或编辑器中,使用Maven安装依赖并编译项目代码——例如:
mvn compile在你的终端或编辑器中,执行
App.main()来写入 InfluxDB——例如,使用 Maven:mvn exec:java -Dexec.mainClass="com.influxdbv3.App"
如果成功,输出是成功消息;否则,错误详情和失败消息。
查看写入的数据
| 时间 | 房间 | 二氧化碳 | 湿度 | 温度 |
|---|---|---|---|---|
| 2022-01-01T08:00:00Z | 厨房 | 0 | 35.9 | 21 |
| 2022-01-01T09:00:00Z | 厨房 | 0 | 36.2 | 23 |
| 2022-01-01T10:00:00Z | 厨房 | 0 | 36.1 | 22.7 |
| 2022-01-01T11:00:00Z | 厨房 | 0 | 36 | 22.4 |
| 2022-01-01T12:00:00Z | 厨房 | 0 | 36 | 22.5 |
| 2022-01-01T13:00:00Z | 厨房 | 1 | 36.5 | 22.8 |
| 2022-01-01T14:00:00Z | 厨房 | 1 | 36.3 | 22.8 |
| 2022-01-01T15:00:00Z | 厨房 | 3 | 36.2 | 22.7 |
| 2022-01-01T16:00:00Z | 厨房 | 7 | 36 | 22.4 |
| 2022-01-01T17:00:00Z | 厨房 | 9 | 36 | 22.7 |
| 2022-01-01T18:00:00Z | 厨房 | 18 | 36.9 | 23.3 |
| 2022-01-01T19:00:00Z | 厨房 | 22 | 36.6 | 23.1 |
| 2022-01-01T20:00:00Z | 厨房 | 26 | 36.5 | 22.7 |
| 2022-01-01T08:00:00Z | 客厅 | 0 | 35.9 | 21.1 |
| 2022-01-01T09:00:00Z | 客厅 | 0 | 35.9 | 21.4 |
| 2022-01-01T10:00:00Z | 客厅 | 0 | 36 | 21.8 |
| 2022-01-01T11:00:00Z | 客厅 | 0 | 36 | 22.2 |
| 2022-01-01T12:00:00Z | 客厅 | 0 | 35.9 | 22.2 |
| 2022-01-01T13:00:00Z | 客厅 | 0 | 36 | 22.4 |
| 2022-01-01T14:00:00Z | 客厅 | 0 | 36.1 | 22.3 |
| 2022-01-01T15:00:00Z | 客厅 | 1 | 36.1 | 22.3 |
| 2022-01-01T16:00:00Z | 客厅 | 4 | 36 | 22.4 |
| 2022-01-01T17:00:00Z | 客厅 | 5 | 35.9 | 22.6 |
| 2022-01-01T18:00:00Z | 客厅 | 9 | 36.2 | 22.8 |
| 2022-01-01T19:00:00Z | 客厅 | 14 | 36.3 | 22.5 |
| 2022-01-01T20:00:00Z | 客厅 | 17 | 36.4 | 22.2 |
恭喜你! 你已经将数据写入InfluxDB。接下来,学习如何查询你的数据。