Environment: centos6.5_x64
Influxdb version: 1.1.0
Python version: 2.6
Preparatory work
Execute the following command:
service influxdb start
Examples are as follows:
[Email protected] ~~]#
- Installing Influxdb-python
GitHub Address: Https://github.com/influxdata/influxdb-python
Install PIP:
yum install python-pip
Install Influxdb-python:
Basic operations
Use the Influxdbclient class to manipulate the database with the following example:
= Influxdbclient ('localhost'8086'root' ") # Initialization
- Show all databases that already exist
Using the Get_list_database function, the example is as follows:
Print Client.get_list_database () # Show all database names
Using the Create_database function, the example is as follows:
Client.create_database (' TestDB ') # CREATE Database
Using the Drop_database function, the example is as follows:
Client.drop_database (' TestDB ') # Delete database
A complete example of database operations is as follows:
#!/usr/bin/env python#-*-coding:utf-8-*- fromInfluxdbImportinfluxdbclientclient= Influxdbclient ('localhost', 8086,'Root',"',"')#InitializePrintClient.get_list_database ()#Show all database namesClient.create_database ('TestDB')#Create a databasePrintClient.get_list_database ()#Show all database namesClient.drop_database ('TestDB')#Deleting a databasePrintClient.get_list_database ()#Show all database names
Table Operations
Influxdbclient to specify a connected database in the following example:
Client = influxdbclient ('localhost'root') testdb'# initialization (specifies the database to be manipulated)
- Displays tables that already exist in the specified database
This can be achieved through the INFLUXQL statement, as follows:
result = Client.query ('show measurements; ' # displaying tables in a database Print ("Result: {0}". Format (Result))
- Create a new table and add data
INFLUXDB does not provide a separate build statement, and you can build the table by adding data, as shown in the following example:
Json_body = [ { "Measurement":"Students", "Tags": { "Stuid":"s123" }, #"Time ": "2017-03-12t22:00:00z", " Fields": { "score": 89}}]client= Influxdbclient ('localhost', 8086,'Root',"','TestDB')#Initialize (specifies the database to be manipulated)Client.write_points (Json_body)#writing data, creating tables at the same time
This can be achieved through the INFLUXQL statement, as follows:
Client.query ("drop measurement students"# Delete Table
A complete example of a data table operation is as follows:
#!/usr/bin/env python#-*-coding:utf-8-*- fromInfluxdbImportInfluxdbclientjson_body= [ { "Measurement":"Students", "Tags": { "Stuid":"s123" }, #"Time ": "2017-03-12t22:00:00z", " Fields": { "score": 89 } }]defshowdbnames (client): Result= Client.query ('show measurements;')#displaying tables in a database Print("Result: {0}". Format (Result) client= Influxdbclient ('localhost', 8086,'Root',"','TestDB')#Initialize (specifies the database to be manipulated)showdbnames (client) client.write_points (json_body)#writing data, creating tables at the same timeshowdbnames (client) client.query ("Drop Measurement Students")#Delete a tableShowdbnames (client)
Data manipulation
Influxdbclient to specify a connected database in the following example:
Client = influxdbclient ('localhost'root') testdb'# initialization (specifies the database to be manipulated)
This can be achieved through write_points, with the following examples:
Json_body = [ { "Measurement":"Students", "Tags": { "Stuid":"s123" }, #"Time ": "2017-03-12t22:00:00z", " Fields": { "score": 89}}]client.write_points (Json_body)#Write Data
This can be achieved through the INFLUXQL statement, as follows:
result = Client.query ('select * from students; ' ) print("Result: {0}". Format (Result))
tags and timestamp the same time data will perform overwrite operations, equivalent to INFLUXDB update operations.
Using the INFLUXQL statement implementation, the delete syntax is shown in the following example:
Client.query ('delete from students; ' # Delete Data
A complete example of data manipulation is as follows:
#!/usr/bin/env python#-*-coding:utf-8-*- fromInfluxdbImportInfluxdbclientjson_body= [ { "Measurement":"Students", "Tags": { "Stuid":"s123" }, #"Time ": "2017-03-12t22:00:00z", " Fields": { "score": 89 } }]defshowdatas (client): Result= Client.query ('SELECT * from students;') Print("Result: {0}". Format (Result) client= Influxdbclient ('localhost', 8086,'Root',"','TestDB')#InitializeClient.write_points (Json_body)#Write DataShowdatas (client)#Querying DataClient.query ('delete from students;')#Delete DataShowdatas (client)#Querying Data
All right, that's it, I hope it helps you.
This article GitHub address:
https://github.com/mike-zhang/mikeBlogEssays/blob/master/2017/20170312_ using python operations influxdb.md
Welcome to Supplement
Using Python to manipulate influxdb