KairosDB 1.1.3-1能否实现先分组聚合再二次分组聚合操作?
Absolutely, you can pull off this two-step grouping and aggregation exactly as you described! KairosDB supports chained aggregations and multiple grouping stages, which fits perfectly with your use case. Let’s walk through how to structure this properly.
Breakdown of Your Workflow
First, let’s recap what you need to do to make sure we’re aligned:
- Stage 1: Group data points by
name+key, calculate the average value for each 10-second time window (e.g., aggregate allname1+key1points into 10s averages, same forname2+key1). - Stage 2: Merge those aggregated results by
key, then sum the values within the same 10-second windows (so sum the 10s averages fromname1+key1andname2+key1into a single value per 10s window forkey1).
Query Implementation
Below is a complete KairosDB JSON query that implements this workflow. I’ll note key details based on whether name is a metric name or a tag field (since your data structure mentions name as part of the data point, it could be either):
Case 1: name is a Metric Name
If name refers to the KairosDB metric name (e.g., name1 and name2 are separate metrics sharing the key tag), use this query:
{ "metrics": [ { "name": ["name1", "name2"], // Target your specific metrics "tags": { "key": ["key1"] // Focus on key1 }, "aggregators": [ // Stage 1: Group by metric name + key + 10s window, compute average { "name": "group_by", "group_by": [ {"name": "metric"}, // Group by metric name (your "name" field) {"name": "tag", "tags": ["key"]}, {"name": "time", "range": 10000} // 10 seconds in milliseconds ], "aggregator": {"name": "avg"} }, // Stage 2: Re-group by key + same 10s window, compute sum { "name": "group_by", "group_by": [ {"name": "tag", "tags": ["key"]}, {"name": "time", "range": 10000} ], "aggregator": {"name": "sum"} } ] } ], "start_absolute": 1600000000000, // Replace with your start timestamp (ms) "end_absolute": 1600003600000 // Replace with your end timestamp (ms) }
Case 2: name is a Tag Field
If name is a custom tag (all data points belong to the same metric, with name and key as tags), adjust the first group-by stage to target the name tag:
{ "metrics": [ { "name": "your_metric_name", // Replace with your actual metric name "tags": { "name": ["name1", "name2"], "key": ["key1"] }, "aggregators": [ // Stage 1: Group by name tag + key tag + 10s window, compute average { "name": "group_by", "group_by": [ {"name": "tag", "tags": ["name", "key"]}, {"name": "time", "range": 10000} ], "aggregator": {"name": "avg"} }, // Stage 2: Re-group by key tag + same 10s window, compute sum { "name": "group_by", "group_by": [ {"name": "tag", "tags": ["key"]}, {"name": "time", "range": 10000} ], "aggregator": {"name": "sum"} } ] } ], "start_absolute": 1600000000000, "end_absolute": 1600003600000 }
Key Notes
- Time Window Unit: KairosDB uses milliseconds for time ranges, so 10 seconds =
10000. - Flexibility: You can adjust the
name/keyfilters to target broader sets (e.g., use"name": "*"to include all metrics/tags matching the key). - Order Matters: The aggregators run in sequence—first the average calculation per
name+keywindow, then the sum perkeywindow.
This query will exactly produce the result you’re looking for: first aggregated averages per name+key 10s window, then summed values per key 10s window.
内容的提问来源于stack exchange,提问作者ByeBye

