安裝依賴包:influxdb
from influxdb import InfluxDBClient
client = InfluxDBClient('localhost', 8086, 'root', '', '')
**# client = InfluxDBClient(host, port, user, password, database) # 初始化**
操作數據庫
顯示已存在的所有數據庫
使用get_list_database函數,示例如下:
print client.get_list_database() # 顯示所有數據庫名稱
創建新數據庫
使用create_database函數,示例如下:
client.create_database(‘testdb’) # 創建數據庫
刪除數據庫
使用drop_database函數,示例如下:
client.drop_database(‘testdb’) # 刪除數據庫
#-*- 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() # 顯示所有數據庫名稱
操作數據庫
顯示已存在的所有數據庫
使用get_list_database函數,示例如下:
print client.get_list_database() # 顯示所有數據庫名稱
創建新數據庫
使用create_database函數,示例如下:
client.create_database(‘testdb’) # 創建數據庫
刪除數據庫
使用drop_database函數,示例如下:
client.drop_database(‘testdb’) # 刪除數據庫
表操作
InfluxDBClient中要指定連接的數據庫,示例如下:
client = InfluxDBClient(‘localhost’, 8086, ‘root’, ‘’, ‘testdb’) # 初始化(指定要操作的數據庫)
顯示指定數據庫中已存在的表
可以通過influxql語句實現,示例如下:
result = client.query(‘show measurements;’) # 顯示數據庫中的表
print(“Result: {0}”.format(result))
創建新表並添加數據
InfluxDB沒有提供單獨的建表語句,可以通過並添加數據的方式建表,示例如下:
json_body = [
{
“measurement”: “students”,
“tags”: {
“stuid”: “s123”
},
“time”: “2019-06-13T22:00:00Z”,
“fields”: {
“score”: 89
}
}
]
client = InfluxDBClient(‘localhost’, 8086, ‘root’, ‘’, ‘testdb’) # 初始化(指定要操作的數據庫)
client.write_points(json_body) # 寫入數據,同時創建表
刪除表
可以通過influxql語句實現,示例如下:
client.query(“drop measurement students”) # 刪除表
#-*- coding:utf-8 -*-
from influxdb import InfluxDBClient
json_body = [
{
"measurement": "students",
"tags": {
"stuid": "s123"
},
"time": "2019-06-13T22: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)
數據操作
InfluxDBClient中要指定連接的數據庫,示例如下:
client = InfluxDBClient(‘localhost’, 8086, ‘root’, ‘’, ‘testdb’) # 初始化(指定要操作的數據庫)
添加
可以通過write_points實現,示例如下:
json_body = [
{
“measurement”: “students”,
“tags”: {
“stuid”: “s123”
},
“time”: “2019-06-13T22:00:00Z”,
“fields”: {
“score”: 89
}
}
]
client.write_points(json_body) # 寫入數據
查詢
可以通過influxql語句實現,示例如下:
result = client.query(‘select * from students;’)
print(“Result: {0}”.format(result))
更新
tags 和 timestamp相同時數據會執行覆蓋操作,相當於InfluxDB的更新操作。
刪除
使用influxql語句實現,delete語法,示例如下:
client.query(‘delete from students;’) # 刪除數據
數據操作完整示例如下:
#-*- coding:utf-8 -*-
from influxdb import InfluxDBClient
json_body = [
{
"measurement": "students",
"tags": {
"stuid": "s123"
},
"time": "2019-06-13T22: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) # 查詢數據