如何在Druid中实现分时间段多粒度数据Rollup
实现Druid多粒度时间聚合的方案
你的需求完全可以通过mixed类型的granularitySpec实现,之前配置失败大概率是区间定义错误、粒度层级不匹配或参数设置不当。以下是具体实现步骤和示例:
核心配置思路
- 以最小查询粒度(hour)作为
segmentsGranularity,确保底层数据能向上聚合到天、月、年粒度,避免数据丢失。 - 为每个时间区间单独配置对应的聚合粒度,确保区间无重叠、覆盖完整。
- 全局
queryGranularity设为null,让各区间的粒度配置生效。
时间区间划分(相对时间版)
根据你的需求,用Druid支持的相对时间表达式定义区间(自动适配当前时间):
- 90天以内:
PT90D/now(从当前时间往前推90天到现在) - 过去3-6个月:
P6M/P3M(从当前时间往前推6个月到往前推3个月) - 过去6-12个月:
P12M/P6M(从当前时间往前推12个月到往前推6个月) - 过去1-3年:
P3Y/P1Y(从当前时间往前推3年到往前推1年)
完整配置示例
以下是批量导入任务的完整spec,你可以根据自己的数据源、维度指标调整:
{ "type": "index_parallel", "spec": { "dataSchema": { "dataSource": "your_datasource", "parser": { "type": "string", "parseSpec": { "format": "json", "timestampSpec": { "column": "timestamp", "format": "iso" }, "dimensionsSpec": { "dimensions": ["dim1", "dim2"] } } }, "metricsSpec": [ { "type": "count", "name": "count" }, { "type": "sum", "name": "sum_value", "fieldName": "value" } ], "granularitySpec": { "type": "mixed", "segmentsGranularity": "hour", "queryGranularity": null, "intervals": [ { "interval": "PT90D/now", "granularity": "hour" }, { "interval": "P6M/P3M", "granularity": "day" }, { "interval": "P12M/P6M", "granularity": "month" }, { "interval": "P3Y/P1Y", "granularity": "year" } ] } }, "ioConfig": { "type": "index_parallel", "inputSource": { "type": "local", "baseDir": "/path/to/your/data", "filter": "*.json" }, "inputFormat": { "type": "json" } }, "tuningConfig": { "type": "index_parallel", "partitionsSpec": { "type": "dynamic" } } } }
常见问题排查
如果之前配置失败,可能是以下原因:
- 区间重叠:比如90天和3个月的区间边界有重叠(3个月可能是90-92天),需调整表达式确保区间连续无重叠,比如用
P3M替代PT90D统一用月份维度划分。 - segmentsGranularity设置过大:如果设为
day,小时粒度的区间无法生成正确数据,必须用最小查询粒度作为分段粒度。 - 时间格式错误:相对时间表达式需遵循ISO 8601标准,比如天用
PxD,小时用PTxH,避免拼写错误。 - queryGranularity未设为null:全局粒度会覆盖区间粒度,导致配置不生效。
内容的提问来源于stack exchange,提问作者Pradeep
相关产品推荐
相关产品推荐

