--- tags: python, influxdb, grafana, note --- # Python + InfluxDB + Grafana (Docker) 因為懶、又只是測試,所以使用 `Docker` 跑 InfluxDB 和 Grafana。 ## Technical versions - python3.7 - influxdb:latest (INFLUXDB_VERSION=1.8.0) - grafana/grafana:6.7.2-ubuntu ## InfluxDB Basic DB應用不外乎要知道資料結構和如何寫入、讀取。 InfluxDB有幾個重要的詞: **timestamp** 因為是時序型資料庫,想必這個非常重要。 **DB會自帶寫入時間**,所以寫入的時候`_time`這欄是可以空白的。 **measurement** `字串` 可以把他想成關聯式資料庫裡的Table。 **tags** `key-value pair` 一段資料的標籤,可以拿標籤去篩出需要的資料。 **fields** `key-value pair` 一段資料的欄位,可以拿欄位去篩出需要的資料,可以說是關聯式資料庫裡的column & data。 **series** series key像是Query `measurement, tag_key=tag_val field_key=field_val` series含有篩出來的資料。 ``` 2019-08-18T00:00:00Z 23 2019-08-18T00:02:00Z 28 ``` **point** 可以把他想成關聯式資料庫裡的Row。 含有_time, measurment, tags, fields **bucket** 所有資料都存在一個bucket,一個bucket屬於一個orgnization。 每個bucket都有一個保留時間。 **organization** 一個包含一組使用者、多的dashboards, tasks, buckets的工作空間。 ### 操作起來! 寫入資料有滿多方法的,有GUI、influx CLI、influx API(HTTP request),透過Telegraf, InfluxDB scrapers,當然也有InfluxQL。 下面說的是`InfluxQL`。 - 先啟動container,並進入influx ```bash $ docker run -p 8086:8086 -v influxdb:/var/lib/influxdb \ influxdb $ docker exec -it influxdb influx ``` - 建立DB並指定在該DB下操作 ```sql /* create database [dbname] */ create database production use production ``` - 建立一筆資料 不用先建立measurement唷~ > 注意 tag pair之間要用`,`分開,field pair也是。但tag set 和 field set之間是一個空白。 ```sql /* insert [measurement], [tag-set] [field-set] */ insert production_line, machine=001,sensor=meter_speed value=12 ``` - 查詢 ```sql /* select [field-key] from [measurement] */ select * from production_line /* select [field-key] from [measurement] where [tag-key]=[tag-value] */ select "value" from "production_line" where "machine"="001","sensor"="meter_speed" ``` - 刪除 measurement ```sql /* drop measurement [measurement]*/ drop measurement prodcution_line ``` - 時間 ```sql /* 前十秒至現在 */ select "value" from "production_line" where "machine"="001" and time > now() - 10s /* 現在至5周後 */ select "value" from "production_line" where "machine"="001" and time > now() and time < now() + 5w /* 第一筆和最後一筆 */ select first("value"), last("value") from "production_line" where "machine"="001" ``` ## InfluxDB-Python [GitHub](https://github.com/influxdata/influxdb-python) 上面有一些基礎的範例。 安裝很順利 ```bash $ pip install influxdb ``` - 連接InfluxDB ```python from influxdb import InfluxDBClient # 建立連線 client = InfluxDBClient('localhost', port, 'root', 'root', 'production') ``` - 操作起來 ```python # 新增DB client.create_database('production') # 要存入的資料! json_data = [ { "measurement": "production_line", "tags": { "machine": "001", "sensor": "meter_speed" }, "time": "2009-11-10T23:00:00Z", "fields": { "value": 12 } } ] # 寫入 client.write_points(json_data) # 讀取 result = client.query('select value from production_line;') ``` ## Grafana 不贅述什麼是Dashboard、Panels和各種關於Grafana的基礎操作。 - 先啟動container ```bash $ docker run -d -p 3000:3000 \ --name grafana grafana/grafana:<version number> ``` 在 `http://localhost:3000` 就能看到登入畫面。 預設帳密都是`admin`。 - 從`Add data source`新增InfluxDB的連接: **HTTP** | Field | Value | | ------ | --------------------- | | URL | http://localhost:8086 | | Access | Browser | **InfluxDB Details** | Field | Value | | ----------- | ---------- | | Database | production | | User | root | | Password | root | | HTTP Method | GET | **Save & Test** **新增 Panel** 在Query裡面設定Query,就和上面所述的讀取一模一樣。 **That's all!** ## References [InfluxDB 簡易操作](https://dotblogs.com.tw/dizzydizzy/2018/06/30/influxbasicoper) [InfluxDB|Yume 練功地](http://yume190.github.io/2016/01/06/InfluxDB/) [InfluxDB Docker](https://hub.docker.com/_/influxdb) [Run Grafana Docker Image](https://grafana.com/docs/grafana/latest/installation/docker/)