本文旨在協助熟悉 Splunk 的使用者學習 Kusto 查詢語言,以便使用 Kusto 撰寫日誌查詢。 會直接比較兩者,突顯主要差異與相似之處,讓你能在現有知識基礎上持續發展。
結構與概念
下表比較了 Splunk 與 Kusto 日誌之間的概念與資料結構:
| 概念 | Splunk | Kusto | 評論 |
|---|---|---|---|
| 部署單位 | 叢集 | 叢集 | Kusto 允許任意跨叢集查詢。 Splunk 並不這樣。 |
| 資料快取 | buckets | 快取與保留政策 | 控制資料的週期與快取等級。 此設定直接影響查詢效能及部署成本。 |
| 資料的邏輯分割 | 索引 | 資料庫 | 允許資料的邏輯分離。 這兩種實作都允許跨分區進行聯集和連接。 |
| 結構化事件元資料 | N/A | 表格 | Splunk 並未將事件元資料的概念暴露給搜尋語言。 Kusto 日誌有表格的概念,該表格包含欄位。 每個事件實例都映射到一列。 |
| 資料列 | 事件 | 列 | 僅有術語變化。 |
| 記錄屬性 | 欄位 | column | 在 Kusto 中,這個設定是預先定義的,作為表格結構的一部分。 在 Splunk 中,每個事件都有自己的欄位集合。 |
| 類型 | 資料類型 | 資料類型 | Kusto 的資料型態更明確,因為它們是設定在欄位上的。 兩者都能動態處理資料型態及大致相當的資料型態集合,包括 JSON 支援。 |
| 查詢與搜尋 | 搜尋 | 查詢 | Kusto 和 Splunk 的概念基本上是一樣的。 |
| 事件導入時間 | 系統時間 | ingestion_time() |
在 Splunk 中,每個事件會獲得該事件被索引的時間戳記。 在 Kusto 中,你可以定義一個名為 ingestion_time 的政策,該政策會暴露一個系統欄位,可以透過 ingestion_time() 函式來參考。 |
Functions
下表指定了 Kusto 中等價於 Splunk 函數的函數。
| Splunk | Kusto | 評論 |
|---|---|---|
strcat |
strcat() |
(1) |
split |
split() |
(1) |
if |
iff() |
(1) |
tonumber |
todouble()tolong()toint() |
(1) |
upperlower |
toupper()tolower() |
(1) |
replace |
replace_string()、replace_strings() 或 replace_regex() |
(1) 雖然 replace 函數在兩個產品中都包含三個參數,但這些參數不同。 |
substr |
substring() |
(1) 請注意,Splunk 使用以一為基礎的索引。 Kusto 標註了零基指數。 |
tolower |
tolower() |
(1) |
toupper |
toupper() |
(1) |
match |
matches regex |
(2) |
regex |
matches regex |
在 Splunk 中,regex 是一個操作符。 在 Kusto 裡,它是一個關聯運算子。 |
searchmatch |
== | 在 Splunk 中,允許 searchmatch 搜尋精確的字串。 |
random |
rand() 隨機函數(rand)(n) |
Splunk 函數回傳一個介於 0 到 231-1 之間的數字。 Kusto 回傳的數字介於 0.0 到 1.0 之間,若提供參數則介於 0 到 n-1 之間。 |
now |
now() |
(1) |
relative_time |
totimespan() |
(1) 在 Kusto 中,Splunk 的 relative_time(datetimeVal, offsetVal) 等價物是 datetimeVal + totimespan(offsetVal)。例如,變成 search | eval n=relative_time(now(), "-1d@d")... | extend myTime = now() - totimespan("1d")。 |
(1) 在 Splunk 中,該函數是透過使用 eval 運算子來調用的。 在 Kusto 中,它被用作 extend 或 project.
(2) 在 Splunk 中,函數是透過使用 運算 eval 子來呼叫的。 在 Kusto 中,它可以與操作符一起使用 where 。
運營商
以下章節將示範如何在 Splunk 和 Kusto 中使用不同運算子。
備註
以下範例中,Splunk 欄位 rule 對應到 Kusto 中的一個資料表,而 Splunk 預設的時間戳對應到 Logs Analytics ingestion_time() 欄位。
搜尋
在 Splunk 中,你可以省略 search 關鍵字並指定一個不引號的字串。 在 Kusto 中,你必須以 find開始每個查詢,一個未引號字串是欄位名稱,查詢值必須是引號字串。
| Product | Operator | Example |
|---|---|---|
| Splunk | search |
search Session.Id="c8894ffd-e684-43c9-9125-42adc25cd3fc" earliest=-24h |
| Kusto | find |
find Session.Id=="c8894ffd-e684-43c9-9125-42adc25cd3fc" and ingestion_time()> ago(24h) |
Filter
Kusto 日誌查詢從套用 filter 的一個表格結果集開始。 在 Splunk 中,過濾是目前索引的預設操作。 你也可以在 Splunk 中使用這個 where 運算子,但我們不建議這麼做。
| Product | Operator | Example |
|---|---|---|
| Splunk | search |
Event.Rule="330009.2" Session.Id="c8894ffd-e684-43c9-9125-42adc25cd3fc" _indextime>-24h |
| Kusto | where |
Office_Hub_OHubBGTaskError| where Session_Id == "c8894ffd-e684-43c9-9125-42adc25cd3fc" and ingestion_time() > ago(24h) |
收集 n 個事件或列供檢查
Kusto 日誌查詢也支援 take 作為 limit 的別名。 在 Splunk 中,若結果有序,則 head 回傳前 n 個結果。 在 Kusto 中, limit 不會被排序,但它會回傳找到的前 n 列。
| Product | Operator | Example |
|---|---|---|
| Splunk | head |
Event.Rule=330009.2| head 100 |
| Kusto | limit |
Office_Hub_OHubBGTaskError| limit 100 |
取得最早的 n 個事件或列,依欄位或欄排序
在 Splunk 中,使用 tail 來顯示底部結果。 在 Kusto 中,你可以用 asc來指定排序方向。
| Product | Operator | Example |
|---|---|---|
| Splunk | head |
Event.Rule="330009.2"| sort Event.Sequence| head 20 |
| Kusto | top |
Office_Hub_OHubBGTaskError| top 20 by Event_Sequence |
擴充結果集,新增欄位或欄位
Splunk 有 eval 函數,但與 Kusto 中的 eval 運算子無法相比。
eval Splunk 中的運算子與 extend Kusto 中的運算子都僅支援純量函數與算術運算子。
| Product | Operator | Example |
|---|---|---|
| Splunk | eval |
Event.Rule=330009.2| eval state= if(Data.Exception = "0", "success", "error") |
| Kusto | extend |
Office_Hub_OHubBGTaskError| extend state = iff(Data_Exception == 0,"success" ,"error") |
Rename
Kusto 使用運算 project-rename 子來重新命名欄位。 在運算子 project-rename 中,查詢可以利用欄位預先建立的任何索引。 Splunk 也有 rename 類似的運算子。
| Product | Operator | Example |
|---|---|---|
| Splunk | rename |
Event.Rule=330009.2| rename Date.Exception as execption |
| Kusto | project-rename |
Office_Hub_OHubBGTaskError| project-rename exception = Date_Exception |
賽制結果與預測
Splunk 使用指令 table 來選擇要包含在結果中的欄位。 Kusto 有一個project運算子可以做到相同的事,還能做得更多。
| Product | Operator | Example |
|---|---|---|
| Splunk | table |
Event.Rule=330009.2| table rule, state |
| Kusto | project |
Office_Hub_OHubBGTaskError| project exception, state |
Splunk 使用指令 fields - 來選擇要排除哪些欄位。 Kusto 有一個 project-away 運算子可以執行相同的操作。
| Product | Operator | Example |
|---|---|---|
| Splunk | fields - |
Event.Rule=330009.2| fields - quota, hightest_seller |
| Kusto | project-away |
Office_Hub_OHubBGTaskError| project-away exception, state |
Aggregation
請參閱可用的 彙整彙總函數列表 。
| Splunk 運算元 | Splunk 範例 | Kusto 運算元 | Kusto 範例 |
|---|---|---|---|
stats |
search (Rule=120502.*)| stats count by OSEnv, Audience |
summarize |
Office_Hub_OHubBGTaskError| summarize count() by App_Platform, Release_Audience |
evenstats |
... | stats count_i by time, category | eventstats sum(count_i) AS count_total by _time_ |
join |
T2 | join kind=inner (T1) on _time | project _time, category, count_i, count_total |
加入
join 在 Splunk 中存在相當大的限制。 子查詢的結果數量限制為 10,000 個(在部署設定檔中設定),且可用的 join flavor 數量有限。
| Product | Operator | Example |
|---|---|---|
| Splunk | join |
Event.Rule=120103* | stats by Client.Id, Data.Alias | join Client.Id max=0 [search earliest=-24h Event.Rule="150310.0" Data.Hresult=-2147221040] |
| Kusto | join |
cluster("OAriaPPT").database("Office PowerPoint").Office_PowerPoint_PPT_Exceptions| where Data_Hresult== -2147221040| join kind = inner (Office_System_SystemHealthMetadata| summarize by Client_Id, Data_Alias)on Client_Id |
排序
預設排序順序為遞增。 要指定降序,在欄位名稱前加上負號(-)。 Kusto 也支援定義 null 的位置,可以放在開頭或結尾。
| Product | Operator | Example |
|---|---|---|
| Splunk | sort |
Event.Rule=120103| sort -Data.Hresult |
| Kusto | order by |
Office_Hub_OHubBGTaskError| order by Data_Hresult, desc |
多值擴展
多值擴展運算子在 Splunk 和 Kusto 中都類似。
| Product | Operator | Example |
|---|---|---|
| Splunk | mvexpand |
mvexpand solutions |
| Kusto | mv-expand |
mv-expand solutions |
結果面向、有趣的領域
在 Azure 入口網站的 Log Analytics 中,只有第一欄會被揭露。 所有欄位皆可透過 API 取得。
| Product | Operator | Example |
|---|---|---|
| Splunk | fields |
Event.Rule=330009.2| fields App.Version, App.Platform |
| Kusto | facets |
Office_Excel_BI_PivotTableCreate| facet by App_Branch, App_Version |
資料去重複
在 Kusto 裡,你可以用 summarize arg_min() 來反轉選擇哪張唱片的順序。
| Product | Operator | Example |
|---|---|---|
| Splunk | dedup |
Event.Rule=330009.2| dedup device_id sortby -batterylife |
| Kusto | summarize arg_max() |
Office_Excel_BI_PivotTableCreate| summarize arg_max(batterylife, *) by device_id |
時序圖
Kusto 和 Splunk 都使用 timechart 運算子來視覺化隨時間推移的資料。 在 Splunk 中,它會彙整指定時間區間的資料,並可搭配各種統計函數使用。 在 Kusto 中,等價的操作是透過 summarize and bin 函數,接著是 渲染時間表 運算子來實現。
| Product | Operator | Example |
|---|---|---|
| Splunk | timechart |
index=StormEvents | where StartTime >= "2007-01-01" AND StartTime <= "2007-12-31" AND DamageCrops > 0 | bin span=7d StartTime | stats count as EventCount by StartTime | timechart span=7d count as EventCount |
| Kusto | timechart |
StormEvents | where StartTime between (datetime(2007-01-01) .. datetime(2007-12-31)) and DamageCrops > 0 | summarize EventCount = count() by bin(StartTime, 7d) | render timechart |
相關內容
- 請先看 Kusto 查詢語言的教學。