環境: CentOS6.5_x64
InfluxDB版本:1.1.0
Python版本:2.6
[root@localhost ~]# service influxdb start
Starting influxdb...
influxdb process was started [ OK ]
[root@localhost ~]#
influxdb-python
#github位址:
https://github.com/influxdata/influxdb-python
yum install python-pip
安裝influxdb-python :
pip install influxdb
from influxdb import InfluxDBClient client = InfluxDBClient('localhost', 8086, 'root', '', '') # 初始化
print client.get_list_database() # 顯示所有資料庫名稱
client.create_database('testdb')#建立資料庫
資料庫 使用drop_database函數,範例如下: client.drop_database('testdb') # 刪除資料庫#! /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) # 写入数据,同时创建表
##可以透過influxql語句實現,範例如下:
client.query("drop measurement 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 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) # 写入数据
可以透過influxql語句實現,範例如下:
result = client.query('select * from students;') print("Result: {0}".format(result))
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) # 查询数据
以上是詳解使用python操作InfluxDB方法的詳細內容。更多資訊請關注PHP中文網其他相關文章!