top
Displays the top N grouped results. You can get the same result as stats count by FIELD, ... | sort limit=N -count with shorter syntax.
Command properties
| Property | Description |
|---|---|
| Command type | Processing query |
| Required permission | None |
| License usage | N/A |
| Parallel execution | Supported |
| Distributed execution | Runs on Data Node (mapper) |
Syntax
Options
n=INT- The number of top results to display (default:
10). Must be an integer of 1 or greater.
Target
FIELD, ...- The list of fields to group by. Specify multiple fields separated by commas (
,). You must specify at least one field.
Input fields
There are no separately defined input fields. The grouping fields specified in FIELD, ... in the target clause must exist in the input records to be reflected in the grouped results.
Output fields
| Field | Type | Description |
|---|---|---|
| (FIELD) | (original type) | The grouping fields specified in FIELD, ... |
| count | long | Number of records per group |
Error codes
Parsing errors
| Error code | Message | Description |
|---|---|---|
| invalid-limit | invalid-limit | The n option value is 0 or less, or cannot be converted to an integer |
| missing-field | missing-field | No field to group by was specified |
Runtime errors
N/A
Description
The top command never runs. The query planner (TopCommandRewritingPlanner) always rewrites it into the command pair stats count by FIELD, ... | sort limit=N -count before execution, and that command pair actually runs. In other words, it aggregates counts by the specified field combination, sorts them in descending order (largest count first), and returns only the top n results. If the rewrite fails (for example, due to an internal parsing error), the original top command passes through unchanged, forwarding the input without modification.
Examples
To prepare the WEB_APACHE_SAMPLE table used in these examples, refer to Preparing sample data.
-
Find the URIs with the most requests
table WEB_APACHE_SAMPLE | top uriGroups by
uriand returns the top 10 URIs with the most requests. -
Query with a specified count
table WEB_APACHE_SAMPLE | top n=5 status, methodGroups by the combination of
statusandmethodand returns the top 5 combinations with the most occurrences. -
The same query written directly with
statsandsorttable WEB_APACHE_SAMPLE | stats count by uri | sort limit=10 -countReturns the same result as example 1.
Change history
The top command is available in the Sonar 4.0 series starting with 4.0.2609.0, where the command previously provided by the Experimental app was built into Sonar core. On versions without the built-in command, install the Experimental app to use it.