使用redis用于地理空间(稳定版本)

时间:2023-02-13 05:17:47

I am trying to model on Redis a way to get all nearby users for a longitude,latitude inputs.

我正在尝试用Redis模型来获取所有附近用户的经度和纬度输入。

That means I need to understand how I should Insert key,value to redis and which data-structure I should use

这意味着我需要理解我应该如何插入key,value to redis,以及我应该使用哪种数据结构。

And on the other hand how I should search in redis for nearby Users by a given longtitude,latitude input

而另一方面,我应该如何在redis中搜索附近的用户一个给定的,纬度输入。

I know it's available on Redis 3.2. but we still using the last redis redis (3.0.3)

我知道它在Redis 3.2上可用。但是我们仍然使用最后的redis redis (3.0.3)

Thank you, ray.

谢谢你,雷。

So for this input: key: userId value

因此,对于这个输入:key: userId值。

1 个解决方案

#1


2  

Redis geo data is organized within Geo-Sets (backed by sorted sets). The approach by having userIds associated with geo data fits well to the Redis Geo support.

Redis geo数据是在地理集合中组织的(由已排序的集合支持)。使用与geo数据相关的userIds的方法非常适合Redis geo支持。

You could maintain one Geo-Set per (domain?/country?) for partitioning reasons. I could not find anything about sorted set size limits. A Redis hash can contain up to 2^32 - 1 field-value pairs and I'm not sure whether this is applicable somehow to sorted sets. So you have one set for simplicity now that is identified by a key, and that holds some members that are associated with geo locations:

由于分区的原因,您可以维护一个(域?/国家?)的地理集。我找不到任何排序的大小限制。复述,散列表可以包含2 ^ 32 - 1同一条我不确定这是否以某种方式适用于分类集。所以你有一个简单的集合,它是由一个键识别的,它包含一些与geo位置相关的成员:

You [GEOADD](http://redis.io/commands/geoadd) some-set-name long1 lat1 userId1 long2 lat2 userId2 ... longN latN userIdN and then the set contains some entries.

您[GEOADD](http://redis.io/commands/geoadd) someset名称long1 lat1 userId1 long2 lat2 userId2…longN latN userIdN,然后这个集合包含一些条目。

Example

例子

GEOADD key 8.6638775 49.5282537 "Weinheim" 8.3796281 48.9978127 "Office Tower" 8.665351, 49.553302 "Train-Station"

The example command adds three places to the Geo-Set identified with key.

示例命令将三个位置添加到带有密钥的gegeset中。

You can query now using [GEORADIUS](http://redis.io/commands/georadius) some-set-name long lat radius m|km|ft|mi to query the set and retrieve all members within the radius around the center of long/lat.

您现在可以使用[GEORADIUS](http://redis.io/commands/georadius)来查询在长/lat中心半径范围内查询该集合并检索所有成员的长lat半径m|km|。

Example

例子

GEORADIUS key 8.6582861 49.5285695 1 KM

The example command queries all elements within a 1 km radius and would return in this case Weinheim as the result.

该示例命令查询半径1公里范围内的所有元素,并在此情况下返回Weinheim。

You can query a Geo-Set also by a set member. Means if you use a location that exists within the set, you can use that as reference instead of specifying the geo location using long/lat (example use case: Search all users within the radius of userId X):

您也可以通过一个集合成员查询一个地理集。意味着,如果您使用的是在集合中存在的位置,您可以使用该位置作为引用,而不是使用long/lat(示例用例:在userId X范围内搜索所有用户)来指定geo位置:

[GEORADIUSBYMEMBER](http://redis.io/commands/georadiusbymember) key member radius m|km|ft|mi

[GEORADIUSBYMEMBER](http://redis.io/commands/georadiusbymember)关键成员半径m|km|ft|mi。

Example

例子

GEORADIUSBYMEMBER key "Train-Station" 5 KM

The result of the command returns the set members within a 5 km radius around Train-Station. The resulting elements are: Train-Station and Weinheim.

该命令的结果返回位于列车站周围5公里半径内的集合成员。由此产生的元素有:火车站和Weinheim。

lettuce is the first Java client that has native support for all Geo-Commands. You can find here the Unit-Test, which demonstrates the API usage.

生菜是第一个支持所有地理命令的Java客户端。您可以在这里找到单元测试,它演示了API的用法。

Please note that the example commands display just a subset of the Geo features, read more within the Redis docs.

请注意,示例命令仅显示Geo特性的一个子集,在Redis文档中读得更多。

#1


2  

Redis geo data is organized within Geo-Sets (backed by sorted sets). The approach by having userIds associated with geo data fits well to the Redis Geo support.

Redis geo数据是在地理集合中组织的(由已排序的集合支持)。使用与geo数据相关的userIds的方法非常适合Redis geo支持。

You could maintain one Geo-Set per (domain?/country?) for partitioning reasons. I could not find anything about sorted set size limits. A Redis hash can contain up to 2^32 - 1 field-value pairs and I'm not sure whether this is applicable somehow to sorted sets. So you have one set for simplicity now that is identified by a key, and that holds some members that are associated with geo locations:

由于分区的原因,您可以维护一个(域?/国家?)的地理集。我找不到任何排序的大小限制。复述,散列表可以包含2 ^ 32 - 1同一条我不确定这是否以某种方式适用于分类集。所以你有一个简单的集合,它是由一个键识别的,它包含一些与geo位置相关的成员:

You [GEOADD](http://redis.io/commands/geoadd) some-set-name long1 lat1 userId1 long2 lat2 userId2 ... longN latN userIdN and then the set contains some entries.

您[GEOADD](http://redis.io/commands/geoadd) someset名称long1 lat1 userId1 long2 lat2 userId2…longN latN userIdN,然后这个集合包含一些条目。

Example

例子

GEOADD key 8.6638775 49.5282537 "Weinheim" 8.3796281 48.9978127 "Office Tower" 8.665351, 49.553302 "Train-Station"

The example command adds three places to the Geo-Set identified with key.

示例命令将三个位置添加到带有密钥的gegeset中。

You can query now using [GEORADIUS](http://redis.io/commands/georadius) some-set-name long lat radius m|km|ft|mi to query the set and retrieve all members within the radius around the center of long/lat.

您现在可以使用[GEORADIUS](http://redis.io/commands/georadius)来查询在长/lat中心半径范围内查询该集合并检索所有成员的长lat半径m|km|。

Example

例子

GEORADIUS key 8.6582861 49.5285695 1 KM

The example command queries all elements within a 1 km radius and would return in this case Weinheim as the result.

该示例命令查询半径1公里范围内的所有元素,并在此情况下返回Weinheim。

You can query a Geo-Set also by a set member. Means if you use a location that exists within the set, you can use that as reference instead of specifying the geo location using long/lat (example use case: Search all users within the radius of userId X):

您也可以通过一个集合成员查询一个地理集。意味着,如果您使用的是在集合中存在的位置,您可以使用该位置作为引用,而不是使用long/lat(示例用例:在userId X范围内搜索所有用户)来指定geo位置:

[GEORADIUSBYMEMBER](http://redis.io/commands/georadiusbymember) key member radius m|km|ft|mi

[GEORADIUSBYMEMBER](http://redis.io/commands/georadiusbymember)关键成员半径m|km|ft|mi。

Example

例子

GEORADIUSBYMEMBER key "Train-Station" 5 KM

The result of the command returns the set members within a 5 km radius around Train-Station. The resulting elements are: Train-Station and Weinheim.

该命令的结果返回位于列车站周围5公里半径内的集合成员。由此产生的元素有:火车站和Weinheim。

lettuce is the first Java client that has native support for all Geo-Commands. You can find here the Unit-Test, which demonstrates the API usage.

生菜是第一个支持所有地理命令的Java客户端。您可以在这里找到单元测试,它演示了API的用法。

Please note that the example commands display just a subset of the Geo features, read more within the Redis docs.

请注意,示例命令仅显示Geo特性的一个子集,在Redis文档中读得更多。