Redis 地理空间
Redis 地理空间数据类型的介绍
Redis 地理空间索引允许您存储坐标并搜索它们。此数据结构可用于在给定半径或边界框内查找附近的点。
基本命令
查看 地理空间索引命令的完整列表。
示例
假设您正在构建一个移动应用程序,该应用程序允许您查找距离您当前位置最近的自行车租赁站。
将多个位置添加到地理空间索引
> GEOADD bikes:rentable -122.27652 37.805186 station:1
(integer) 1
> GEOADD bikes:rentable -122.2674626 37.8062344 station:2
(integer) 1
> GEOADD bikes:rentable -122.2469854 37.8104049 station:3
(integer) 1
您是否厌倦了使用 redis-cli?试试 Redis Insight - Redis 的开发者 GUI。
"""
Code samples for Geospatial doc pages:
https://redis.ac.cn/docs/latest/develop/data-types/geospatial/
"""
import redis
r = redis.Redis(decode_responses=True)
res1 = r.geoadd("bikes:rentable", [-122.27652, 37.805186, "station:1"])
print(res1) # >>> 1
res2 = r.geoadd("bikes:rentable", [-122.2674626, 37.8062344, "station:2"])
print(res2) # >>> 1
res3 = r.geoadd("bikes:rentable", [-122.2469854, 37.8104049, "station:3"])
print(res3) # >>> 1
res4 = r.geosearch(
"bikes:rentable",
longitude=-122.27652,
latitude=37.805186,
radius=5,
unit="km",
)
print(res4) # >>> ['station:1', 'station:2', 'station:3']
import assert from 'assert';
import { createClient } from 'redis';
const client = createClient();
await client.connect();
const res1 = await client.geoAdd('bikes:rentable', {
longitude: -122.27652,
latitude: 37.805186,
member: 'station:1'
});
console.log(res1) // 1
const res2 = await client.geoAdd('bikes:rentable', {
longitude: -122.2674626,
latitude: 37.8062344,
member: 'station:2'
});
console.log(res2) // 1
const res3 = await client.geoAdd('bikes:rentable', {
longitude: -122.2469854,
latitude: 37.8104049,
member: 'station:3'
})
console.log(res3) // 1
const res4 = await client.geoSearch(
'bikes:rentable', {
longitude: -122.27652,
latitude: 37.805186,
},
{ radius: 5,
unit: 'km'
}
);
console.log(res4) // ['station:1', 'station:2', 'station:3']
await client.quit()
package io.redis.examples;
import redis.clients.jedis.GeoCoordinate;
import redis.clients.jedis.UnifiedJedis;
import redis.clients.jedis.args.GeoUnit;
import redis.clients.jedis.resps.GeoRadiusResponse;
import java.util.List;
import java.util.stream.Collectors;
import static org.junit.Assert.assertEquals;
public class GeoExample {
public void run() {
try (UnifiedJedis jedis = new UnifiedJedis("redis://localhost:6379")) {
long res1 = jedis.geoadd("bikes:rentable", -122.27652, 37.805186, "station:1");
System.out.println(res1); // 1
long res2 = jedis.geoadd("bikes:rentable", -122.2674626, 37.8062344, "station:2");
System.out.println(res2); // 1
long res3 = jedis.geoadd("bikes:rentable", -122.2469854, 37.8104049, "station:3");
System.out.println(res2); // 1
List<GeoRadiusResponse> res4 = jedis.geosearch(
"bikes:rentable",
new GeoCoordinate(-122.27652, 37.805186),
5,
GeoUnit.KM
);
List<String> members = res4.stream() //
.map(GeoRadiusResponse::getMemberByString) //
.collect(Collectors.toList());
System.out.println(members); // [station:1, station:2, station:3]
}
}
}
using NRedisStack.Tests;
using StackExchange.Redis;
public class Geo_tutorial
{
[SkipIfRedis(Is.OSSCluster)]
public void run()
{
var muxer = ConnectionMultiplexer.Connect("localhost:6379");
var db = muxer.GetDatabase();
bool res1 = db.GeoAdd("bikes:rentable", -122.27652, 37.805186, "station:1");
Console.WriteLine(res1); // >>> True
bool res2 = db.GeoAdd("bikes:rentable", -122.2674626, 37.8062344, "station:2");
Console.WriteLine(res2); // >>> True
bool res3 = db.GeoAdd("bikes:rentable", -122.2469854, 37.8104049, "station:3");
Console.WriteLine(res3); // >>> True
// Tests for 'geoadd' step.
GeoRadiusResult[] res4 = db.GeoSearch("bikes:rentable",
-122.27652,
37.805186,
new GeoSearchCircle(5, GeoUnit.Kilometers)
);
foreach (GeoRadiusResult member in res4)
{
Console.WriteLine($"Member: '{member.Member}', distance: {member.Distance}, position: {member.Position}");
}
// >>> Member: 'station:1', distance: 0.0001, position: -122.27652043104172 37.80518485897756
// >>> Member: 'station:2', distance: 0.8047, position: -122.26745992898941 37.80623423353753
// >>> Member: 'station:3', distance: 2.6596, position: -122.24698394536972 37.81040384984464
// Tests for 'geosearch' step.
}
}
查找给定位置 5 公里半径内的所有位置,并返回到每个位置的距离
> GEOSEARCH bikes:rentable FROMLONLAT -122.2612767 37.7936847 BYRADIUS 5 km WITHDIST
1) 1) "station:1"
2) "1.8523"
2) 1) "station:2"
2) "1.4979"
3) 1) "station:3"
2) "2.2441"
您是否厌倦了使用 redis-cli?试试 Redis Insight - Redis 的开发者 GUI。
"""
Code samples for Geospatial doc pages:
https://redis.ac.cn/docs/latest/develop/data-types/geospatial/
"""
import redis
r = redis.Redis(decode_responses=True)
res1 = r.geoadd("bikes:rentable", [-122.27652, 37.805186, "station:1"])
print(res1) # >>> 1
res2 = r.geoadd("bikes:rentable", [-122.2674626, 37.8062344, "station:2"])
print(res2) # >>> 1
res3 = r.geoadd("bikes:rentable", [-122.2469854, 37.8104049, "station:3"])
print(res3) # >>> 1
res4 = r.geosearch(
"bikes:rentable",
longitude=-122.27652,
latitude=37.805186,
radius=5,
unit="km",
)
print(res4) # >>> ['station:1', 'station:2', 'station:3']
import assert from 'assert';
import { createClient } from 'redis';
const client = createClient();
await client.connect();
const res1 = await client.geoAdd('bikes:rentable', {
longitude: -122.27652,
latitude: 37.805186,
member: 'station:1'
});
console.log(res1) // 1
const res2 = await client.geoAdd('bikes:rentable', {
longitude: -122.2674626,
latitude: 37.8062344,
member: 'station:2'
});
console.log(res2) // 1
const res3 = await client.geoAdd('bikes:rentable', {
longitude: -122.2469854,
latitude: 37.8104049,
member: 'station:3'
})
console.log(res3) // 1
const res4 = await client.geoSearch(
'bikes:rentable', {
longitude: -122.27652,
latitude: 37.805186,
},
{ radius: 5,
unit: 'km'
}
);
console.log(res4) // ['station:1', 'station:2', 'station:3']
await client.quit()
package io.redis.examples;
import redis.clients.jedis.GeoCoordinate;
import redis.clients.jedis.UnifiedJedis;
import redis.clients.jedis.args.GeoUnit;
import redis.clients.jedis.resps.GeoRadiusResponse;
import java.util.List;
import java.util.stream.Collectors;
import static org.junit.Assert.assertEquals;
public class GeoExample {
public void run() {
try (UnifiedJedis jedis = new UnifiedJedis("redis://localhost:6379")) {
long res1 = jedis.geoadd("bikes:rentable", -122.27652, 37.805186, "station:1");
System.out.println(res1); // 1
long res2 = jedis.geoadd("bikes:rentable", -122.2674626, 37.8062344, "station:2");
System.out.println(res2); // 1
long res3 = jedis.geoadd("bikes:rentable", -122.2469854, 37.8104049, "station:3");
System.out.println(res2); // 1
List<GeoRadiusResponse> res4 = jedis.geosearch(
"bikes:rentable",
new GeoCoordinate(-122.27652, 37.805186),
5,
GeoUnit.KM
);
List<String> members = res4.stream() //
.map(GeoRadiusResponse::getMemberByString) //
.collect(Collectors.toList());
System.out.println(members); // [station:1, station:2, station:3]
}
}
}
using NRedisStack.Tests;
using StackExchange.Redis;
public class Geo_tutorial
{
[SkipIfRedis(Is.OSSCluster)]
public void run()
{
var muxer = ConnectionMultiplexer.Connect("localhost:6379");
var db = muxer.GetDatabase();
bool res1 = db.GeoAdd("bikes:rentable", -122.27652, 37.805186, "station:1");
Console.WriteLine(res1); // >>> True
bool res2 = db.GeoAdd("bikes:rentable", -122.2674626, 37.8062344, "station:2");
Console.WriteLine(res2); // >>> True
bool res3 = db.GeoAdd("bikes:rentable", -122.2469854, 37.8104049, "station:3");
Console.WriteLine(res3); // >>> True
// Tests for 'geoadd' step.
GeoRadiusResult[] res4 = db.GeoSearch("bikes:rentable",
-122.27652,
37.805186,
new GeoSearchCircle(5, GeoUnit.Kilometers)
);
foreach (GeoRadiusResult member in res4)
{
Console.WriteLine($"Member: '{member.Member}', distance: {member.Distance}, position: {member.Position}");
}
// >>> Member: 'station:1', distance: 0.0001, position: -122.27652043104172 37.80518485897756
// >>> Member: 'station:2', distance: 0.8047, position: -122.26745992898941 37.80623423353753
// >>> Member: 'station:3', distance: 2.6596, position: -122.24698394536972 37.81040384984464
// Tests for 'geosearch' step.
}
}
了解更多
- Redis Geospatial 解释 通过向您展示如何构建本地公园景点地图来介绍地理空间索引。
- Redis 大学的 RU101 详细介绍了 Redis 地理空间索引。