python鏈接,操做 InfluxDB

 

準備工做

  • 啓動服務器

  執行以下命令:python

  service influxdb start

  示例以下:git

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

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

安裝pip : 數據庫

yum install python-pip

安裝influxdb-python :json

pip install influxdb

基本操做

使用InfluxDBClient類操做數據庫,示例以下:服務器

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

  使用get_list_database函數,示例以下:函數

  print client.get_list_database() # 顯示全部數據庫名稱ui

  • 建立新數據庫

  使用create_database函數,示例以下:spa

  client.create_database(‘testdb‘) # 建立數據庫code

  • 刪除數據庫

  使用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)  # 查詢數據
相關文章
相關標籤/搜索