iT邦幫忙

第 11 屆 iThome 鐵人賽

DAY 29
0
自我挑戰組

從python入門到物聯網系列 第 29

Day29 - python 連接 InfluxDB

前言

前面我們實作了 Publish & Subscribe 收發資料,有了資料之後接下來就是要將它們儲存下來,我們常聽到的通常是關連式資料庫(Mysql),但是我們現在應用在物聯網的架構下,所以希望應用的是時序資料庫 Time Series Datebase - influxdb是一個開源分散式時序、事件和指標資料庫。

  • InfluxDB 三大特性:

    1. Time Series: 可以使用與時間有關的函數
    2. Metrics: 可以對大量數據進行計算
    3. Eevents: 支持任意的事件數據
  • InfluxDB名詞:

InfluxDB名詞 一般資料庫的概念
database 資料庫
measurement 資料庫中的表,就是table
points 表裡面的一行數據,就是 row
  • Point由時間戳(time)、標籤(tags)和值(field)組成。
Point屬性 一般資料庫的概念
time 每個數據記錄的時間,是資料庫自動生成的主索引
tags 各種有索引的屬性
field 各種記錄的值、數據資料

在EC2 安裝 InfluxDB

開啟EC2後在終端機執行

  1. add influxdb.key
curl -sL https://repos.influxdata.com/influxdb.key | sudo apt-key add -
echo "deb https://repos.influxdata.com/ubuntu trusty stable" | sudo tee /etc/apt/sources.list.d/influxdb.list
  1. 更新伺服器的套件管理
sudo apt update
  1. 安裝influxdb
sudo apt-get install influxdb

  1. 開啟 influxdb
sudo systemctl enable influxdb
sudo systemctl start influxdb
sudo service influxdb start

  1. 開啟 EC2 防火牆的 Port : 8086
    這是個重要的步驟,因為 influxdb 的 Port 預設是 8086,因此,要讓 EC2 允許外部裝置透過這個網路進行訊息傳送。

InfluxDB 基本用法

  • 進入 InfluxDB 指令模式
influx
  1. databases 操作
# 查看所有 DB
> show databases 

# 創建 testdb DB
> create database testdb

# 刪除 testdb DB
> drop database testdb

# 進入使用 testdb DB
> use testdb              
  1. measurement 操作
# 查看所有 measurement
> show measurements

# 刪除 testdb 這個 measurement (也就是刪除testdb表)
> drop measurement testdb

# 查看 testdb 這個 measurement 所有內容
> select * from testdb
  1. data 操作
    跟一般SQL 語法相同
    沒有更改和刪除功能
  • 離開 InfluxDB 指令模式
exit

python 連接 InfluxDB

  • 安裝influxdb-python
pip install influxdb

基本操作

database操作

  • 初始化連線設定
from influxdb import InfluxDBClient
client = InfluxDBClient('host', 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')

Try 創建/刪除 DB

from influxdb import InfluxDBClient
# 連線設定
# 初始化地端程式
client = InfluxDBClient('54.xx.xx.xx', 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())

結果:

# 一開始為空
[{'name': '_internal'}
# 新增 testdb
[{'name': '_internal'}, {'name': 'testdb'}]
# 刪除 testdb
[{'name': '_internal'} 

measurements操作

  • 顯示資料庫中目前有那些 measurement
client.query('show measurements') 
  • 創建measurement輸入數據
# 資料 (不用寫時間,InfluxDB會自動生成時間戳記)
data = [
    {
        "measurement": "Temperature",
        "tags": {
            "topic": "Sensor/Temperature"
        },
        "fields": {
            "tem": 25
        }
    }
]

# 寫入數據,同時創建表
client.write_points(data) 
  • 刪除measurement
client.query('drop measurement Temperature')

data 操作

  • 查詢 SQL語法
client.query('select * from Temperature')    
  • influxDB沒有提供資料的刪除與修改方法

Try 創建/刪除 measurement、查詢data

from influxdb import InfluxDBClient
# 連線設定
# 初始化(指定要操作的資料庫)
client = InfluxDBClient('54.xx.xx.xx', 8086, 'root', '', 'testdb') 

# 資料 (不用寫時間,InfluxDB會自動生成時間戳記)
data = [
    {
        "measurement": "Temperature",
        "tags": {
            "topic": "Sensor/Temperature"
        },
        "fields": {
            "tem": 25
        }
    }
]

# 寫入數據,同時創建表
client.write_points(data)

# 查詢 Temperature 表內容資料
result = client.query('select * from Temperature')  
print(list(result.get_points()))

# 刪除 measurement => Temperature
client.query('delete from Temperature') 

# 查詢 Temperature 表內容資料
result = client.query('select * from Temperature')  
print(list(result.get_points()))

結果:

# data 寫入的資料
[{'time': '2019-10-14T05:07:32.639472146Z', 'tem': 25, 'topic': 'Sensor/Temperature'}]
# 刪除表格後資料就空了
[]

在終端機用 influx 指令查看


明天就是鐵人賽的最後一天囉!/images/emoticon/emoticon08.gif
明天會把前面實作 Publish 之後 Subscribe 收到的資料上傳到今天介紹的 influxDB 上,以及利用python 把資料畫成圖表,最後會對於這30天的鐵人賽做整理與回顧,做個最後的summary總結。
即將完成鐵人啦!!! /images/emoticon/emoticon12.gif

參考資料

https://kknews.cc/code/k9baner.html

https://www.influxdata.com/


上一篇
Day28 - 使用 Python 接收後轉發訊息
下一篇
Day30 - python 連接 InfluxDB 應用
系列文
從python入門到物聯網30
圖片
  直播研討會
圖片
{{ item.channelVendor }} {{ item.webinarstarted }} |
{{ formatDate(item.duration) }}
直播中

1 則留言

我要留言

立即登入留言