1. 程式人生 > >python連接,操作 InfluxDB

python連接,操作 InfluxDB

pan write https 服務器 同時 刪除表 class 操作 href

準備工作

  • 啟動服務器

  執行如下命令:

  service influxdb start

  示例如下:

[root@localhost ~]# service influxdb start
Starting influxdb...
influxdb process was started [ OK ]
[root@localhost ~]#
  • 安裝influxdb-python

github地址: https://github.com/influxdata/influxdb-python

安裝pip :

yum install python-pip

安裝influxdb-python :

pip install influxdb 

基本操作

使用InfluxDBClient類操作數據庫,示例如下:

from influxdb import InfluxDBClient
client = InfluxDBClient(localhost, 8086, root, ‘‘, ‘‘) # 初始化
  • 顯示已存在的所有數據庫

  使用get_list_database函數,示例如下:

  print client.get_list_database() # 顯示所有數據庫名稱

  • 創建新數據庫

  使用create_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() # 顯示所有數據庫名稱

表操作

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": "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)

數據操作

InfluxDBClient中要指定連接的數據庫,示例如下:

client = InfluxDBClient(localhost, 8086, root, ‘‘, testdb) # 初始化(指定要操作的數據庫)
  • 添加

可以通過write_points實現,示例如下:

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))
  • 更新

tags 和 timestamp相同時數據會執行覆蓋操作,相當於InfluxDB的更新操作。

  • 刪除

使用influxql語句實現,delete語法,示例如下:

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