什麼是即時運算?簡單說,就是資料產生的那一刻就被處理、分析並產出結果,無需等待離線批次作業。本文以「20 分鐘內熱銷商品 Top 10」為例,剖析傳統架構的痛點,並示範如何用 Storm 實現滑動窗口計數,揭開流式計算的神祕面紗。
核心思想:世界發生的事是即時的,計算模型也應該是即時的。 採用 Storm 等流式引擎,具備原生水平擴展、容錯與低延遲特性,免去手動分片與一致性煩惱。
# 包裹的字串,如 #世界#、#微博#。Storm 內建 TOPOLOGY_TICK_TUPLE_FREQ_SECS,Executor 定期發送 System Tick Tuple,下游 Bolt 收到即觸發窗口滾動與結果發射。
public static boolean isTick(Tuple tuple) {
return tuple != null
&& Constants.SYSTEM_COMPONENT_ID.equals(tuple.getSourceComponent())
&& Constants.SYSTEM_TICK_STREAM_ID.equals(tuple.getSourceStreamId());
}
builder.setSpout("wordGenerator", new TestWordSpout(), 5);
builder.setBolt("counter", new RollingCountBolt(9, 3), 4)
.fieldsGrouping("wordGenerator", new Fields("word"));
builder.setBolt("intermediateRanker", new IntermediateRankingsBolt(TOP_N), 4)
.fieldsGrouping("counter", new Fields("obj"));
builder.setBolt("totalRanker", new TotalRankingsBolt(TOP_N))
.globalGrouping("intermediateRanker");
最終輸出即為 t0 - t1 時段內的熱門話題榜單,完美解決「這個世界正在發生什麼」的即時洞察需求。