splunk是当下比较火的大数据分析工具,可以收集日志数据、性能数据、网络数据包。这些数据都是一些非结构化的数据,我们可以统一将这些数据统一采集到splunk之后,splunk可以对这些数据进行索引、调查、监控、可视化,告警等等。
还可以做一下机器学习训练等操作,非常方便。
之前的都是比较简单的查询,例如查询IPS的流量,统计某个用户访问网站频率,攻击频率等等。
splunk有多个数据集,mysql这种就很好做关联查询了,left jion 等等,查了资料整理如下,给需要的人参考。
SQL command | SQL example | Splunk SPL example | 备注 |
---|---|---|
SELECT * |
SELECT * FROM mytable source=mytable |
source就相当于table |
WHERE |
SELECT * FROM mytable WHERE mycolumn=5
source=mytable mycolumn=5 |
|
SELECT |
SELECT mycolumn1, mycolumn2 FROM mytable source=mytable | FIELDS mycolumn1, mycolumn2 |
通过fields可以规定查询哪几列 |
AND/OR |
SELECT * FROM mytable WHERE (mycolumn1="true" OR mycolumn2="red") AND mycolumn3="blue" source=mytable AND (mycolumn1="true" OR mycolumn2="red") AND mycolumn3="blue" |
|
AS (alias) |
SELECT mycolumn AS column_alias FROM mytable source=mytable | RENAME mycolumn as column_alias | FIELDS column_alias |
rename关键字 |
BETWEEN |
SELECT * FROM mytable WHERE mycolumn BETWEEN 1 AND 5 source=mytable mycolumn>=1 mycolumn<=5 |
|
GROUP BY |
SELECT mycolumn, avg(mycolumn) FROM mytable WHERE mycolumn=value GROUP BY mycolumn source=mytable mycolumn=value | STATS avg(mycolumn) BY mycolumn | FIELDS mycolumn, avg(mycolumn ) |
stats对结果分组,并取平均值 |
LEFT (OUTER) JOIN |
SELECT * FROM mytable1 LEFT JOIN mytable2 ON mytable1.mycolumn= mytable2.mycolumn source=mytable1 | JOIN type=left mycolumn [SEARCH source=mytable2] |
[SEARCH..]相当于一个子查询了,然后进行连接 |
TRUNCATE TABLE |
TRUNCATE TABLE mytable source=mytable | DELETE |
|
UNION |
SELECT mycolumn FROM mytable1 UNION SELECT mycolumn FROM mytable2 source=mytable1 | APPEND [SEARCH source=mytable2] | DEDUP mycolumn |
APPEND相当于将当前查询与子查询组合起来 |
UNION ALL |
SELECT * FROM mytable1 UNION ALL SELECT * FROM mytable2 source=mytable1 | APPEND [SEARCH source=mytable2] |
区别在于,不需要去重字段 |
在表关联查询中,示例使用的是字段相同的,但是我的比较特殊,没有字段关联名称关联,但是我确认字段的值是关联的。
index="数据集1" category="类别" | stats count as "访问次数" BY srcuser|join type=left srcuser [SEARCH index="数据集2" |rename ldap_id as srcuser]|table srcuser "访问次数" mail_id,user_cname,user_email,user_deptpath,user_dept1,leader_ldap,leader_email,leader_cname