InfluxDB - Writing Data with the HTTP API

本文介绍如何使用InfluxDB内置的HTTP API创建数据库及写入数据,包括通过HTTP API创建数据库的方法、使用Line Protocol格式写入单个数据点或多数据点的步骤,以及从文件批量写入数据的技巧。
摘要由CSDN通过智能技术生成

There are many ways to write data into InfluxDB including the command line interfaceclient libraries and plugins for common data formats such as Graphite. Here we’ll show you how to create a database and write data to it using the built-in HTTP API.

Creating a database using the HTTP API

To create a database send a POST request to the /query endpoint and set the URL parameter q to CREATE DATABASE <new_database_name>. The example below sends a request to InfluxDB running on localhost and creates the database mydb:

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

Writing data using the HTTP API

The HTTP API is the primary means of writing data into InfluxDB, by sending POST requests to the /write endpoint. The example below writes a single point to the mydb database. The data consist of the measurement cpu_load_short, the tag keys host and region with the tag valuesserver01 and us-west, the field key value with a field value of 0.64, and the timestamp 1434055562000000000

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

When writing points, you must specify an existing database in the db query parameter. See the API Reference documentation for a complete list of the available query parameters.

The body of the POST - we call this the Line Protocol - contains the time-series data that you wish to store. They consist of a measurement, tags, fields, and a timestamp. InfluxDB requires a measurement name. Strictly speaking, tags are optional but most series include tags to differentiate data sources and to make querying both easy and efficient. Both tag keys and tag values are strings. Field keys are required and are always strings, and, by default, field values are floats. The timestamp - supplied at the end of the line in Unix time in nanoseconds since January 1, 1970 UTC - is optional. If you do not specify a timestamp InfluxDB uses the server’s local nanosecond timestamp in Unix epoch. Anything that has to do with time in InfluxDB is always UTC.

Writing multiple points


Post multiple points to multiple series at the same time by separating each point with a new line. Batching points in this manner results in much higher performance.

The following example writes three points to the database mydb. The first point belongs to the series with the measurement cpu_load_short and tag set host=server02 and has the server’s local timestamp. The second point belongs to the series with the measurement cpu_load_short and tag set host=server02,region=us-west and has the specified timestamp 1422568543702900257. The third point has the same specified timestamp as the second point, but it is written to the series with the measurement cpu_load_short and tag set direction=in,host=server01,region=us-west

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'

Writing points from a file


Write points from a file by passing @filename to curl. The data in the file should follow InfluxDB’s line protocol syntax.

Example of a properly-formatted file (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

Write the data in cpu_data.txt to the mydb database with:

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

Note: If your data file has more than 5,000 points, it may be necessary to split that file into several files in order to write your data in batches to InfluxDB. By default, the HTTP request times out after five seconds. InfluxDB will still attempt to write the points after that time out but there will be no confirmation that they were successfully written.

Schemaless Design


InfluxDB is a schemaless database. You can add new measurements, tags, and fields at any time. Note that if you attempt to write data with a different type than previously used (for example, writing a string to a field that previously accepted integers), InfluxDB will reject those data.

A note on REST…


InfluxDB uses HTTP solely as a convenient and widely supported data transfer protocol.

Modern web APIs have settled on REST because it addresses a common need. As the number of endpoints grows the need for an organizing system becomes pressing. REST is the industry agreed style for organizing large numbers of endpoints. This consistency is good for those developing and consuming the API: everyone involved knows what to expect.

REST, however, is a convention. InfluxDB makes do with three API endpoints. This simple, easy to understand system uses HTTP as a transfer method for InfluxQL. The InfluxDB API makes no attempt to be RESTful.

HTTP response summary


  • 2xx: If your write request received HTTP 204 No Content, it was a success! If it’s HTTP 200 OK, InfluxDB understood the request but couldn’t complete it. The body of the response will contain additional error information.
  • 4xx: InfluxDB could not understand the request.
  • 5xx: The system is overloaded or significantly impaired.

Examples of error responses:

  • Writing a float to a field that previously accepted booleans:
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'

returns:

HTTP/1.1 400 Bad Request
[...]
write failed: field type conflict: input field "booleanonly" on measurement "tobeornottobe" is type float64, already exists as type boolean
  • Writing a point to a database that doesn’t exist:
curl -i -XPOST 'http://localhost:8086/write?db=atlantis' --data-binary 'liters value=10'

returns:

HTTP/1.1 404 Not Found
[...]
database not found: "atlantis"

Next steps


Now that you know how to write data with the built-in HTTP API discover how to query them with the Querying Data guide!

Sure! Here's an example of a 4-lane MIPI data alignment module written in Verilog: ```verilog module mipi_data_alignment ( input [31:0] data_in, input clk, input rst, output reg [31:0] data_out ); reg [31:0] shift_reg [0:3]; always @(posedge clk or posedge rst) begin if (rst) begin for (int i = 0; i < 4; i = i + 1) begin shift_reg[i] <= 32'b0; end end else begin for (int i = 0; i < 4; i = i + 1) begin shift_reg[i] <= {shift_reg[i][30:0], data_in[i*8 +: 8]}; end end end always @(posedge clk) begin if (rst) begin data_out <= 32'b0; end else begin for (int i = 0; i < 4; i = i + 1) begin data_out[i*8 +: 8] <= shift_reg[i][31:24]; end end end endmodule ``` In this module, `data_in` is a 32-bit input representing the MIPI data for the four lanes, `clk` is the clock signal, `rst` is the reset signal, and `data_out` is the aligned output data for the four lanes. The array `shift_reg[0:3]` consists of four 32-bit shift registers, each corresponding to one lane. On each rising edge of the clock (`clk`), the data for each lane is shifted left by eight bits, and the new input data for each lane is concatenated to the rightmost eight bits of the corresponding shift register. This aligns the incoming MIPI data stream for each lane. The aligned output data for each lane is updated on each rising edge of the clock based on the current values of the shift registers. Remember to customize this code according to your specific requirements and the MIPI protocol specifications you are working with.
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值