首頁  >  文章  >  後端開發  >  詳解使用python操作InfluxDB方法

詳解使用python操作InfluxDB方法

高洛峰
高洛峰原創
2017-03-14 15:28:0111316瀏覽

環境: CentOS6.5_x64
InfluxDB版本:1.1.0
Python版本:2.6

##準備工作

    啟動伺服器
  •   執行以下指令:

      service influxdb start
  •   範例如下:

[root@localhost ~]# service influxdb start
Starting influxdb...
influxdb process was started [ OK ]
[root@localhost ~]#

#安裝

influxdb-python

#git

hub位址:

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方法的詳細內容。更多資訊請關注PHP中文網其他相關文章!

陳述:
本文內容由網友自願投稿,版權歸原作者所有。本站不承擔相應的法律責任。如發現涉嫌抄襲或侵權的內容,請聯絡admin@php.cn