Writing Data with the 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:

1 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 values server01 and us-west, the field key value with a field value of 0.64, and the timestamp 1434055562000000000

1 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. Points will be written to db’s default retention policy if you do not supply a retention policy via the rp query parameter. See the API Reference documentation for a complete list of the available query parameters.

 

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

1 curl -i -XPOST 'http://localhost:8086/write?db=mydb' --data-binary 'cpu_load_short,host=server02 value=0.67
2 cpu_load_short,host=server02,region=us-west value=0.55 1422568543702900257
3 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):

1 cpu_load_short,host=server02 value=0.67
2 cpu_load_short,host=server02,region=us-west value=0.55 1422568543702900257
3 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:

1 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.

HTTP response summary

2xx: If your write request received HTTP 204 No Content, it was a success!
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:

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

returns:

1 HTTP/1.1 400 Bad Request
2 [...]
3 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:

1 curl -i -XPOST 'http://localhost:8086/write?db=atlantis' --data-binary 'liters value=10'

 returns:

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

 

posted @ 2019-05-14 15:19  那年花开月正圆  阅读(310)  评论(0编辑  收藏  举报