This article explains how to use python to operate the InfluxDB method 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
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)
You can use the influxql statement, for example:
Result = client. query ('Show measurements; ') # display the print ("Result: {0}". format (result) in the database ))
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.
The above is a detailed description of how to use python to operate the InfluxDB method. For more information, see other related articles in the first PHP community!