Redis的GEO结构

GEO就是Geolocation的简写形式,代表地理坐标。Redis在3.2版本中加入了对GEO的支持,允许存储地理坐标信息,帮助我们根据经纬度来检索数据。常见的命令有:

  • GEOADD:添加一个地理空间信息,包含:经度(longitude)、纬度(latitude)、值(member)
  • GEODIST:计算指定的两个点之间的距离并返回
  • GEOHASH:将指定member的坐标转为hash字符串形式并返回
  • GEOPOS:返回指定member的坐标
  • GEORADIUS:指定圆心、半径,找到该圆内包含的所有member,并按照与圆心之间的距离排序后返回。6.以后已废弃
  • GEOSEARCH:在指定范围内搜索member,并按照与指定点之间的距离排序后返回。范围可以是圆形或矩形。6.2.新功能
  • GEOSEARCHSTORE:与GEOSEARCH功能一致,不过可以把结果存储到一个指定的key。 6.2.新功能

命令概览:

bash 复制代码
GEOADD key [NX|XX] [CH] longitude latitude member [longitude latitude member ...]
summary: Add one or more geospatial items in the geospatial index represented using a sorted set
since: 3.2.0

GEODIST key member1 member2 [m|km|ft|mi]
summary: Returns the distance between two members of a geospatial index
since: 3.2.0

GEOHASH key member [member ...]
summary: Returns members of a geospatial index as standard geohash strings
since: 3.2.0

GEOPOS key member [member ...]
summary: Returns longitude and latitude of members of a geospatial index
since: 3.2.0

GEORADIUS key longitude latitude radius m|km|ft|mi [WITHCOORD] [WITHDIST] [WITHHASH] [COUNT count [ANY]] [ASC|DESC] [STORE key] [STOREDIST key]
summary: Query a sorted set representing a geospatial index to fetch members matching a given maximum distance from a point
since: 3.2.0

GEORADIUSBYMEMBER key member radius m|km|ft|mi [WITHCOORD] [WITHDIST] [WITHHASH] [COUNT count [ANY]] [ASC|DESC] [STORE key] [STOREDIST key]
summary: Query a sorted set representing a geospatial index to fetch members matching a given maximum distance from a member
since: 3.2.0

GEOSEARCH key [FROMMEMBER member] [FROMLONLAT longitude latitude] [BYRADIUS radius m|km|ft|mi] [BYBOX width height m|km|ft|mi] [ASC|DESC] [COUNT count [ANY]] [WITHCOORD] [WITHDIST] [WITHHASH]
summary: Query a sorted set representing a geospatial index to fetch members inside an area of a box or a circle.
since: 6.2

GEOSEARCHSTORE destination source [FROMMEMBER member] [FROMLONLAT longitude latitude] [BYRADIUS radius m|km|ft|mi] [BYBOX width height m|km|ft|mi] [ASC|DESC] [COUNT count [ANY]] [WITHCOORD] [WITHDIST] [WITHHASH] [STOREDIST]
summary: Query a sorted set representing a geospatial index to fetch members inside an area of a box or a circle, and store the result in another key.
since: 6.2

用例:

bash 复制代码
127.0.0.1:6379> geoadd geo1 111.203200 27.680740 yangxi
(integer) 1
127.0.0.1:6379> geoadd geo1 111.324864 27.736339 xinhua
(integer) 1
127.0.0.1:6379> geodist geo1 xinhua yangxi km
"13.4824"
127.0.0.1:6379> geodist geo1 xinhua yangxi m
"13482.4195"
127.0.0.1:6379> geohash geo1 xinhua
1) "wkzjv30rck0"
127.0.0.1:6379> geohash geo1 yangxi
1) "wkzj6rpsn20"
127.0.0.1:6379> geopos geo1 xinhua
1) 1) "111.32486253976821899"
   2) "27.73633963669661284"
127.0.0.1:6379> geosearch geo1 frommember yangxi byradius 20 km withdist
1) 1) "yangxi"
   2) "0.0000"
2) 1) "xinhua"
   2) "13.4824"

具体应用在查询附近的人啊,商店,等跟地理位置有关的。

相关推荐
靖顺2 小时前
【OceanBase 诊断调优】—— packet fly cost too much time 的根因分析
数据库·oceanbase
liuxin334455662 小时前
学籍管理系统:实现教育管理现代化
java·开发语言·前端·数据库·安全
B1nna3 小时前
Redis学习(三)缓存
redis·学习·缓存
yuanbenshidiaos4 小时前
C++--------------树
java·数据库·c++
dengjiayue6 小时前
MySQL 查询大偏移量(LIMIT)问题分析
数据库·mysql
言之。6 小时前
【MySQL】在MySQL中如何定位慢查询?
数据库·mysql
DashVector6 小时前
如何通过HTTP API插入Doc
数据库·人工智能·http·阿里云·向量检索
DashVector6 小时前
如何通过HTTP API分组检索Doc
服务器·数据库·http·数据库开发·数据库架构
Suwg2096 小时前
【MySQL】踩坑笔记——保存带有换行符等特殊字符的数据,需要进行转义保存
数据库·笔记·mysql
2401_857610036 小时前
中文学习系统:成本效益分析与系统优化
java·数据库·学习·架构