收集精简的增量数据

用 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 数据

Passons à la pratique !

用 Python 分析 IoT 数据

Preparing Video For Download...