1. 首页 > Redis教程 > 正文

Redis教程FG018-Redis GEO地理空间数据实战

本文档风哥主要介绍Redis GEO类型的地理空间数据实战,包括GEO类型概念、GEO操作命令、GEO使用场景、GEO规划、坐标系、性能考虑、基础操作、高级操作、半径操作、过期操作以及实战案例等内容,风哥教程参考Redis官方文档Data types guide等内容编写,适合DBA人员和开发人员在生产环境中使用。

Part01-基础概念与理论知识

1.1 GEO类型概念

Redis GEO是一种用于存储和查询地理空间数据的数据结构,它基于Sorted Set实现,使用GeoHash算法对地理坐标进行编码。GEO类型的特点:

  • 高效存储:使用GeoHash算法对地理坐标进行编码,节省存储空间
  • 快速查询:支持基于半径的附近搜索
  • 距离计算:支持计算两个位置之间的距离
  • 排序功能:可以按距离排序返回结果

1.2 GEO操作命令

# GEO操作命令

## 1. 基础命令
– GEOADD:添加地理位置
– GEOPOS:获取地理位置的坐标
– GEODIST:计算两个地理位置之间的距离
– GEORADIUS:根据给定的经纬度和半径,查找附近的地理位置
– GEORADIUSBYMEMBER:根据给定的成员和半径,查找附近的地理位置
– GEOHASH:获取地理位置的GeoHash编码

## 2. 过期命令
– EXPIRE:设置GEO集合的过期时间
– TTL:查看GEO集合的剩余过期时间
– PERSIST:移除GEO集合的过期时间

1.3 GEO使用场景

GEO类型的使用场景:

  • 附近搜索:查找附近的商店、餐厅、用户等
  • 距离计算:计算两个位置之间的距离
  • 地理围栏:设置地理围栏,当用户进入或离开特定区域时触发事件
  • 位置追踪:追踪车辆、人员的位置
  • 地图应用:在地图上显示附近的兴趣点
  • 导航系统:提供基于位置的导航服务

更多视频教程www.fgedu.net.cn

Part02-生产环境规划与建议

2.1 GEO规划

生产环境GEO规划:

  • GEO集合命名规范:使用业务前缀+集合类型的方式,如shop:locations
  • 坐标精度:根据业务需求确定坐标的精度
  • 数据量:合理控制GEO集合的大小,避免过大
  • 过期时间:根据数据生命周期设置合理的过期时间

2.2 坐标系

坐标系说明:

  • 经纬度格式:经度(longitude)在前,纬度(latitude)在后
  • 取值范围:经度范围为-180到180,纬度范围为-85.05112878到85.05112878
  • 精度:Redis GEO使用GeoHash算法,精度取决于编码长度
  • 单位:距离单位支持m(米)、km(公里)、mi(英里)、ft(英尺)

2.3 性能考虑

# 性能考虑

## 1. 操作性能
– GEOADD:O(logN)
– GEOPOS:O(logN)
– GEODIST:O(logN)
– GEORADIUS:O(N + logM),N为区域内的元素数量,M为返回的元素数量
– GEORADIUSBYMEMBER:O(N + logM)

## 2. 性能优化
– 合理控制GEO集合的大小
– 优化GEORADIUS操作,使用合适的半径
– 限制返回的元素数量
– 使用管道批量执行操作

## 3. 最佳实践
– 对于大GEO集合,使用多个GEO集合分片存储
– 定期清理过期的GEO数据
– 使用合适的半径进行附近搜索

学习交流加群风哥QQ113257174

Part03-生产环境项目实施方案

3.1 基础操作

# 基础操作

## 1. 添加地理位置
$ /redis/app/bin/redis-cli -h 192.168.1.100 -p 6379 -a fgedu@2026 geoadd shop:locations 116.404 39.915 “shop1” 116.414 39.915 “shop2” 116.404 39.925 “shop3” 116.414 39.925 “shop4”

# 输出示例
(integer) 4

## 2. 获取地理位置的坐标
$ /redis/app/bin/redis-cli -h 192.168.1.100 -p 6379 -a fgedu@2026 geopos shop:locations shop1 shop2 shop3

# 输出示例
1) 1) “116.404000014066696”
2) “39.91500017166436”
2) 1) “116.414000034332275”
2) “39.91500017166436”
3) 1) “116.404000014066696”
2) “39.92500009157732”

## 3. 计算两个地理位置之间的距离
$ /redis/app/bin/redis-cli -h 192.168.1.100 -p 6379 -a fgedu@2026 geodist shop:locations shop1 shop2 km

# 输出示例
“1.1180”

## 4. 获取地理位置的GeoHash编码
$ /redis/app/bin/redis-cli -h 192.168.1.100 -p 6379 -a fgedu@2026 geohash shop:locations shop1 shop2

# 输出示例
1) “wx4g52e1ce0”
2) “wx4g5bu1pg0”

3.2 高级操作

# 高级操作

## 1. 根据经纬度和半径查找附近的地理位置
$ /redis/app/bin/redis-cli -h 192.168.1.100 -p 6379 -a fgedu@2026 georadius shop:locations 116.409 39.920 2 km withdist withcoord

# 输出示例
1) 1) “shop1”
2) “0.5590”
3) 1) “116.404000014066696”
2) “39.91500017166436”
2) 1) “shop3”
2) “0.5590”
3) 1) “116.404000014066696”
2) “39.92500009157732”
3) 1) “shop2”
2) “0.5590”
3) 1) “116.414000034332275”
2) “39.91500017166436”
4) 1) “shop4”
2) “0.5590”
3) 1) “116.414000034332275”
2) “39.92500009157732”

## 2. 根据成员和半径查找附近的地理位置
$ /redis/app/bin/redis-cli -h 192.168.1.100 -p 6379 -a fgedu@2026 georadiusbymember shop:locations shop1 2 km withdist

# 输出示例
1) 1) “shop1”
2) “0.0000”
2) 1) “shop3”
2) “1.1180”
3) 1) “shop2”
2) “1.1180”
4) 1) “shop4”
2) “1.5811”

## 3. 限制返回的元素数量
$ /redis/app/bin/redis-cli -h 192.168.1.100 -p 6379 -a fgedu@2026 georadius shop:locations 116.409 39.920 2 km withdist count 2

# 输出示例
1) 1) “shop1”
2) “0.5590”
2) 1) “shop3”
2) “0.5590”

## 4. 按距离排序
$ /redis/app/bin/redis-cli -h 192.168.1.100 -p 6379 -a fgedu@2026 georadius shop:locations 116.409 39.920 2 km withdist desc

# 输出示例
1) 1) “shop4”
2) “0.5590”
2) 1) “shop2”
2) “0.5590”
3) 1) “shop3”
2) “0.5590”
4) 1) “shop1”
2) “0.5590”

3.3 半径操作

# 半径操作

## 1. 查找附近的商店(500米内)
$ /redis/app/bin/redis-cli -h 192.168.1.100 -p 6379 -a fgedu@2026 georadius shop:locations 116.409 39.920 0.5 km withdist

# 输出示例
(empty list or set)

## 2. 查找附近的商店(1公里内)
$ /redis/app/bin/redis-cli -h 192.168.1.100 -p 6379 -a fgedu@2026 georadius shop:locations 116.409 39.920 1 km withdist

# 输出示例
(empty list or set)

## 3. 查找附近的商店(2公里内)
$ /redis/app/bin/redis-cli -h 192.168.1.100 -p 6379 -a fgedu@2026 georadius shop:locations 116.409 39.920 2 km withdist

# 输出示例
1) 1) “shop1”
2) “0.5590”
2) 1) “shop3”
2) “0.5590”
3) 1) “shop2”
2) “0.5590”
4) 1) “shop4”
2) “0.5590”

## 4. 查找附近的商店(3公里内)
$ /redis/app/bin/redis-cli -h 192.168.1.100 -p 6379 -a fgedu@2026 georadius shop:locations 116.409 39.920 3 km withdist

# 输出示例
1) 1) “shop1”
2) “0.5590”
2) 1) “shop3”
2) “0.5590”
3) 1) “shop2”
2) “0.5590”
4) 1) “shop4”
2) “0.5590”

3.4 过期操作

# 过期操作

## 1. 设置GEO集合的过期时间(秒)
$ /redis/app/bin/redis-cli -h 192.168.1.100 -p 6379 -a fgedu@2026 geoadd temp:locations 116.404 39.915 “temp1” 116.414 39.915 “temp2”
$ /redis/app/bin/redis-cli -h 192.168.1.100 -p 6379 -a fgedu@2026 expire temp:locations 300

# 输出示例
(integer) 2
(integer) 1

## 2. 查看GEO集合的剩余过期时间(秒)
$ /redis/app/bin/redis-cli -h 192.168.1.100 -p 6379 -a fgedu@2026 ttl temp:locations

# 输出示例
(integer) 299

## 3. 移除GEO集合的过期时间
$ /redis/app/bin/redis-cli -h 192.168.1.100 -p 6379 -a fgedu@2026 persist temp:locations

# 输出示例
(integer) 1

## 4. 为GEO集合设置不同的过期时间
$ /redis/app/bin/redis-cli -h 192.168.1.100 -p 6379 -a fgedu@2026 expire shop:locations 604800

# 输出示例
(integer) 1

风哥提示:Redis接口限流是保护系统的重要机制,合理的限流策略可以防止系统过载,确保系统的稳定性和可用性。在实际应用中,需要根据具体业务场景和数据特点,选择合适的限流算法和策略。

Part04-生产案例与实战讲解

# 附近搜索

## 1. 添加多个商店位置
$ /redis/app/bin/redis-cli -h 192.168.1.100 -p 6379 -a fgedu@2026 geoadd shop:locations 116.404 39.915 “shop1” 116.414 39.915 “shop2” 116.404 39.925 “shop3” 116.414 39.925 “shop4” 116.409 39.920 “shop5”

# 输出示例
(integer) 5

## 2. 查找当前位置附近的商店(1公里内)
$ /redis/app/bin/redis-cli -h 192.168.1.100 -p 6379 -a fgedu@2026 georadius shop:locations 116.409 39.920 1 km withdist withcoord

# 输出示例
1) 1) “shop5”
2) “0.0000”
3) 1) “116.40900003910065”
2) “39.9200000769989”

## 3. 查找当前位置附近的商店(2公里内)
$ /redis/app/bin/redis-cli -h 192.168.1.100 -p 6379 -a fgedu@2026 georadius shop:locations 116.409 39.920 2 km withdist

# 输出示例
1) 1) “shop5”
2) “0.0000”
2) 1) “shop1”
2) “0.5590”
3) 1) “shop3”
2) “0.5590”
4) 1) “shop2”
2) “0.5590”
5) 1) “shop4”
2) “0.5590”

## 4. 按距离排序并限制返回数量
$ /redis/app/bin/redis-cli -h 192.168.1.100 -p 6379 -a fgedu@2026 georadius shop:locations 116.409 39.920 2 km withdist asc count 3

# 输出示例
1) 1) “shop5”
2) “0.0000”
2) 1) “shop1”
2) “0.5590”
3) 1) “shop3”
2) “0.5590”

4.2 距离计算

# 距离计算

## 1. 计算两个商店之间的距离
$ /redis/app/bin/redis-cli -h 192.168.1.100 -p 6379 -a fgedu@2026 geodist shop:locations shop1 shop2 km
$ /redis/app/bin/redis-cli -h 192.168.1.100 -p 6379 -a fgedu@2026 geodist shop:locations shop1 shop3 km
$ /redis/app/bin/redis-cli -h 192.168.1.100 -p 6379 -a fgedu@2026 geodist shop:locations shop1 shop4 km

# 输出示例
“1.1180”
“1.1180”
“1.5811”

## 2. 计算多个商店之间的距离
$ /redis/app/bin/redis-cli -h 192.168.1.100 -p 6379 -a fgedu@2026 geodist shop:locations shop2 shop3 km
$ /redis/app/bin/redis-cli -h 192.168.1.100 -p 6379 -a fgedu@2026 geodist shop:locations shop2 shop4 km
$ /redis/app/bin/redis-cli -h 192.168.1.100 -p 6379 -a fgedu@2026 geodist shop:locations shop3 shop4 km

# 输出示例
“1.5811”
“1.1180”
“1.1180”

## 3. 使用不同的距离单位
$ /redis/app/bin/redis-cli -h 192.168.1.100 -p 6379 -a fgedu@2026 geodist shop:locations shop1 shop2 m
$ /redis/app/bin/redis-cli -h 192.168.1.100 -p 6379 -a fgedu@2026 geodist shop:locations shop1 shop2 mi
$ /redis/app/bin/redis-cli -h 192.168.1.100 -p 6379 -a fgedu@2026 geodist shop:locations shop1 shop2 ft

# 输出示例
“1118.0343”
“0.6947”
“3668.0915”

4.3 地理围栏

# 地理围栏

## 1. 设置地理围栏(以shop5为中心,1公里半径)
$ /redis/app/bin/redis-cli -h 192.168.1.100 -p 6379 -a fgedu@2026 georadiusbymember shop:locations shop5 1 km withdist

# 输出示例
1) 1) “shop5”
2) “0.0000”

## 2. 检查用户是否在围栏内
$ /redis/app/bin/redis-cli -h 192.168.1.100 -p 6379 -a fgedu@2026 geoadd user:locations 116.409 39.920 “user1” 116.419 39.920 “user2”

# 输出示例
(integer) 2

## 3. 检查user1是否在shop5的围栏内
$ /redis/app/bin/redis-cli -h 192.168.1.100 -p 6379 -a fgedu@2026 geodist shop:locations shop5 user:locations user1 km

# 输出示例
“0.0000”

## 4. 检查user2是否在shop5的围栏内
$ /redis/app/bin/redis-cli -h 192.168.1.100 -p 6379 -a fgedu@2026 geodist shop:locations shop5 user:locations user2 km

# 输出示例
“1.1180”

## 5. 批量检查用户是否在围栏内
$ /redis/app/bin/redis-cli -h 192.168.1.100 -p 6379 -a fgedu@2026 georadius shop:locations 116.409 39.920 1 km withdist

# 输出示例
1) 1) “shop5”
2) “0.0000”

4.4 位置追踪

# 位置追踪

## 1. 记录车辆位置
$ /redis/app/bin/redis-cli -h 192.168.1.100 -p 6379 -a fgedu@2026 geoadd vehicle:locations 116.404 39.915 “vehicle1” 116.414 39.915 “vehicle2” 116.404 39.925 “vehicle3”

# 输出示例
(integer) 3

## 2. 更新车辆位置
$ /redis/app/bin/redis-cli -h 192.168.1.100 -p 6379 -a fgedu@2026 geoadd vehicle:locations 116.405 39.916 “vehicle1”

# 输出示例
(integer) 1

## 3. 获取车辆当前位置
$ /redis/app/bin/redis-cli -h 192.168.1.100 -p 6379 -a fgedu@2026 geopos vehicle:locations vehicle1

# 输出示例
1) 1) “116.40500006079674”
2) “39.91600012756839”

## 4. 查找附近的车辆
$ /redis/app/bin/redis-cli -h 192.168.1.100 -p 6379 -a fgedu@2026 georadius vehicle:locations 116.409 39.920 2 km withdist

# 输出示例
1) 1) “vehicle1”
2) “0.5590”
2) 1) “vehicle3”
2) “0.5590”
3) 1) “vehicle2”
2) “0.5590”

## 5. 计算车辆之间的距离
$ /redis/app/bin/redis-cli -h 192.168.1.100 -p 6379 -a fgedu@2026 geodist vehicle:locations vehicle1 vehicle2 km

# 输出示例
“1.0058”

更多学习教程公众号风哥教程itpux_com

Part05-风哥经验总结与分享

5.1 最佳实践

Redis GEO地理空间数据实战最佳实践:

  • GEO集合命名规范:使用业务前缀+集合类型的方式,如shop:locations,学习交流加群风哥微信: itpux-com
  • 坐标精度:根据业务需求确定坐标的精度
  • 合理设置过期时间:根据数据生命周期设置合适的过期时间
  • 优化查询:使用合适的半径和限制返回数量
  • 监控GEO集合大小:避免GEO集合过大导致性能问题
  • 使用多个GEO集合:对于大数据集,使用多个GEO集合分片存储

5.2 常见问题

常见问题及解决:

  • 坐标精度:注意Redis GEO的坐标精度限制,纬度范围为-85.05112878到85.05112878
  • 性能问题:对于大GEO集合,GEORADIUS操作可能会较慢,需要优化
  • 内存使用:GEO集合基于Sorted Set实现,内存使用与元素数量相关
  • 过期时间:合理设置过期时间,避免数据过期导致业务问题
  • 半径选择:选择合适的半径,避免返回过多结果

5.3 优化技巧

风哥提示:Redis GEO是一种强大的地理空间数据结构,适合实现附近搜索、距离计算、地理围栏等功能。在实际应用中,需要根据业务需求合理设计GEO集合和操作策略,确保系统的可靠性和性能。

# 优化技巧

## 1. GEO集合设计优化
– 使用简短的GEO集合名称
– 合理设计数据分片,避免单个GEO集合过大
– 为不同的业务场景设计不同的GEO集合
– 考虑数据的生命周期,设置合理的过期时间

## 2. 操作优化
– 使用GEOADD批量添加地理位置
– 优化GEORADIUS操作,使用合适的半径
– 限制返回的元素数量,使用count参数
– 使用管道批量执行操作

## 3. 性能优化
– 监控GEO集合的大小,及时处理过大的集合
– 优化GEORADIUS操作的执行时间
– 避免在高峰期执行大量的GEO操作
– 使用多个GEO集合分片存储大规模数据

## 4. 内存优化
– 合理控制GEO集合的大小
– 设置合理的过期时间,自动清理过期数据
– 选择合适的内存淘汰策略

## 5. 监控优化
– 监控GEO集合的大小和内存使用
– 监控GEORADIUS操作的执行时间
– 设置合理的告警机制
– 定期分析GEO集合的使用情况

通过本文档的学习,您应该掌握了Redis GEO类型的地理空间数据实战,能够在生产环境中合理使用GEO类型实现附近搜索、距离计算、地理围栏、位置追踪等功能。在实际应用中,需要根据具体业务场景选择合适的操作命令和GEO集合设计,确保系统的可靠性和性能。

风哥提示:Redis GEO是一种强大的地理空间数据结构,适合实现附近搜索、距离计算、地理围栏等功能。在实际应用中,需要根据业务需求合理设计GEO集合和操作策略,确保系统的可靠性和性能。

from Redis视频:www.itpux.com

本文由风哥教程整理发布,仅用于学习测试使用,转载注明出处:http://www.fgedu.net.cn/10327.html

联系我们

在线咨询:点击这里给我发消息

微信号:itpux-com

工作日:9:30-18:30,节假日休息