开始编写数据
- 2 / 3
本教程将指导您创建行协议数据并将其写入InfluxDB的基本知识。
InfluxDB 提供多种不同的方法来摄取或写入数据,包括以下内容:
- Influx 用户界面 (UI)
- InfluxDB HTTP API(v1和v2)
- Telegraf
influx3数据 CLI- InfluxDB客户端库
influx命令行界面
如果使用像Telegraf或InfluxDB客户端库这样的工具,它们可以为您构建行协议,但了解行协议是如何工作的很好。
行协议
所有写入 InfluxDB 的数据都是使用 行协议 进行写入的,这是一种基于文本的格式,可以让您提供所需的信息以将数据点写入 InfluxDB。本教程涵盖了行协议的基础知识,但有关详细信息,请参见 行协议参考。
行协议元素
每行行协议包含以下元素:
* Required- * measurement: 用于标识存储数据的measurement的字符串。
- 标签集: 以逗号分隔的键值对列表,每个代表一个标签。标签键和值为未加引号的字符串。 空格、逗号和等号字符必须被转义。
- * 字段集: 逗号分隔的键值对列表,每个表示一个字段。 字段键是未加引号的字符串。 空格和逗号必须转义。 字段值可以是 字符串 (带引号), 浮点数, 整数, 无符号整数, 或 布尔值。
- 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 1719924000
home,room=Kitchen temp=21.0,hum=35.9,co=0i 1719924000
home,room=Living\ Room temp=21.4,hum=35.9,co=0i 1719927600
home,room=Kitchen temp=23.0,hum=36.2,co=0i 1719927600
home,room=Living\ Room temp=21.8,hum=36.0,co=0i 1719931200
home,room=Kitchen temp=22.7,hum=36.1,co=0i 1719931200
home,room=Living\ Room temp=22.2,hum=36.0,co=0i 1719934800
home,room=Kitchen temp=22.4,hum=36.0,co=0i 1719934800
home,room=Living\ Room temp=22.2,hum=35.9,co=0i 1719938400
home,room=Kitchen temp=22.5,hum=36.0,co=0i 1719938400
home,room=Living\ Room temp=22.4,hum=36.0,co=0i 1719942000
home,room=Kitchen temp=22.8,hum=36.5,co=1i 1719942000
home,room=Living\ Room temp=22.3,hum=36.1,co=0i 1719945600
home,room=Kitchen temp=22.8,hum=36.3,co=1i 1719945600
home,room=Living\ Room temp=22.3,hum=36.1,co=1i 1719949200
home,room=Kitchen temp=22.7,hum=36.2,co=3i 1719949200
home,room=Living\ Room temp=22.4,hum=36.0,co=4i 1719952800
home,room=Kitchen temp=22.4,hum=36.0,co=7i 1719952800
home,room=Living\ Room temp=22.6,hum=35.9,co=5i 1719956400
home,room=Kitchen temp=22.7,hum=36.0,co=9i 1719956400
home,room=Living\ Room temp=22.8,hum=36.2,co=9i 1719960000
home,room=Kitchen temp=23.3,hum=36.9,co=18i 1719960000
home,room=Living\ Room temp=22.5,hum=36.3,co=14i 1719963600
home,room=Kitchen temp=23.1,hum=36.6,co=22i 1719963600
home,room=Living\ Room temp=22.2,hum=36.4,co=17i 1719967200
home,room=Kitchen temp=22.7,hum=36.5,co=26i 1719967200
将行协议写入InfluxDB
以下示例展示了如何将前面的 示例数据,已经以行协议格式, 写入InfluxDB Cloud Serverless存储桶。
要了解更多可用的工具和选项,请参阅 Write data。
本入门教程中的一些示例假设您的 InfluxDB 凭据(URL、organization 和 token)是通过 环境变量 提供的。
在浏览器中访问 cloud2.influxdata.com 登录并访问 InfluxDB 用户界面。
使用左侧导航栏导航到 加载数据 > 桶。
加载数据
- 点击 添加数据 到您想要写入数据的桶,然后选择 行协议。
- 选择 手动输入。
- 重要 在上面的精度下拉菜单中,选择秒(以匹配行协议中的时间戳精度)。
- 复制上面的 行协议 并粘贴到行协议文本字段中。
- 点击 写入数据.
用户界面确认数据已成功写入。
如果您还没有,请下载、安装并配置
influxCLI。使用
influx write命令将前面的线协议写入InfluxDB。提供以下内容:
influx write \
--bucket get-started \
--precision s "
home,room=Living\ Room temp=21.1,hum=35.9,co=0i 1719924000
home,room=Kitchen temp=21.0,hum=35.9,co=0i 1719924000
home,room=Living\ Room temp=21.4,hum=35.9,co=0i 1719927600
home,room=Kitchen temp=23.0,hum=36.2,co=0i 1719927600
home,room=Living\ Room temp=21.8,hum=36.0,co=0i 1719931200
home,room=Kitchen temp=22.7,hum=36.1,co=0i 1719931200
home,room=Living\ Room temp=22.2,hum=36.0,co=0i 1719934800
home,room=Kitchen temp=22.4,hum=36.0,co=0i 1719934800
home,room=Living\ Room temp=22.2,hum=35.9,co=0i 1719938400
home,room=Kitchen temp=22.5,hum=36.0,co=0i 1719938400
home,room=Living\ Room temp=22.4,hum=36.0,co=0i 1719942000
home,room=Kitchen temp=22.8,hum=36.5,co=1i 1719942000
home,room=Living\ Room temp=22.3,hum=36.1,co=0i 1719945600
home,room=Kitchen temp=22.8,hum=36.3,co=1i 1719945600
home,room=Living\ Room temp=22.3,hum=36.1,co=1i 1719949200
home,room=Kitchen temp=22.7,hum=36.2,co=3i 1719949200
home,room=Living\ Room temp=22.4,hum=36.0,co=4i 1719952800
home,room=Kitchen temp=22.4,hum=36.0,co=7i 1719952800
home,room=Living\ Room temp=22.6,hum=35.9,co=5i 1719956400
home,room=Kitchen temp=22.7,hum=36.0,co=9i 1719956400
home,room=Living\ Room temp=22.8,hum=36.2,co=9i 1719960000
home,room=Kitchen temp=23.3,hum=36.9,co=18i 1719960000
home,room=Living\ Room temp=22.5,hum=36.3,co=14i 1719963600
home,room=Kitchen temp=23.1,hum=36.6,co=22i 1719963600
home,room=Living\ Room temp=22.2,hum=36.4,co=17i 1719967200
home,room=Kitchen temp=22.7,hum=36.5,co=26i 1719967200
"
如果成功,输出是成功消息;否则,错误详情和失败消息。
使用 Telegraf 来消费行协议,然后将其写入 InfluxDB Cloud Serverless。
如果您还没有,请按照说明下载并安装 Telegraf。
复制并保存家庭传感器数据示例到您本地系统的文件中 - 例如,
home.lp。cat <<- EOF > home.lp home,room=Living\ Room temp=21.1,hum=35.9,co=0i 1719924000 home,room=Kitchen temp=21.0,hum=35.9,co=0i 1719924000 home,room=Living\ Room temp=21.4,hum=35.9,co=0i 1719927600 home,room=Kitchen temp=23.0,hum=36.2,co=0i 1719927600 home,room=Living\ Room temp=21.8,hum=36.0,co=0i 1719931200 home,room=Kitchen temp=22.7,hum=36.1,co=0i 1719931200 home,room=Living\ Room temp=22.2,hum=36.0,co=0i 1719934800 home,room=Kitchen temp=22.4,hum=36.0,co=0i 1719934800 home,room=Living\ Room temp=22.2,hum=35.9,co=0i 1719938400 home,room=Kitchen temp=22.5,hum=36.0,co=0i 1719938400 home,room=Living\ Room temp=22.4,hum=36.0,co=0i 1719942000 home,room=Kitchen temp=22.8,hum=36.5,co=1i 1719942000 home,room=Living\ Room temp=22.3,hum=36.1,co=0i 1719945600 home,room=Kitchen temp=22.8,hum=36.3,co=1i 1719945600 home,room=Living\ Room temp=22.3,hum=36.1,co=1i 1719949200 home,room=Kitchen temp=22.7,hum=36.2,co=3i 1719949200 home,room=Living\ Room temp=22.4,hum=36.0,co=4i 1719952800 home,room=Kitchen temp=22.4,hum=36.0,co=7i 1719952800 home,room=Living\ Room temp=22.6,hum=35.9,co=5i 1719956400 home,room=Kitchen temp=22.7,hum=36.0,co=9i 1719956400 home,room=Living\ Room temp=22.8,hum=36.2,co=9i 1719960000 home,room=Kitchen temp=23.3,hum=36.9,co=18i 1719960000 home,room=Living\ Room temp=22.5,hum=36.3,co=14i 1719963600 home,room=Kitchen temp=23.1,hum=36.6,co=22i 1719963600 home,room=Living\ Room temp=22.2,hum=36.4,co=17i 1719967200 home,room=Kitchen temp=22.7,hum=36.5,co=26i 1719967200 EOF运行以下命令以生成一个 Telegraf 配置文件 (
./telegraf.conf),该文件启用inputs.file和outputs.influxdb_v2插件:telegraf --sample-config \ --input-filter file \ --output-filter influxdb_v2 \ > telegraf.conf在您的编辑器中,打开
./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"]output-influxdb_v2输出插件: 在[[outputs.influxdb_v2]]部分,将默认值替换为以下您 InfluxDB Cloud Serverless 存储桶的配置:[[outputs.influxdb_v2]] # InfluxDB Cloud Serverless URL urls = ["${INFLUX_HOST}"] # INFLUX_TOKEN is an environment variable you assigned to your API token token = "${INFLUX_TOKEN}" # An empty string (InfluxDB ignores this parameter) organization = "" # Bucket name bucket = "get-started"示例配置使用以下InfluxDB凭据:
urls: an array containing yourINFLUX_HOSTenvironment variabletoken: yourINFLUX_TOKENenvironment variableorganization: an empty string (InfluxDB ignores this parameter)bucket: the name of the bucket to write to
要写入数据,请使用以下选项启动
telegraf守护进程:--config: 指定配置文件的路径。--once: 运行一次配置的输入和输出的Telegraf收集循环,然后退出。
在您的终端中输入以下命令:
telegraf --once --config ./telegraf.conf如果写入成功,输出类似于以下内容:
2023-05-31T20:09:08Z D! [agent] Starting service inputs 2023-05-31T20:09:19Z D! [outputs.influxdb_v2] Wrote batch of 52 metrics in 348.008167ms 2023-05-31T20:09:19Z D! [outputs.influxdb_v2] Buffer fullness: 0 / 10000 metrics
Telegraf及其插件提供了多种读取和写入数据的选项。要了解更多,请查看如何 使用Telegraf写入数据。
使用您现有的工作负载写入数据,这些工作负载已经使用 InfluxDB v1 /write API 端点。
如果从 InfluxDB 1.x 迁移数据,请参阅 从 InfluxDB 1.x 迁移数据到 InfluxDB InfluxDB Cloud Serverless 指南。
要使用InfluxDB v1 HTTP API将数据写入InfluxDB,请发送请求到InfluxDB API /write端点,使用POST请求方法。
POST https://cloud2.influxdata.com/write
请在您的请求中包含以下内容:
- Headers:
- Authorization: Token <INFLUX_TOKEN>
- Content-Type: 文本/纯文本; 字符集=utf-8
- Accept: 应用/json
- 查询参数:
- db: InfluxDB 桶名称
- precision:时间戳精度 (默认是
ns) - rp: 保留策略 名称 (默认是命名空间的默认 DBRP 映射,如果存在;否则,是一个 自动生成的 DBRP 映射).
- 请求体: 行协议作为纯文本
以下示例使用 cURL 和 InfluxDB v1 API 将行协议写入 InfluxDB。 给定 API_TOKEN 是一个 全访问 API 令牌, InfluxDB 创建一个名为 get-started/autogen 的桶和一个 autogen DBRP 映射,然后将数据写入该桶。
response=$(curl --silent --write-out "%{response_code}:-%{errormsg}" \
"https://cloud2.influxdata.com/write?db=get-started&precision=s" \
--header "Authorization: Token API_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 "$response"
fi
替换以下内容:
API_TOKEN: 一个具有足够权限的令牌 用于指定的存储桶。 为了让InfluxDB 自动生成DBRP映射,您必须在写入请求中使用 全访问API令牌 。
如果成功,输出为HTTP 204 No Content状态码;否则,出现错误状态码和失败消息。
204
要使用InfluxDB v2 HTTP API将数据写入InfluxDB,请通过使用POST请求方法发送请求到InfluxDB API /api/v2/write端点。
POST https://cloud2.influxdata.com/api/v2/write
请在您的请求中包含以下内容:
- Headers:
- Authorization: Token <INFLUX_TOKEN>
- Content-Type: 文本/纯文本; 字符集=utf-8
- Accept: 应用/json
- 查询参数:
- bucket: InfluxDB 桶名称
- precision: 时间戳精度 (默认是
ns)
- 请求体: 行协议作为纯文本
以下示例使用cURL和InfluxDB v2 API将行协议写入InfluxDB:
response=$(curl --silent --write-out "%{response_code}:-%{errormsg}" \
"https://cloud2.influxdata.com/api/v2/write?bucket=get-started&precision=s" \
--header "Authorization: Token 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
替换以下内容:
API_TOKEN: 一个令牌,具有足够的权限 访问指定的存储桶
如果成功,输出为HTTP 204 No Content状态码;否则,出现错误状态码和失败消息。
204
要使用Python将数据写入InfluxDB Cloud Serverless,请使用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 # API WRITE token value. token = os.getenv('INFLUX_TOKEN') # host is the URL hostname without protocol or trailing slash client = InfluxDBClient3( host='cloud2.influxdata.com', token=token, database='get-started' ) lines = [ "home,room=Living\ Room temp=21.1,hum=35.9,co=0i 1719924000", "home,room=Kitchen temp=21.0,hum=35.9,co=0i 1719924000", "home,room=Living\ Room temp=21.4,hum=35.9,co=0i 1719927600", "home,room=Kitchen temp=23.0,hum=36.2,co=0i 1719927600", "home,room=Living\ Room temp=21.8,hum=36.0,co=0i 1719931200", "home,room=Kitchen temp=22.7,hum=36.1,co=0i 1719931200", "home,room=Living\ Room temp=22.2,hum=36.0,co=0i 1719934800", "home,room=Kitchen temp=22.4,hum=36.0,co=0i 1719934800", "home,room=Living\ Room temp=22.2,hum=35.9,co=0i 1719938400", "home,room=Kitchen temp=22.5,hum=36.0,co=0i 1719938400", "home,room=Living\ Room temp=22.4,hum=36.0,co=0i 1719942000", "home,room=Kitchen temp=22.8,hum=36.5,co=1i 1719942000", "home,room=Living\ Room temp=22.3,hum=36.1,co=0i 1719945600", "home,room=Kitchen temp=22.8,hum=36.3,co=1i 1719945600", "home,room=Living\ Room temp=22.3,hum=36.1,co=1i 1719949200", "home,room=Kitchen temp=22.7,hum=36.2,co=3i 1719949200", "home,room=Living\ Room temp=22.4,hum=36.0,co=4i 1719952800", "home,room=Kitchen temp=22.4,hum=36.0,co=7i 1719952800", "home,room=Living\ Room temp=22.6,hum=35.9,co=5i 1719956400", "home,room=Kitchen temp=22.7,hum=36.0,co=9i 1719956400", "home,room=Living\ Room temp=22.8,hum=36.2,co=9i 1719960000", "home,room=Kitchen temp=23.3,hum=36.9,co=18i 1719960000", "home,room=Living\ Room temp=22.5,hum=36.3,co=14i 1719963600", "home,room=Kitchen temp=23.1,hum=36.6,co=22i 1719963600", "home,room=Living\ Room temp=22.2,hum=36.4,co=17i 1719967200", "home,room=Kitchen temp=22.7,hum=36.5,co=26i 1719967200" ] client.write(lines,write_precision='s')样本执行以下操作:
从
influxdb_client_3模块导入InfluxDBClient3对象。调用
InfluxDBClient3()构造函数以实例化一个配置了以下凭据的 InfluxDB 客户端:host: InfluxDB Cloud Serverless 区域主机名(不带协议或尾部斜杠的 URL)token: 一个 token,具有对指定存储桶的写入权限。将此存储在秘密存储或环境变量中,以避免暴露原始的 token 字符串。database: 要写入的InfluxDB Cloud Serverless桶的名称
定义一系列线路协议字符串,每个字符串代表一个数据记录。
使用行协议记录列表和写入选项调用
client.write()方法。因为示例行协议中的时间戳是以秒为精度,因此示例传递了
write_precision='s'选项来将 时间戳精度 设置为秒。
要执行模块并将行协议写入您的 InfluxDB Cloud Serverless 桶,请在终端中输入以下命令:
python write.py
如果成功,输出是成功消息;否则,错误详情和失败消息。
要使用 Go 将数据写入 InfluxDB Cloud Serverless,使用 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 // API 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://cloud2.influxdata.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 1719124000`, `home,room=Kitchen temp=21.0,hum=35.9,co=0i 1719124000`, `home,room=Living\ Room temp=21.4,hum=35.9,co=0i 1719127600`, `home,room=Kitchen temp=23.0,hum=36.2,co=0i 1719127600`, `home,room=Living\ Room temp=21.8,hum=36.0,co=0i 1719131200`, `home,room=Kitchen temp=22.7,hum=36.1,co=0i 1719131200`, `home,room=Living\ Room temp=22.2,hum=36.0,co=0i 1719134800`, `home,room=Kitchen temp=22.4,hum=36.0,co=0i 1719134800`, `home,room=Living\ Room temp=22.2,hum=35.9,co=0i 1719138400`, `home,room=Kitchen temp=22.5,hum=36.0,co=0i 1719138400`, `home,room=Living\ Room temp=22.4,hum=36.0,co=0i 1719142000`, `home,room=Kitchen temp=22.8,hum=36.5,co=1i 1719142000`, `home,room=Living\ Room temp=22.3,hum=36.1,co=0i 1719145600`, `home,room=Kitchen temp=22.8,hum=36.3,co=1i 1719145600`, `home,room=Living\ Room temp=22.3,hum=36.1,co=1i 1719149200`, `home,room=Kitchen temp=22.7,hum=36.2,co=3i 1719149200`, `home,room=Living\ Room temp=22.4,hum=36.0,co=4i 1719152800`, `home,room=Kitchen temp=22.4,hum=36.0,co=7i 1719152800`, `home,room=Living\ Room temp=22.6,hum=35.9,co=5i 1719156400`, `home,room=Kitchen temp=22.7,hum=36.0,co=9i 1719156400`, `home,room=Living\ Room temp=22.8,hum=36.2,co=9i 1719160000`, `home,room=Kitchen temp=23.3,hum=36.9,co=18i 1719160000`, `home,room=Living\ Room temp=22.5,hum=36.3,co=14i 1719163600`, `home,room=Kitchen temp=23.1,hum=36.6,co=22i 1719163600`, `home,room=Living\ Room temp=22.2,hum=36.4,co=17i 1719167200`, `home,room=Kitchen temp=22.7,hum=36.5,co=26i 1719167200`, } // 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 Cloud Serverless 存储桶,请在终端中输入以下命令:
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 = 'cloud2.influxdata.com'; const database = 'get-started'; /** * INFLUX_TOKEN is an environment variable you assigned to your * API 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 1719124000`, `home,room=Kitchen temp=21.0,hum=35.9,co=0i 1719124000`, `home,room=Living\\ Room temp=21.4,hum=35.9,co=0i 1719127600`, `home,room=Kitchen temp=23.0,hum=36.2,co=0 1719127600`, `home,room=Living\\ Room temp=21.8,hum=36.0,co=0i 1719131200`, `home,room=Kitchen temp=22.7,hum=36.1,co=0i 1719131200`, `home,room=Living\\ Room temp=22.2,hum=36.0,co=0i 1719134800`, `home,room=Kitchen temp=22.4,hum=36.0,co=0i 1719134800`, `home,room=Living\\ Room temp=22.2,hum=35.9,co=0i 1719138400`, `home,room=Kitchen temp=22.5,hum=36.0,co=0i 1719138400`, `home,room=Living\\ Room temp=22.4,hum=36.0,co=0i 1719142000`, `home,room=Kitchen temp=22.8,hum=36.5,co=1i 1719142000`, `home,room=Living\\ Room temp=22.3,hum=36.1,co=0i 1719145600`, `home,room=Kitchen temp=22.8,hum=36.3,co=1i 1719145600`, `home,room=Living\\ Room temp=22.3,hum=36.1,co=1i 1719149200`, `home,room=Kitchen temp=22.7,hum=36.2,co=3i 1719149200`, `home,room=Living\\ Room temp=22.4,hum=36.0,co=4i 1719152800`, `home,room=Kitchen temp=22.4,hum=36.0,co=7i 1719152800`, `home,room=Living\\ Room temp=22.6,hum=35.9,co=5i 1719156400`, `home,room=Kitchen temp=22.7,hum=36.0,co=9i 1719156400`, `home,room=Living\\ Room temp=22.8,hum=36.2,co=9i 1719160000`, `home,room=Kitchen temp=23.3,hum=36.9,co=18i 1719160000`, `home,room=Living\\ Room temp=22.5,hum=36.3,co=14i 1719163600`, `home,room=Kitchen temp=23.1,hum=36.6,co=22i 1719163600`, `home,room=Living\\ Room temp=22.2,hum=36.4,co=17i 1719167200`, `home,room=Kitchen temp=22.7,hum=36.5,co=26i 1719167200`, ]; /** * 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 Cloud Serverless 区域 URLtoken: 一个 token 具有对指定存储桶的写入访问权限。 将其存储在秘密存储或环境变量中,以避免暴露 原始的 token 字符串。
定义了一组行协议字符串,其中每个字符串表示一个数据记录。
为每条记录调用客户端的
write()方法,定义成功或失败的消息返回,并将待处理的承诺收集到writePromises数组中。每次调用write()都传递以下参数:record: 行协议记录database: 要写入的 InfluxDB Cloud Serverless 存储桶的名称{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.mjs以写入 InfluxDB Cloud Serverless: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://cloud2.influxdata.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 1719924000", "home,room=Kitchen temp=21.0,hum=35.9,co=0i 1719924000", "home,room=Living\\ Room temp=21.4,hum=35.9,co=0i 1719927600", "home,room=Kitchen temp=23.0,hum=36.2,co=0i 1719927600", "home,room=Living\\ Room temp=21.8,hum=36.0,co=0i 1719931200", "home,room=Kitchen temp=22.7,hum=36.1,co=0i 1719931200", "home,room=Living\\ Room temp=22.2,hum=36.0,co=0i 1719934800", "home,room=Kitchen temp=22.4,hum=36.0,co=0i 1719934800", "home,room=Living\\ Room temp=22.2,hum=35.9,co=0i 1719938400", "home,room=Kitchen temp=22.5,hum=36.0,co=0i 1719938400", "home,room=Living\\ Room temp=22.4,hum=36.0,co=0i 1719942000", "home,room=Kitchen temp=22.8,hum=36.5,co=1i 1719942000", "home,room=Living\\ Room temp=22.3,hum=36.1,co=0i 1719945600", "home,room=Kitchen temp=22.8,hum=36.3,co=1i 1719945600", "home,room=Living\\ Room temp=22.3,hum=36.1,co=1i 1719949200", "home,room=Kitchen temp=22.7,hum=36.2,co=3i 1719949200", "home,room=Living\\ Room temp=22.4,hum=36.0,co=4i 1719952800", "home,room=Kitchen temp=22.4,hum=36.0,co=7i 1719952800", "home,room=Living\\ Room temp=22.6,hum=35.9,co=5i 1719956400", "home,room=Kitchen temp=22.7,hum=36.0,co=9i 1719956400", "home,room=Living\\ Room temp=22.8,hum=36.2,co=9i 1719960000", "home,room=Kitchen temp=23.3,hum=36.9,co=18i 1719960000", "home,room=Living\\ Room temp=22.5,hum=36.3,co=14i 1719963600", "home,room=Kitchen temp=23.1,hum=36.6,co=22i 1719963600", "home,room=Living\\ Room temp=22.2,hum=36.4,co=17i 1719967200", "home,room=Kitchen temp=22.7,hum=36.5,co=26i 1719967200" }; // 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 Cloud Serverless 区域 URL
- database: 要写入的 InfluxDB Cloud Serverless 存储桶的名称
- token: 一个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 Cloud Serverless 桶,请在终端中输入以下命令:
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_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://cloud2.influxdata.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 1719924000", "home,room=Kitchen temp=21.0,hum=35.9,co=0i 1719924000", "home,room=Living\\ Room temp=21.4,hum=35.9,co=0i 1719927600", "home,room=Kitchen temp=23.0,hum=36.2,co=0i 1719927600", "home,room=Living\\ Room temp=21.8,hum=36.0,co=0i 1719931200", "home,room=Kitchen temp=22.7,hum=36.1,co=0i 1719931200", "home,room=Living\\ Room temp=22.2,hum=36.0,co=0i 1719934800", "home,room=Kitchen temp=22.4,hum=36.0,co=0i 1719934800", "home,room=Living\\ Room temp=22.2,hum=35.9,co=0i 1719938400", "home,room=Kitchen temp=22.5,hum=36.0,co=0i 1719938400", "home,room=Living\\ Room temp=22.4,hum=36.0,co=0i 1719942000", "home,room=Kitchen temp=22.8,hum=36.5,co=1i 1719942000", "home,room=Living\\ Room temp=22.3,hum=36.1,co=0i 1719945600", "home,room=Kitchen temp=22.8,hum=36.3,co=1i 1719945600", "home,room=Living\\ Room temp=22.3,hum=36.1,co=1i 1719949200", "home,room=Kitchen temp=22.7,hum=36.2,co=3i 1719949200", "home,room=Living\\ Room temp=22.4,hum=36.0,co=4i 1719952800", "home,room=Kitchen temp=22.4,hum=36.0,co=7i 1719952800", "home,room=Living\\ Room temp=22.6,hum=35.9,co=5i 1719956400", "home,room=Kitchen temp=22.7,hum=36.0,co=9i 1719956400", "home,room=Living\\ Room temp=22.8,hum=36.2,co=9i 1719960000", "home,room=Kitchen temp=23.3,hum=36.9,co=18i 1719960000", "home,room=Living\\ Room temp=22.5,hum=36.3,co=14i 1719963600", "home,room=Kitchen temp=23.1,hum=36.6,co=22i 1719963600", "home,room=Living\\ Room temp=22.2,hum=36.4,co=17i 1719967200", "home,room=Kitchen temp=22.7,hum=36.5,co=26i 1719967200" ); /** * 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 Cloud Serverless 区域 URLdatabase: 要写入的InfluxDB Cloud Serverless桶的名称token: 一个 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。 现在数据存储在InfluxDB中,让我们查询一下它。