如何通过Logstash JDBC插件将Mariadb经纬度转为geo_point用于Kibana
当然可以修改索引映射来实现需求!
Elasticsearch 不允许直接修改已存在字段的类型,但我们可以通过**重新索引(reindex)**的方式,将数据迁移到一个带有正确geo_point映射的新索引中,具体步骤如下:
1. 创建带有geo_point映射的新索引
首先,定义一个新索引的映射,把你的经纬度字段转换成geo_point类型。假设你原索引名为maria_db_data,新索引名为maria_db_data_geo,且原数据里有latitude(纬度)和longitude(经度)两个字段:
PUT /maria_db_data_geo { "mappings": { "properties": { // 定义geo_point类型的字段,比如叫location "location": { "type": "geo_point" }, // 保留原索引的其他字段映射,比如文本、数值类型等 "user_name": { "type": "text" }, "record_time": { "type": "date" } // 其他字段按需添加 } } }
2. 使用Reindex API迁移数据
接下来,把原索引的数据同步到新索引,同时在同步过程中把latitude和longitude组合成location字段:
POST _reindex { "source": { "index": "maria_db_data" }, "dest": { "index": "maria_db_data_geo" }, "script": { "source": "ctx._source.location = [ctx._source.longitude, ctx._source.latitude]; " // 如果不需要保留原lat/lon字段,可以加上下面两行删除它们 // "ctx._source.remove('latitude'); " // "ctx._source.remove('longitude'); " } }
3. 验证新索引的映射和数据
执行完reindex后,检查新索引的映射是否正确:
GET /maria_db_data_geo/_mapping
也可以搜索一条数据,确认location字段已经是geo_point类型:
GET /maria_db_data_geo/_search { "query": { "match_all": {} }, "size": 1 }
4. 切换到新索引使用
- 在Kibana中创建新的索引模式,指向
maria_db_data_geo - 之后你就可以在Kibana的可视化功能里,选择
location字段来制作地理报告(比如地图热力图、坐标点分布等)
如果想要保留原索引的名称,可以先删除旧索引,再给新索引设置别名:
// 删除旧索引 DELETE /maria_db_data // 给新索引设置别名 POST /_aliases { "actions": [ { "add": { "index": "maria_db_data_geo", "alias": "maria_db_data" } } ] }
这样后续的Logstash同步和Kibana操作都可以继续使用原索引名。
内容的提问来源于stack exchange,提问作者Assem
相关产品推荐
相关产品推荐

