美文网首页
redis中geo的使用

redis中geo的使用

作者: 风一样的存在 | 来源:发表于2024-09-08 10:37 被阅读0次

判断一个坐标周边相近的坐标:

  • 在redisson的使用:
double latitude = 32.668206, longitude = -97.248256;
RGeo<String> geo = redisson.getGeo("key");
Map<String, Double> result = geo.radiusWithDistance(longitude,latitude,10, GeoUnit.METERS);
  • 在spring-data-redis中的使用:
Circle circle = new Circle(new Point(116.397128, 39.916527), new Distance(10, Metrics.MILES));
GeoResults<RedisGeoCommands.GeoLocation<String>> geoResults = redisTemplate.opsForGeo().radius("key",circle);
  • 在jedis中的使用:
try(Jedis jedis = jedisPool.getResource()) {
      List<GeoRadiusResponse> list = jedis.georadius("key", longitude , latitude , 10, GeoUnit.M);
}

使用redisson的时候,会遇到如下的问题,修改版本号3.8.0解决(版本超过3.8.0默认使用的read only模式查询,低版本redis不支持)。

org.redisson.client.RedisException: ERR unknown command 'GEORADIUS_RO'. channel: [id: 0xadbfd179, L:/127.0.0.1:52191 - R:/127.0.0.1:6379] command: (GEORADIUS_RO), params: [geo-expedia-US, -97.248256, 32.668206, 10.0, m, WITHDIST]

    at org.redisson.client.handler.CommandDecoder.decode(CommandDecoder.java:343)
    at org.redisson.client.handler.CommandDecoder.decodeCommand(CommandDecoder.java:178)
    at org.redisson.client.handler.CommandDecoder.decode(CommandDecoder.java:117)
    at org.redisson.client.handler.CommandDecoder.decode(CommandDecoder.java:102)

maven对应的依赖:

<dependency>
      <groupId>org.redisson</groupId>
      <artifactId>redisson-spring-boot-starter</artifactId>
      <version>2.14.0</version>
</dependency>

相关文章

网友评论

      本文标题:redis中geo的使用

      本文链接:https://www.haomeiwen.com/subject/yogsljtx.html