Redis从入门到入坑09——Geospatial 数据类型
Geospatial 数据类型简介(官网原文)
Redis geospatial indexes let you store coordinates and search for them. This data structure is useful for finding nearby points within a given radius or bounding box
Redis geospatial indexes允许您存储坐标并搜索它们。这种数据结构对于在给定半径或边界框内查找附近的点非常有用
geospatial数据类型常用命令
查看 geospatial 数据类型下所有命令: help @geo
127.0.0.1:6379> help @geo
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
GEORADIUSBYMEMBER_RO key member radius M|KM|FT|MI [WITHCOORD] [WITHDIST] [WITHHASH] [COUNT count [ANY]] [ASC|DESC]
summary: A read-only variant for GEORADIUSBYMEMBER
since: 3.2.10
GEORADIUS_RO key longitude latitude radius M|KM|FT|MI [WITHCOORD] [WITHDIST] [WITHHASH] [COUNT count [ANY]] [ASC|DESC]
summary: A read-only variant for GEORADIUS
since: 3.2.10
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.0
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]] [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.0
GEOADD key [NX|XX] [CH] longitude latitude member [longitude latitude member ...]:将指定的地理空间项(经度、纬度、名称)添加到指定的键。数据以排序集的形式存储在键中,这样就可以使用GEOSEARCH命令查询这些项。
--
127.0.0.1:6379> GEOADD Sicily 13.361389 38.115556 Palermo 15.087269 37.502669 Catania
(integer) 2
GEODIST key member1 member2 [M|KM|FT|MI]: 获取两地之间的距离,[M|KM|FT|MI] 参数是距离单位,默认使用的是 M
127.0.0.1:6379> GEODIST Sicily Palermo Catania
"166274.1516"
-- 公里
127.0.0.1:6379> GEODIST Sicily Palermo Catania KM
"166.2742"
-- 英里
127.0.0.1:6379> GEODIST Sicily Palermo Catania MI
"103.3182"
-- 英尺
127.0.0.1:6379> GEODIST Sicily Palermo Catania FT
"545518.8700"
GEOHASH key member [member ...]:该命令返回一个数组,其中每个元素都是与作为参数传递给命令的每个成员名对应的Geohash
127.0.0.1:6379> GEOADD Sicily 13.361389 38.115556 "Palermo" 15.087269 37.502669 "Catania"
(integer) 0
127.0.0.1:6379> GEOHASH Sicily Palermo Catania
1) "sqc8b49rny0"
2) "sqdtr74hyu0"
GEOPOS key member [member ...]:该命令返回一个数组,其中每个元素都是一个双元素数组,表示作为参数传递给命令的每个成员名的经度和纬度(x,y)。不存在的元素被报告为数组的NULL元素。
127.0.0.1:6379> GEOPOS Sicily Palermo Catania
1) 1) "13.36138933897018433"
2) "38.11555639549629859"
2) 1) "15.08726745843887329"
2) "37.50266842333162032"
-- 不存在的则会报 nil
127.0.0.1:6379> GEOPOS Sicily Palermo Catania NonExisting
1) 1) "13.36138933897018433"
2) "38.11555639549629859"
2) 1) "15.08726745843887329"
2) "37.50266842333162032"
3) (nil)
GEORADIUS key longitude latitude radius M|KM|FT|MI [WITHCOORD] [WITHDIST] [WITHHASH] [COUNT count [ANY]] [ASC|DESC] [STORE key] [STOREDIST key]:返回使用GEOADD填充地理空间信息的已排序集合的成员,这些成员位于用中心位置和到中心的最大距离(半径)指定的区域的边界内(以给定的经纬度为中心, 返回键包含的位置元素当中, 与中心的距离不超过给定最大距离的所有位置元素。)
GEORADIUS 中文乱码:在连接redis执行redi-cli 命令时加上 --raw 参数则能解决
127.0.0.1:6379> GEOADD city 116.403963 39.915119 "天安门" 116.403414 39.924091 "故宫" 116.024067 40.362639 "长城"
(integer) 3
--返回乱码
127.0.0.1:6379> GEORADIUS city 116.418017 39.914402 10 km withdist withcoord count 10 withhash desc
1) 1) "\xe6\x95\x85\xe5\xae\xab"
2) "1.6470"
3) (integer) 4069885568908290
4) 1) "116.40341609716415405"
2) "39.92409008156928252"
2) 1) "\xe5\xa4\xa9\xe5\xae\x89\xe9\x97\xa8"
2) "1.2016"
3) (integer) 4069885555089531
4) 1) "116.40396326780319214"
2) "39.91511970338637383"
127.0.0.1:6379> exit
-- 处理乱码
root@41cf590913c7:/data# redis-cli --raw
-- 乱码解决
127.0.0.1:6379> GEORADIUS city 116.418017 39.914402 10 km withdist withcoord count 10 withhash desc
故宫
1.6470
4069885568908290
116.40341609716415405
39.92409008156928252
天安门
1.2016
4069885555089531
116.40396326780319214
39.91511970338637383
GEORADIUSBYMEMBER key member radius M|KM|FT|MI [WITHCOORD] [WITHDIST] [WITHHASH] [COUNT count [ANY]] [ASC|DESC] [STORE key] [STOREDIST key]:该命令与GEORADIUS完全相同,唯一的区别是,它不使用经纬度值作为要查询的区域的中心,而是使用已存在于由排序集表示的地理空间索引中的成员的名称。
-- 找出位于指定范围内的元素,中心点是由给定的位置元素决定
127.0.0.1:6379> GEORADIUSBYMEMBER city 天安门 10 km withdist withcoord
天安门
0.0000
116.40396326780319214
39.91511970338637383
故宫
0.9988
116.40341609716415405
39.92409008156928252
GEORADIUSBYMEMBER_RO key member radius M|KM|FT|MI [WITHCOORD] [WITHDIST] [WITHHASH] [COUNT count [ANY]] [ASC|DESC]:该命令与GEORADIUSBYMEMBER命令相同,只是它不支持可选的STORE和STOREDIST参数。
127.0.0.1:6379> GEORADIUSBYMEMBER_RO city 故宫 10 km withdist withcoord
天安门
0.9988
116.40396326780319214
39.91511970338637383
故宫
0.0000
116.40341609716415405
39.92409008156928252
GEORADIUS_RO key longitude latitude radius M|KM|FT|MI [WITHCOORD] [WITHDIST] [WITHHASH] [COUNT count [ANY]] [ASC|DESC]:该命令与GEORADIUS命令相同,只是它不支持可选的STORE和STOREDIST参数。
127.0.0.1:6379> GEORADIUS_RO city 116.418017 39.914402 10 km withdist withcoord count 10 withhash desc
故宫
1.6470
4069885568908290
116.40341609716415405
39.92409008156928252
天安门
1.2016
4069885555089531
116.40396326780319214
39.91511970338637383
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]:返回使用GEOADD填充地理空间信息的排序集的成员,这些成员位于给定形状指定的区域的边界内。该命令扩展了GEORADIUS命令,因此除了在圆形区域内搜索之外,它还支持在矩形区域内搜索。
127.0.0.1:6379> GEOSEARCH city FROMLONLAT 116 39 BYBOX 400 400 km ASC WITHCOORD WITHDIST
天安门
107.5345
116.40396326780319214
39.91511970338637383
故宫
108.4636
116.40341609716415405
39.92409008156928252
长城
151.5754
116.02406591176986694
40.36263993239462167
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]] [STOREDIST]: 该命令类似于GEOSEARCH,但将结果存储在目标键中。该命令取代现已弃用的GEORADIUS和GEORADIUSBYMEMBER。默认情况下,它将结果及其地理空间信息存储在目标排序集中。当使用STOREDIST选项时,该命令将项目存储在一个排序的集合中,该集合以浮点数的形式填充它们与圆形或框中心的距离,并使用为该形状指定的相同单位
127.0.0.1:6379> GEOPOS desCity 故宫 长城 天安门
116.40341609716415405
39.92409008156928252
116.02406591176986694
40.36263993239462167
116.40396326780319214
39.91511970338637383
应用场景
- 交友软件中附近的人
- 外卖软件中附近的美食店铺
- 高德地图附近的核酸检查点
- 滴滴打车,最直观的操作就是实时记录更新各个车的位置