InfluxDB——python使用手冊

InfluxDB——python使用手冊

AStdAS.png

準備工做

安裝InfluxDB:html

請參考筆者相關博文:Centos7安裝InfluxDB1.7python

安裝pip :數據庫

yum install python-pip

安裝influxdb-python :緩存

pip install influxdb

實際上py的influx官方包的doc也已經足夠詳細,值得過一遍:py-influxdbbash

基本操做

使用InfluxDBClient類操做數據庫,示例以下:函數

# 初始化
client = InfluxDBClient('localhost', 8086, 'your_username', 'yuor_password', 'your_dbname')
  • 顯示已存在的全部數據庫

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

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

  • 建立新數據庫

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

  client.create_database('testdb') # 建立數據庫htm

  • 刪除數據庫

  使用drop_database函數,示例以下:

  client.drop_database('testdb') # 刪除數據庫

數據庫操做完整示例以下:

from influxdb import InfluxDBClient

# 初始化(指定要操做的數據庫)
client = InfluxDBClient('localhost', 8086, 'your_username', 'yuor_password', 'your_dbname') 
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, 'your_username', 'yuor_password', 'your_dbname')
  • 顯示指定數據庫中已存在的表

  能夠經過influxql語句實現,示例以下:

result = client.query('show measurements;') # 顯示數據庫中的表
print("Result: {0}".format(result))
  • 建立新表並添加數據

InfluxDB沒有提供單獨的建表語句,能夠經過並添加數據的方式建表,示例以下:

current_time = datetime.datetime.utcnow().isoformat("T")
body = [
    {
        "measurement": "students",
        "time": current_time,
        "tags": {
            "class": 1
        },
        "fields": {
            "name": "Hyc",
            "age": 3
        },
    }
]

res = client.write_points(body)
  • 刪除表

能夠經過influxql語句實現,示例以下:

client.query("drop measurement students") # 刪除表

數據表操做完整示例以下:

import datetime
from influxdb import InfluxDBClient


client = InfluxDBClient('localhost', 8086, 'your_username', 'yuor_password', 'your_dbname') 
current_time = datetime.datetime.utcnow().isoformat("T")
body = [
    {
        "measurement": "students",
        "time": current_time,
        "tags": {
            "class": 1
        },
        "fields": {
            "name": "Hyc",
            "age": 3
        },
    }
]
res = client.write_points(body)
client.query("drop measurement students")

數據操做

InfluxDBClient中要指定鏈接的數據庫,示例以下:

# 初始化(指定要操做的數據庫)
client = InfluxDBClient('localhost', 8086, 'your_username', 'yuor_password', 'your_dbname')
  • 添加

通過筆者測試write_points至關於其它數據庫的批量寫入操做,建議處理大量數據是對數據進行緩存後利用write_points一次批量寫入。

能夠經過write_points實現,示例以下:

body = [
    {
        "measurement": "students",
        "time": current_time,
        "tags": {
            "class": 1
        },
        "fields": {
            "name": "Hyc",
            "age": 3
        },
    },
    {
        "measurement": "students",
        "time": current_time,
        "tags": {
            "class": 2
        },
        "fields": {
            "name": "Ncb",
            "age": 21
        },
    },
]
res = client.write_points(body)
  • 查詢

能夠經過influxql語句實現,示例以下:

result = client.query('select * from students;')
print("Result: {0}".format(result))
  • 更新

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

  • 刪除

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

client.query('delete from students;') # 刪除數據

參考文章

InfluDB官方文檔:https://docs.influxdata.com/influxdb/v1.7/introduction/installation/
python-influx doc:https://influxdb-python.readthedocs.io/en/latest/include-readme.html
Mike_Zhang:使用python操做InfluxDB

相關文章
相關標籤/搜索