如何在Grafana中对InfluxDB V2的Geohash值进行分组统计
问题:基于Geohash分组统计并在Grafana地图展示
我正在开展一个项目,需基于Geohash值分组在地图上展示位置,但难以编写查询语句实现Geohash值的展示、分组及各组内出现次数统计。
示例InfluxDB数据
geoip2influx,type=geomaphash City="Jakarta",Count=2,Country_code="ID",Country_name="Indonesia",Geohash="qqt4fxmz",Host="Server-1",Ip="203.12.34.56",Latitude="-6.2088",Longitude="106.8456" geoip2influx,type=geomaphash City="Jakarta",Count=2,Country_code="ID",Country_name="Indonesia",Geohash="qqt4fxmz",Host="Server-1",Ip="203.12.34.56",Latitude="-6.2088",Longitude="106.8456" geoip2influx,type=geomaphash City="Jakarta",Count=2,Country_code="ID",Country_name="Indonesia",Geohash="qqt4fxmz",Host="Server-1",Ip="203.12.34.56",Latitude="-6.2088",Longitude="106.8456" geoip2influx,type=geomaphash City="Jakarta",Count=2,Country_code="ID",Country_name="Indonesia",Geohash="qqt4fxmz",Host="Server-1",Ip="203.12.34.56",Latitude="-6.2088",Longitude="106.8456"
当前使用的Flux查询语句
from(bucket: "geoip2influx") |> range(start: v.timeRangeStart, stop: v.timeRangeStop) |> filter(fn: (r) => r["_measurement"] == "geoip2influx") |> filter(fn: (r) => r["_field"] == "Geohash" ) |> pivot(rowKey:["_time"], columnKey: ["_field"], valueColumn: "_value") |> drop(columns: ["_start", "_stop", "_measurement", "type"]) |> group(columns: ["_value"])
期望查询结果
| Geohash | Count |
|---|---|
| qqt4fxmz | 3 |
该结果需支持将Geohash字段作为地图的Geohash Field,统计结果作为Size Field使用。
环境信息
- Grafana版本:10.1.4
- 数据源类型及版本:InfluxDB 2.0.4
- Grafana部署系统:Docker
- 用户系统及浏览器:Ubuntu 20.04 & Chrome
- Grafana插件:Geomap
解决方案:修改Flux查询实现分组统计
当前查询仅完成分组操作,未实现计数统计,需调整逻辑以提取Geohash值并按其分组计数。以下是修改后的Flux语句:
from(bucket: "geoip2influx") |> range(start: v.timeRangeStart, stop: v.timeRangeStop) // 过滤目标测量值和类型标签 |> filter(fn: (r) => r["_measurement"] == "geoip2influx" and r["type"] == "geomaphash") // 仅保留Geohash字段数据 |> filter(fn: (r) => r["_field"] == "Geohash") // 重命名字段避免后续处理混乱 |> rename(columns: {_value: "Geohash"}) // 按Geohash字段分组 |> group(columns: ["Geohash"]) // 统计每组记录数并生成Count字段 |> count(column: "Count") // 移除冗余系统字段,仅保留核心数据 |> drop(columns: ["_start", "_stop", "_measurement", "type", "_field"])
关键调整说明
- 精准过滤数据:加入
r["type"] == "geomaphash"条件,确保只处理目标类型的数据。 - 字段重命名:将
_value改为Geohash,让字段名更直观,适配后续分组和展示需求。 - 分组计数:使用
count(column: "Count")直接统计每个Geohash分组的记录数量,生成所需的Count字段。 - 清理冗余字段:最后移除不需要的系统字段,仅保留Geohash和Count两个核心字段,完美适配Grafana Geomap插件的配置要求。
替换查询后,在Geomap插件配置中:
- 选择Geohash Field为
Geohash - 选择Size Field为
Count
即可实现按Geohash分组的地图展示。
内容的提问来源于stack exchange,提问作者iamyusuf
相关产品推荐
相关产品推荐

