InfluxDB——python使用手冊


InfluxDB——python使用手冊

AStdAS.png

准備工作

安裝InfluxDB:

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

安裝pip :

yum install python-pip

安裝influxdb-python :

pip install influxdb 

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

基本操作

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

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

  使用get_list_database函數,示例如下:

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

  • 創建新數據庫

  使用create_database函數,示例如下:

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

  • 刪除數據庫

  使用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


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM