蒐集極簡遞增資料

使用 Python 分析 IoT 資料

Matthias Voppichler

IT Developer

什麼是快取?

資料儲存

  • 完成資料流蒐集後
  • 一筆一筆寫入
    • 對磁碟造成高負載
  • 使用快取
使用 Python 分析 IoT 資料

快取

cache = []

def on_message(client, userdata, message): data = json.loads(message.payload) cache.append(data)
if len(cache) > MAX_CACHE:
with Path("data.txt").open("a") as f: f.writelines(cache) cache.clear()
# Connect function to mqtt datastream subscribe.callback(on_message, topics="datacamp/energy", hostname=MQTT_HOST)
使用 Python 分析 IoT 資料

簡易資料流

C331,6020
M640,104
C331,6129
M640,180
C331,6205
M640,256
使用 Python 分析 IoT 資料

觀測時間戳

  • 「payload 內的 timestamp」
  • message.timestamp
  • datetime.now()
使用 Python 分析 IoT 資料

觀測時間戳

def on_message(client, userdata, message):
    publishtime = message.timestamp

consume_time = datetime.utcnow()
使用 Python 分析 IoT 資料

pd.to_datetime()

print(df.head())
   timestamp         device  val
0  1540535443083       C331  347069.305500
1  1540535460858       C331  347069.381205
import pandas as pd
df["timestamp"] = pd.to_datetime(df["timestamp"], unit="ms")
  timestamp                  device  val
0 2018-10-26 06:30:43.083      C331  347069.305500
1 2018-10-26 06:31:00.858      C331  347069.381205
使用 Python 分析 IoT 資料

一起來練習吧!

使用 Python 分析 IoT 資料

Preparing Video For Download...