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-pip
pip install influxdb
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() # 显示所有数据库名称
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)
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) # 写入数据
result = client.query('select * from students;') print("Result: {0}".format(result))
delete syntax, the example is as follows:
client.query('delete from students;') # 删除数据
#! /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) # 查询数据
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!