語法符號說明
必要參數顯⽰在尖括號 < > 中。
可選參數顯⽰在⽅括號 [ ] 中。
table
在查詢結果中只展示對應字段。
示例
| table 字段1,字段2,字段3
stats
將查詢結果進行聚合統計,類似 SQL 中的 group 。
示例
| stats count by 字段1,字段2
將查詢結果按 字段1 和 字段2 分組,統計記錄數量。
timechart
將查詢結果以時間為 X 軸進行聚合統計 。
語法
timechart [sep=<string>] [format=<string>] [partial=<bool>] [cont=<bool>] [limit=<int>]
[agg=<stats-agg-term>] [<bin-options>...] ( (<single-agg> [BY <split-by-clause>] ) | (<eval-
expression>) BY <split-by-clause> )
示例
| timechart span=1h count by 字段
將查詢結果按 字段 分組,統計每小時記錄數。
head
篩選出前10000條日志作為查詢結果。
示例
| head 10000
eval if in
示例
| eval 新字段=if(字段 in ("甲","乙","丙"),"結果1","結果2")
如果 字段 的值為"甲","乙","丙"中的任意一個,則 新字段 的值為"結果1",否則 新字段 的值為"結果2"。
iplocation
生成IP對應的地區信息,會在結果中加入 City、Country、lat、lon 和 Region 字段,用來表示日志中IP的所在地信息。
語法
iplocation [prefix=<string>] [allfields=<bool>] [lang=<string>] <ip-address-fieldname>
示例
| iplocation lang=zh ip
| table Country,City,ip
tstats
對加速數據模型進行統計查詢。
語法
| tstats [prestats=<bool>] [local=<bool>] [append=<bool>] [summariesonly=<bool>]
[allow_old_summaries=<bool>] [chunk_size=<unsigned int>] <stats-func>...
[FROM ( <namespace> | sid=<tscollect-job-id> | datamodel=<data_model-name> )]
[WHERE <search-query> | <field> IN (<value-list>)]
[BY <field-list> [span=<timespan>] ]
示例
| tstats summariesonly=t count from datamodel=數據模型名 where 1=1
rex
用正則表達式在某個字段中提取新字段。
語法
rex [field=<field>] ( <regex-expression> [max_match=<int>] [offset_field=<string>] ) |
(mode=sed <sed-expression>)
示例
| rex field=_raw "正則1(?\<新字段名\>正則2)正則3"
replace
將字段中的值替換為新值。可以使用通配符 * 進行模糊匹配
語法
replace (<wc-string> WITH <wc-string>)...[IN <field-list>]
示例
| replace "原值" with "新值" in 字段名
earliest latest
將查詢結果控制在某個時間范圍。
一般用於子查詢或動態查詢。
- 現在:now
- 2小時前:-2h@h
- 1天前:-1d@d
示例
index=* earliest=-2h@h latest=now