3. InfluxDB使用HTTP的API编写数据

Wesley13
• 阅读 831

InfluxDB前篇介绍

Centos7 下 InfluxDB 从安装开始到入门InfluxDB关键概念经过前面两个篇章的探讨,基本已经了解了InfluxDB的操作,下面再来继续看看使用HTTP API编写数据。

使用HTTP的API请求创建数据库

首先查看InfluxDB当前有哪些数据库了。

[root@server81 ~]# docker exec -it influxdb bashroot@d2918dc47850:/#root@d2918dc47850:/# influxConnected to http://localhost:8086 version 1.7.2InfluxDB shell version: 1.7.2Enter an InfluxQL query>> show databases;name: databasesname----_internalmydb>

可以从上面看出,当前有 _internalemydb 两个数据库。

下面使用POST请求创建一个 testdb 数据库

curl -i -XPOST http://localhost:8086/query --data-urlencode "q=CREATE DATABASE testdb"

执行如下:

## 查看一下influxdb的进程[root@server81 ~]# docker psCONTAINER ID        IMAGE               COMMAND                  CREATED             STATUS              PORTS                    NAMESd2918dc47850        influxdb            "/entrypoint.sh in..."   2 days ago          Up 2 days           0.0.0.0:8086->8086/tcp   influxdb82a294241ff7        registry:2          "/entrypoint.sh /e..."   4 weeks ago         Up 5 days           0.0.0.0:5000->5000/tcp   registry[root@server81 ~]### 使用http的api创建数据库testdb[root@server81 ~]# curl -i -XPOST http://localhost:8086/query --data-urlencode "q=CREATE DATABASE testdb"HTTP/1.1 200 OKContent-Type: application/jsonRequest-Id: 41f2a1c7-1250-11e9-8071-0242ac110003X-Influxdb-Build: OSSX-Influxdb-Version: 1.7.2X-Request-Id: 41f2a1c7-1250-11e9-8071-0242ac110003Date: Mon, 07 Jan 2019 07:45:52 GMTTransfer-Encoding: chunked{"results":[{"statement_id":0}]}[root@server81 ~]#

执行完毕之后,查看一下数据库是否创建成功,如下:

## 查看一下所有influxdb的数据库,可以看到已经创建好了testdb了。> show databases;name: databasesname----_internalmydbtestdb>> use testdbUsing database testdb>## 查看testdb有哪些表> show measurements>>

那么在这里再次分析一下这个请求的格式:

curl -i -XPOST http://localhost:8086/query --data-urlencode "q=CREATE DATABASE testdb"

格式说明:

  • curl -i -XPOST 则是进行POST请求

  • http://localhost:8086/query 请求influxdb的8086端口服务,路径为 /query

  • --data-urlencode 设置请求数据进行 url编码

  • 设置请求执行的命令 "q=CREATE DATABASE testdb" 执行创建数据库

使用HTTP的API写入数据

curl -i -XPOST 'http://localhost:8086/write?db=testdb' --data-binary 'cpu_load_short,host=server01,region=us-west value=0.64 1434055562000000000'

请求格式说明:

  • 首先http请求路径 /write?db=testdb ,说明将数据写入 testdb数据库中。

  • --data-binary 说明 HTTP POST请求中的数据为纯二进制数据

  • 'cpu_load_short,host=server01,region=us-west value=0.64 1434055562000000000'

  • 说明将数据插入到 cpu_load_short 表中

  • 设置两个tag, host=server01,region=us-west

  • 设置一个field, value=0.64

  • 插入数据的时间戳 1434055562000000000 ,如果不插入时间,则会写入服务器的本地时间

执行如下:

## 执行API请求插入数据[root@server81 ~]# curl -i -XPOST 'http://localhost:8086/write?db=testdb' --data-binary 'cpu_load_short,host=server01,region=us-west value=0.64 1434055562000000000'HTTP/1.1 204 No ContentContent-Type: application/jsonRequest-Id: a9f092e4-1254-11e9-8075-0242ac110003X-Influxdb-Build: OSSX-Influxdb-Version: 1.7.2X-Request-Id: a9f092e4-1254-11e9-8075-0242ac110003Date: Mon, 07 Jan 2019 08:17:24 GMT[root@server81 ~]#

执行请求之后,到InfluxDB数据库中查询看看:

> show measurementsname: measurementsname----cpu_load_short>> select * from cpu_load_shortname: cpu_load_shorttime                host     region  value----                ----     ------  -----1434055562000000000 server01 us-west 0.64>

写入点时,必须指定现有的数据库。有关可用查询参数的完整列表,请参阅API参考文档。

使用HTTP的API请求写入多个点的数据

curl -i -XPOST 'http://localhost:8086/write?db=testdb' --data-binary 'cpu_load_short,host=server02 value=0.67cpu_load_short,host=server02,region=us-west value=0.55 1422568543702900257cpu_load_short,direction=in,host=server01,region=us-west value=2.0 1422568543702900257'

上面的语句是写入三个数据点如下:

  • 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

先执行请求来看看是什么效果

[root@server81 ~]# curl -i -XPOST 'http://localhost:8086/write?db=testdb' --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'HTTP/1.1 204 No ContentContent-Type: application/jsonRequest-Id: f3b56186-1255-11e9-8079-0242ac110003X-Influxdb-Build: OSSX-Influxdb-Version: 1.7.2X-Request-Id: f3b56186-1255-11e9-8079-0242ac110003Date: Mon, 07 Jan 2019 08:26:38 GMT[root@server81 ~]#

插入数据之后,在InfluxDB查询看看

> select * from cpu_load_shortname: cpu_load_shorttime                host     region  value----                ----     ------  -----1434055562000000000 server01 us-west 0.64>> select * from cpu_load_shortname: cpu_load_shorttime                direction host     region  value----                --------- ----     ------  -----1422568543702900257 in        server01 us-west 21422568543702900257           server02 us-west 0.551434055562000000000           server01 us-west 0.641546849598178339889           server02         0.67>

好了,从上面可以看出,新插入了三条数据,并且cpu_load_short表还自动增加了一个direction字段。

读取文件,然后使用HTTP的API来写入数据

有时候可以直接根据日志文件的数据,写入InfluxDB中。

首先准备好一个文件cpu_data.txt

cpu_load_short,host=server02 value=0.67cpu_load_short,host=server02,region=us-west value=0.55 1422568543702900257cpu_load_short,direction=in,host=server01,region=us-west value=2.0 1422568543702900257

**注意:**该文件内的格式应该遵循InfluxDB的行协议语法。

准备文件如下:

[root@server81 ~]# cat cpu_data.txtcpu_load_short,host=server02 value=0.67cpu_load_short,host=server02,region=us-west value=0.55 1422568543702900257cpu_load_short,direction=in,host=server01,region=us-west value=2.0 1422568543702900257[root@server81 ~]#

执行API写入数据

curl -i -XPOST 'http://localhost:8086/write?db=testdb' --data-binary @cpu_data.txt

执行如下:

## 查看文件内容数据[root@server81 ~]# cat cpu_data.txtcpu_load_short,host=server02 value=0.67cpu_load_short,host=server02,region=us-west value=0.55 1422568543702900257cpu_load_short,direction=in,host=server01,region=us-west value=2.0 1422568543702900257[root@server81 ~]### 读取文件,然后使用API请求写入InfluxDB[root@server81 ~]# curl -i -XPOST 'http://localhost:8086/write?db=testdb' --data-binary @cpu_data.txtHTTP/1.1 204 No ContentContent-Type: application/jsonRequest-Id: 4bd04e2a-1257-11e9-807b-0242ac110003X-Influxdb-Build: OSSX-Influxdb-Version: 1.7.2X-Request-Id: 4bd04e2a-1257-11e9-807b-0242ac110003Date: Mon, 07 Jan 2019 08:36:15 GMT[root@server81 ~]#

查看InfluxDB:

> select * from cpu_load_shortname: cpu_load_shorttime                direction host     region  value----                --------- ----     ------  -----1422568543702900257 in        server01 us-west 21422568543702900257           server02 us-west 0.551434055562000000000           server01 us-west 0.641546849598178339889           server02         0.67>> select * from cpu_load_shortname: cpu_load_shorttime                direction host     region  value----                --------- ----     ------  -----1422568543702900257 in        server01 us-west 21422568543702900257           server02 us-west 0.551434055562000000000           server01 us-west 0.641546849598178339889           server02         0.671546850175491084332           server02         0.67>

cpu_load_short,host=server02,region=us-west value=0.55 1422568543702900257可以看到这条数据在插入之前已经有了的。但是查询数据并没有重复插入,说明只要数据完全一致,并不会重复插入。

下面来重复插入测试一下看看:

## 前面的第一次数据插入[root@server81 ~]# curl -i -XPOST 'http://localhost:8086/write?db=testdb' --data-binary @cpu_data.txtHTTP/1.1 204 No ContentContent-Type: application/jsonRequest-Id: 4bd04e2a-1257-11e9-807b-0242ac110003X-Influxdb-Build: OSSX-Influxdb-Version: 1.7.2X-Request-Id: 4bd04e2a-1257-11e9-807b-0242ac110003Date: Mon, 07 Jan 2019 08:36:15 GMT[root@server81 ~]### 第二次数据插入[root@server81 ~]# curl -i -XPOST 'http://localhost:8086/write?db=testdb' --data-binary @cpu_data.txtHTTP/1.1 204 No ContentContent-Type: application/jsonRequest-Id: f5eb3016-1257-11e9-807d-0242ac110003X-Influxdb-Build: OSSX-Influxdb-Version: 1.7.2X-Request-Id: f5eb3016-1257-11e9-807d-0242ac110003Date: Mon, 07 Jan 2019 08:41:00 GMT[root@server81 ~]#

查看重复插入同样数据后的InfluxDB

> select * from cpu_load_shortname: cpu_load_shorttime                direction host     region  value----                --------- ----     ------  -----1422568543702900257 in        server01 us-west 21422568543702900257           server02 us-west 0.551434055562000000000           server01 us-west 0.641546849598178339889           server02         0.671546850175491084332           server02         0.67>> select * from cpu_load_shortname: cpu_load_shorttime                direction host     region  value----                --------- ----     ------  -----1422568543702900257 in        server01 us-west 21422568543702900257           server02 us-west 0.551434055562000000000           server01 us-west 0.641546849598178339889           server02         0.671546850175491084332           server02         0.671546850460880063366           server02         0.67>

因为插入的数据中,只有这条cpu_load_short,host=server02 value=0.67 数据是没有时间戳的,所以可以插入。其他都不能重复插入。

注意:如果您的数据文件有超过5,000个点,则可能需要将该文件拆分为多个文件,以便将数据批量写入InfluxDB。默认情况下,HTTP请求在五秒后超时。在此之后,InfluxDB仍会尝试写点数据,但不会确认它们是否已成功编写。

无模式设计

InfluxDB是一个无模式数据库。您可以随时添加新的测量,标签和字段。请注意,如果您尝试使用与以前使用的类型不同的类型编写数据(例如,将字符串写入先前接受整数的字段),InfluxDB将拒绝这些数据。

HTTP响应摘要

  • 2xx:如果收到您的写请求HTTP 204 No Content,则表示成功!

  • 4xx:InfluxDB无法理解请求。

  • 5xx:系统过载或严重受损。

错误响应的示例:

将浮点数据写入先前接受布尔值的字段:

curl -i -XPOST 'http://localhost:8086/write?db=testdb' --data-binary 'tobeornottobe booleanonly=true'curl -i -XPOST 'http://localhost:8086/write?db=testdb' --data-binary 'tobeornottobe booleanonly=5'

执行如下:

## 首先写入新表tobeornottobe一个布尔值的字段,写入成功[root@server81 ~]# curl -i -XPOST 'http://localhost:8086/write?db=testdb' --data-binary 'tobeornottobe booleanonly=true'HTTP/1.1 204 No ContentContent-Type: application/jsonRequest-Id: 34ff0315-1259-11e9-807f-0242ac110003X-Influxdb-Build: OSSX-Influxdb-Version: 1.7.2X-Request-Id: 34ff0315-1259-11e9-807f-0242ac110003Date: Mon, 07 Jan 2019 08:49:56 GMT[root@server81 ~]### 将数值5写入布尔值的字段,则400报错[root@server81 ~]# curl -i -XPOST 'http://localhost:8086/write?db=testdb' --data-binary 'tobeornottobe booleanonly=5'HTTP/1.1 400 Bad RequestContent-Type: application/jsonRequest-Id: 3ab628e4-1259-11e9-8080-0242ac110003X-Influxdb-Build: OSSX-Influxdb-Error: partial write: field type conflict: input field "booleanonly" on measurement "tobeornottobe" is type float, already exists as type boolean dropped=1X-Influxdb-Version: 1.7.2X-Request-Id: 3ab628e4-1259-11e9-8080-0242ac110003Date: Mon, 07 Jan 2019 08:50:05 GMTContent-Length: 165{"error":"partial write: field type conflict: input field \"booleanonly\" on measurement \"tobeornottobe\" is type float, already exists as type boolean dropped=1"}[root@server81 ~]#

查看一下InfluxDB的数据:

> show measurementsname: measurementsname----cpu_load_shorttobeornottobe>> select * from tobeornottobename: tobeornottobetime                booleanonly----                -----------1546850996203127571 true>

可以看出,数据只插入了一条。

那么再来看看,如果将数据写入一个不存在的数据库,会报什么错误呢?

[root@server81 ~]# curl -i -XPOST 'http://localhost:8086/write?db=atlantis' --data-binary 'liters value=10'HTTP/1.1 404 Not FoundContent-Type: application/jsonRequest-Id: c8c48879-1259-11e9-8083-0242ac110003X-Influxdb-Build: OSSX-Influxdb-Error: database not found: "atlantis"X-Influxdb-Version: 1.7.2X-Request-Id: c8c48879-1259-11e9-8083-0242ac110003Date: Mon, 07 Jan 2019 08:54:04 GMTContent-Length: 45{"error":"database not found: \"atlantis\""}[root@server81 ~]#

从上面可以看到,报了数据库找不到的错误{"error":"database not found: \"atlantis\""}

本文分享自微信公众号 - DevOps社群(DevOpsCommunity)。
如有侵权,请联系 support@oschina.cn 删除。
本文参与“OSC源创计划”,欢迎正在阅读的你也加入,一起分享。

点赞
收藏
评论区
推荐文章
blmius blmius
2年前
MySQL:[Err] 1292 - Incorrect datetime value: ‘0000-00-00 00:00:00‘ for column ‘CREATE_TIME‘ at row 1
文章目录问题用navicat导入数据时,报错:原因这是因为当前的MySQL不支持datetime为0的情况。解决修改sql\mode:sql\mode:SQLMode定义了MySQL应支持的SQL语法、数据校验等,这样可以更容易地在不同的环境中使用MySQL。全局s
芝士年糕 芝士年糕
1年前
InfluxDB安装以及使用
一、简介InfluxDB是一种时序数据库,用来存放监控数据。InfluxDB是用Go语言编写的一个开源分布式时序、事件和指标数据库,无需外部依赖。其主要特色功能1)基于时间序列,支持与时间有关的相关函数(如最大,最小,求和等)2)可度量性:你可以实时对大量数据进行计算3)基于事件:它支持任意的事件数据InfluxDB的主要特色1)无结构(无模式):可以是任
Jacquelyn38 Jacquelyn38
2年前
2020年前端实用代码段,为你的工作保驾护航
有空的时候,自己总结了几个代码段,在开发中也经常使用,谢谢。1、使用解构获取json数据let jsonData  id: 1,status: "OK",data: 'a', 'b';let  id, status, data: number   jsonData;console.log(id, status, number )
皕杰报表之UUID
​在我们用皕杰报表工具设计填报报表时,如何在新增行里自动增加id呢?能新增整数排序id吗?目前可以在新增行里自动增加id,但只能用uuid函数增加UUID编码,不能新增整数排序id。uuid函数说明:获取一个UUID,可以在填报表中用来创建数据ID语法:uuid()或uuid(sep)参数说明:sep布尔值,生成的uuid中是否包含分隔符'',缺省为
Easter79 Easter79
2年前
Twitter的分布式自增ID算法snowflake (Java版)
概述分布式系统中,有一些需要使用全局唯一ID的场景,这种时候为了防止ID冲突可以使用36位的UUID,但是UUID有一些缺点,首先他相对比较长,另外UUID一般是无序的。有些时候我们希望能使用一种简单一些的ID,并且希望ID能够按照时间有序生成。而twitter的snowflake解决了这种需求,最初Twitter把存储系统从MySQL迁移
Stella981 Stella981
2年前
InfluxDB学习之InfluxDB的安装和简介
系列详情请看:《InfluxDB系列教程(https://www.oschina.net/action/GoToLink?urlhttp%3A%2F%2Fwww.linuxdaxue.com%2Fseries%2Finfluxdbseries%2F)》:InfluxDB是一个当下比较流行的时序数据库,InfluxDB使用Go语言编写,无需外部
Stella981 Stella981
2年前
InfluxDB基本概念和操作
InfluxDB基本概念1、数据格式在InfluxDB中,我们可以粗略的将要存入的一条数据看作一个虚拟的key和其对应的value(fieldvalue)。格式如下:1cpu_usage,hostserver01,regionuswestvalue
Stella981 Stella981
2年前
InfluxDB学习之InfluxDB的基本概念
一、与传统数据库中的名词做比较influxDB中的名词传统数据库中的概念database数据库measurement数据库中的表points表里面的一行数据二、InfluxDB中独有的概念1)PointPoint由时间戳(time)、数据(field)、标签(tags)组成。Po
Wesley13 Wesley13
2年前
MySQL部分从库上面因为大量的临时表tmp_table造成慢查询
背景描述Time:20190124T00:08:14.70572408:00User@Host:@Id:Schema:sentrymetaLast_errno:0Killed:0Query_time:0.315758Lock_
Python进阶者 Python进阶者
3个月前
Excel中这日期老是出来00:00:00,怎么用Pandas把这个去除
大家好,我是皮皮。一、前言前几天在Python白银交流群【上海新年人】问了一个Pandas数据筛选的问题。问题如下:这日期老是出来00:00:00,怎么把这个去除。二、实现过程后来【论草莓如何成为冻干莓】给了一个思路和代码如下:pd.toexcel之前把这