Home >Backend Development >Python Tutorial >Detailed explanation of how to operate InfluxDB using python
Environment: CentOS6.5_x64
InfluxDB version: 1.1.0
PythonVersion: 2.6
Start the server
Execute the following command:
service influxdb start
The example is as follows:
[root@localhost ~]# service influxdb start Starting influxdb... influxdb process was started [ OK ] [root@localhost ~]#
Installationinfluxdb-python
##github address: https://github.com/influxdata/influxdb-python
Install pip:yum install python-pipInstall influxdb-python:
pip install influxdbBasic operationsUse the InfluxDBClient class operation Database, the example is as follows:
from influxdb import InfluxDBClient client = InfluxDBClient('localhost', 8086, 'root', '', '') # 初始化
Function, the example is as follows:
print client.get_list_database() # Display all database names
Deletedatabase
Database operationThe complete example is as follows:
#! /usr/bin/env python #-*- coding:utf-8 -*- from influxdb import InfluxDBClient client = InfluxDBClient('localhost', 8086, 'root', '', '') # 初始化 print client.get_list_database() # 显示所有数据库名称 client.create_database('testdb') # 创建数据库 print client.get_list_database() # 显示所有数据库名称 client.drop_database('testdb') # 删除数据库 print client.get_list_database() # 显示所有数据库名称Table operationInfluxDBClient needs to specify the database to connect to, the example is as follows:
client = InfluxDBClient('localhost', 8086, 'root', '', 'testdb') # 初始化(指定要操作的数据库)
result = client.query('show measurements;') # 显示数据库中的表print("Result: {0}".format(result))
json_body = [ { "measurement": "students", "tags": { "stuid": "s123" }, #"time": "2017-03-12T22:00:00Z", "fields": { "score": 89 } } ] client = InfluxDBClient('localhost', 8086, 'root', '', 'testdb') # 初始化(指定要操作的数据库) client.write_points(json_body) # 写入数据,同时创建表
client.query("drop measurement students") # 删除表
Data table operationThe complete example is as follows:
#! /usr/bin/env python #-*- coding:utf-8 -*- from influxdb import InfluxDBClient json_body = [ { "measurement": "students", "tags": { "stuid": "s123" }, #"time": "2017-03-12T22:00:00Z", "fields": { "score": 89 } } ] def showDBNames(client): result = client.query('show measurements;') # 显示数据库中的表 print("Result: {0}".format(result)) client = InfluxDBClient('localhost', 8086, 'root', '', 'testdb') # 初始化(指定要操作的数据库) showDBNames(client) client.write_points(json_body) # 写入数据,同时创建表 showDBNames(client) client.query("drop measurement students") # 删除表 showDBNames(client)Data operationInfluxDBClient, you need to specify the database to connect to. The example is as follows:
client = InfluxDBClient('localhost', 8086, 'root', '', 'testdb') # 初始化(指定要操作的数据库)
json_body = [ { "measurement": "students", "tags": { "stuid": "s123" }, #"time": "2017-03-12T22:00:00Z", "fields": { "score": 89 } } ] client.write_points(json_body) # 写入数据can be implemented through influxql statements, the example is as follows:
result = client.query('select * from students;') print("Result: {0}".format(result))When tags is the same as timestamp, the data will perform an overwrite operation, which is equivalent to the update operation of InfluxDB.
delete syntax, the example is as follows:
client.query('delete from students;') # 删除数据The complete example of data operation is as follows:
#! /usr/bin/env python #-*- coding:utf-8 -*- from influxdb import InfluxDBClient json_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') # 初始化 client.write_points(json_body) # 写入数据 showDatas(client) # 查询数据 client.query('delete from students;') # 删除数据 showDatas(client) # 查询数据Okay, that’s it, I hope it will be helpful to you.
The above is the detailed content of Detailed explanation of how to operate InfluxDB using python. For more information, please follow other related articles on the PHP Chinese website!