Kusto Query Language
Kusto Query Language
What is a query statement?
There are three kinds of user query statements:
All query statements are separated by a ;
(semicolon), and only affect the query at hand.
The most common kind of query statement is a tabular expression statement, which means both its input and output consist of tables or tabular datasets. Tabular statements contain zero or more operators, each of which starts with a tabular input and returns a tabular output. Operators are sequenced by a |
(pipe). Data flows, or is piped, from one operator to the next. The data is filtered or manipulated at each step and then fed into the following step.
It's like a funnel, where you start out with an entire data table. Each time the data passes through another operator, it's filtered, rearranged, or summarized. Because the piping of information from one operator to another is sequential, the query operator order is important, and can affect both results and performance. At the end of the funnel, you're left with a refined output.
Lets See the Example
Note
KQL is case-sensitive for everything – table names, table column names, operators, functions, and so on.
Azure Monitor for setting alerts , troubleshooting some example query are given below.
Heartbeat
| where OSType == 'Windows' or OSType == 'Linux'
| summarize arg_max(TimeGenerated, *) by SourceComputerId
| sort by Computer
| project Computer, ComputerEnvironment
| render table
KQL Query to find the Computer OS Type, OS Name OS Major Version, and OS Minor Version.
| where OSType == 'Windows' or OSType == 'Linux'
| summarize arg_max(TimeGenerated, *) by SourceComputerId
| sort by Computer
| project Computer, ComputerEnvironment,
OSType, OSName, OSMajorVersion, OSMinorVersion
| render table
Heartbeat
| where OSType == 'Windows'
| summarize arg_max(TimeGenerated, *) by SourceComputerId
| sort by Computer
| project Computer, ComputerEnvironment,
OSType, OSName, OSMajorVersion, OSMinorVersion
| render table
KQL Query to find the Computer with OS Type has only Linux and display OS Name OS Major Version and OS Minor Version.
| where OSType == 'Linux'
| summarize arg_max(TimeGenerated, *) by SourceComputerId
| sort by Computer
| project Computer, ComputerEnvironment,
OSType, OSName, OSMajorVersion, OSMinorVersion
| render table
Heartbeat
| summarize arg_max(TimeGenerated, *) by SourceComputerId
| sort by Computer
| project Computer, ComputerEnvironment, ComputerIP
| render table
KQL in other services
KQL is used by many other Microsoft services. For specific information on the use of KQL in these environments, refer to the following links:
- Log queries in Azure Monitor
- Kusto Query Language in Microsoft Sentinel
- Understanding the Azure Resource Graph query language
- Proactively hunt for threats with advanced hunting in Microsoft 365 Defender
- CMPivot queries
Basic Query:
kqlMyDataTable | project Column1, Column2 | where Column1 > 10 | order by Column2 desc
Filtering Data:
kqlMyDataTable | where Column1 > 10 and Column2 == "Value"
Aggregating Data:
kqlMyDataTable | summarize TotalCount=count(), AvgValue=avg(Column1)
Grouping Data:
kqlMyDataTable | summarize TotalCount=count() by Column2
Joining Tables:
kqlJoin Table1 on ColumnA, Table2 on ColumnB | project Table1.ColumnX, Table2.ColumnY
Time Series Analysis:
MyDataTable | summarize TotalCount=count() by bin(Timestamp, 1h) | order by Timestamp asc
Custom Functions:
kqllet CustomFunction = (arg1: int, arg2: string) { // Function logic here }; MyDataTable | extend NewColumn = CustomFunction(Column1, Column2)
Working with JSON Data:
kqlMyDataTable | extend ParsedJson = parse_json(JsonColumn) | project ParsedJson.PropertyA
Time Window Functions:
kqlMyDataTable | extend Window = range start=start_date to end=end_date step 1d | summarize CountInWindow=count() by Window
Count Distinct Values:
kqlMyDataTable | summarize DistinctCount=dcount(Column1)
Top N Values:
kqlMyDataTable | top 10 by Column1 desc
Extract Regex Matches:
MyDataTable | extend ExtractedValue = extract(@"\d{3}", 0, Column1)
Outliers Detection:
kqlMyDataTable | summarize Outliers = series_outliers(Column1)
Pivoting Data:
kqlMyDataTable | pivot Column2, sum(Column1) for Column3 in ("Value1", "Value2")
Calculate Percentiles:
kqlMyDataTable | summarize Percentile50=percentile(Column1, 50), Percentile90=percentile(Column1, 90)
Chart Visualization:
kqlMyDataTable | summarize Count=count() by Category | project Category, Count | render piechart
Comments
Post a Comment