有很多方法可以将数据写入InfluxDB,包括命令行界面,客户端库和常见(例如Graphite)插件。在这里,我们将向您展示如何使用内置的HTTP AP创建数据库并向其写入数据
![](https://img.haomeiwen.com/i6278284/76b88d3d79bb79a1.png)
一,使用 HTTP API 创建和删除数据库
-
创建数据库
要创建数据库,请将 POST 请求发送到 /query 端点,并将 URL 参数 q 设置为 CREATE DATABASE <new_database_name> 。下面的示例向本地主机上运行的 InfluxDB 发送请求,并创建数据库 mydb:
curl -i -XPOST http://localhost:8086/query --data-urlencode "q=CREATE DATABASE mydb"
- 删除数据库
curl -POST http://localhost:8086/query --data-urlencode "q=DROP DATABASE mydb"
二,使用 HTTP API 添加数据
-
添加数据
通过向 /write 端点发送 POST 请求,将数据写入InfluxDB,下面的这个例子就是将数据写入到数据库 mydb ,--data-binary 后面是需插入数据
curl -i -XPOST 'http://localhost:8086/write?db=mydb' --data-binary 'cpu_load_short,host=server01,region=us-west value=0.64 1434055562000000000'
-
cpu_load_short 是表名(measurement)
-
tag 字段是 host 和 region,值分别为:server01 和 us-west
-
field key 字段是 value,值为 0.64
-
时间戳(timestamp)指定为1434055562000000000
这样,就向 mydb 数据库的 cpu_load_short 表中插入了一条数据,其中,db参数必须指定一个数据库中已经存在的数据库名,数据体的格式遵从 InfluxDB 规定格式,首先是表名,后面是 tags,然后是 field,最后是时间戳。tags、field和时间戳三者之间以空格相分隔
-
添加多条数据
通过新行分隔多条数据可以同时将多条数据同时写入到数据库。以这种方式写入数据的效率会更加高效
curl -i -XPOST 'http://localhost:8086/write?db=mydb' --data-binary 'cpu_load_short,host=server02 value=0.67
cpu_load_short,host=server02,region=us-west value=0.55 1422568543702900257
cpu_load_short,direction=in,host=server01,region=us-west value=2.0 1422568543702900257'
- 第一条:tag 为 host=server02, field 为 value=0.67 并使用服务本地的timestamp
- 第二条:tag 为 host=server02,region=us-west ,field 为 value=0.55 并指定时间戳timestamp=1422568543702900257
- 第三条:tag 为 direction=in,host=server01,region=us-west ,field 为 value=2.0 并指定时间戳 timestamp=1422568543702900257
-
使用文件添加数据
通过将 @filename传递给 curl 来从文件中写入点。文件中的数据应该遵循InfluxDB的行协议语法
新建文件 cpu_data.txt,文件类容如下:
cpu_load_short,host=server02 value=0.67
cpu_load_short,host=server02,region=us-west value=0.55 1422568543702900257
cpu_load_short,direction=in,host=server01,region=us-west value=2.0 1422568543702900257
用 cpu_data.txt 将数据写入 mydb 数据库:
curl -i -XPOST 'http://localhost:8086/write?db=mydb' --data-binary @cpu_data.txt
三,HTTP API 响应
在使用HTTP API时,InfluxDB的响应主要有以下几个:
- 2xx:204 代表 no content,200 代表 InfluxDB 可以接收请求但是没有完成请求。一般会在 body 体中带有出错信息。
- 4xx:InfluxDB不能解析请求。
- 5xx: 系统出现错误严重受损。
错误示范的例子:
- 类型不匹配:将 float 类型写到了 boolean 类型上
curl -i -XPOST 'http://localhost:8086/write?db=hamlet' --data-binary 'tobeornottobe booleanonly=true'
curl -i -XPOST 'http://localhost:8086/write?db=hamlet' --data-binary 'tobeornottobe booleanonly=5'
返回结果:
HTTP/1.1 400 Bad Request
Content-Type: application/json
Request-Id: [...]
X-Influxdb-Version: 1.4.x
Date: Wed, 01 Mar 2017 19:38:01 GMT
Content-Length: 150
/
{"error":"field type conflict: input field \"booleanonly\" on measurement \"tobeornottobe\" is type float, already exists as type boolean dropped=1"}
- 数据写入到不存在的数据库
curl -i -XPOST 'http://localhost:8086/write?db=atlantis' --data-binary 'liters value=10'
返回结果:
HTTP/1.1 404 Not Found
Content-Type: application/json
Request-Id: [...]
X-Influxdb-Version: 1.4.x
Date: Wed, 01 Mar 2017 19:38:35 GMT
Content-Length: 45
{"error":"database not found: \"atlantis\""}
通过本篇,大概了解了如何通过 HTTP API 来写入 InfluxDB 数据,下一篇将会介绍下如何使用 InfluxDB 的 HTTP API 执行查询操作
网友评论