Use python to operate InfluxDB and python to operate influxdb
Environment: CentOS6.5 _ x64
InfluxDB version: 1.1.0
Python: 2.6
Preparations
Run the following command:
service influxdb start
Example:
[root@localhost ~]# service influxdb startStarting influxdb...influxdb process was started [ OK ][root@localhost ~]#
Github address: https://github.com/influxdata/influxdb-python
Install pip:
yum install python-pip
Install influxdb-python:
pip install influxdb
Basic operations
The following is an example of using the InfluxDBClient class to operate a database:
From influxdb import InfluxDBClientclient = InfluxDBClient ('localhost', 8086, 'root', '','') # Initialization
- Show all existing databases
Use the get_list_database function, for example:
Print client. get_list_database () # display all database names
Use the create_database function, for example:
Client. create_database ('testdb') # create a database
Use the drop_database function, for example:
Client. drop_database ('testdb') # delete a database
A complete example of database operations is as follows:
#! /Usr/bin/env python #-*-coding: UTF-8-*-from influxdb import InfluxDBClientclient = InfluxDBClient ('localhost', 8086, 'root ','','') # initialize print client. get_list_database () # display all database names client. create_database ('testdb') # create a database print client. get_list_database () # display all database names client. drop_database ('testdb') # Delete the print client of the database. get_list_database () # Show All Database names
Table operations
The database to be connected in InfluxDBClient, for example:
Client = InfluxDBClient ('localhost', 8086, 'root', '', 'testdb') # initialize (specify the database to be operated)
- Displays existing tables in the specified database.
You can use the influxql statement, for example:
Result = client. query ('show measurements; ') # display the print ("Result: {0}". format (result) in the database ))
- Create a new table and add data
InfluxDB does not provide a separate table creation statement. You can create a table by adding data. The example is as follows:
Json_body = [{"measurement": "students", "tags": {"stuid": "s123" },# "time": "2017-03-12T22: 00: 00Z ", "fields": {"score": 89}] client = InfluxDBClient ('localhost', 8086, 'root', '', 'testdb ') # initialize the client (specify the database to be operated. write_points (json_body) # Write Data and create a table
You can use the influxql statement, for example:
Client. query ("drop measurement students") # delete a table
A complete example of data table operations is as follows:
#! /Usr/bin/env python #-*-coding: UTF-8-*-from influxdb import InfluxDBClientjson_body = [{"measurement": "students", "tags ": {"stuid": "s123" },# "time": "2017-03-12T22: 00: 00Z", "fields": {"score ": 89 }}] def showDBNames (client): result = client. query ('show measurements; ') # display the print ("Result: {0}" in the database }". format (result) client = InfluxDBClient ('localhost', 8086, 'root', '', 'testdb') # initialize (specify the database to be operated) showDBNames (client) client. write_points (json_body) # Write Data and create the table showDBNames (client) client. query ("drop measurement students") # Delete the table showDBNames (client)
Data Operations
The database to be connected in InfluxDBClient, for example:
Client = InfluxDBClient ('localhost', 8086, 'root', '', 'testdb') # initialize (specify the database to be operated)
It can be implemented through write_points, for example:
Json_body = [{"measurement": "students", "tags": {"stuid": "s123" },# "time": "2017-03-12T22: 00: 00Z ", "fields": {"score": 89}] client. write_points (json_body) # Write Data
You can use the influxql statement, for example:
result = client.query('select * from students;') print("Result: {0}".format(result))
When tags and timestamp are the same, overwriting is performed, which is equivalent to updating InfluxDB.
Use the influxql statement to implement the delete syntax, for example:
Client. query ('delete from students; ') # delete data
A complete example of data operations is as follows:
#! /Usr/bin/env python #-*-coding: UTF-8-*-from influxdb import InfluxDBClientjson_body = [{"measurement": "students", "tags ": {"stuid": "s123" },# "time": "2017-03-12T22: 00: 00Z", "fields": {"score ": 89 }}] def showDatas (client): result = client. query ('select * from students; ') print ("Result: {0 }". format (result) client = InfluxDBClient ('localhost', 8086, 'root', '', 'testdb') # initialize the client. write_points (json_body) # Write Data to showDatas (client) # query data client. query ('delete from students; ') # delete data showDatas (client) # query data
Okay, that's all. I hope it will help you.
Github address:
Bytes
Please add