redis操作GEO

1.springboot项目下引入redis依赖

<dependency>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-starter-data-redis</artifactId>
</dependency>
<!-- redis 连接池  -->
<dependency>
    <groupId>org.apache.commons</groupId>
    <artifactId>commons-pool2</artifactId>
</dependency>

2.yml配置redis

spring:
  data:
    redis:
      host: localhost
      port: 6379
      database: 0
      lettuce:
        pool:
          min-idle: 0
          max-idle: 8
          max-wait: -1ms
          max-active: 16

3.代码区

3.1 config构建

设置序列化

package com.wty.javaspringboot3.config;

import com.fasterxml.jackson.annotation.JsonAutoDetect;
import com.fasterxml.jackson.annotation.JsonTypeInfo;
import com.fasterxml.jackson.annotation.PropertyAccessor;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.fasterxml.jackson.databind.jsontype.impl.LaissezFaireSubTypeValidator;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.data.redis.connection.RedisConnectionFactory;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.data.redis.serializer.Jackson2JsonRedisSerializer;
import org.springframework.data.redis.serializer.StringRedisSerializer;


@Configuration
public class RedisConfig {
    @Bean
    public RedisTemplate<String, Object> redisTemplate(RedisConnectionFactory redisConnectionFactory) {
        RedisTemplate<String, Object> template = new RedisTemplate<>();
        template.setConnectionFactory(redisConnectionFactory);

        // 设置objectMapper:转换java对象的时候使用
        ObjectMapper objectMapper = new ObjectMapper();
        objectMapper.setVisibility(PropertyAccessor.ALL, JsonAutoDetect.Visibility.ANY);
        objectMapper.activateDefaultTyping( LaissezFaireSubTypeValidator.instance, ObjectMapper.DefaultTyping.NON_FINAL,  JsonTypeInfo.As.WRAPPER_ARRAY);
        Jackson2JsonRedisSerializer<Object> jackson2JsonRedisSerializer = new Jackson2JsonRedisSerializer<>(objectMapper, Object.class);
        StringRedisSerializer stringRedisSerializer = new StringRedisSerializer();

        // 设置key/value值的序列化方式
        template.setKeySerializer(stringRedisSerializer);
        template.setValueSerializer(jackson2JsonRedisSerializer);
        template.setHashKeySerializer(stringRedisSerializer);
        template.setHashValueSerializer(jackson2JsonRedisSerializer);

        template.afterPropertiesSet();
        return template;
    }
}

3.2 service构建

一些功能的demo

package com.wty.javaspringboot3.service;

import com.wty.javaspringboot3.utils.DistanceMetric;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.geo.*;
import org.springframework.data.redis.connection.RedisGeoCommands;
import org.springframework.data.redis.core.GeoOperations;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.Objects;

@Service
public class GeoService {

    private final RedisTemplate<String, Object> redisTemplate;

    @Autowired
    public GeoService(RedisTemplate<String, Object> redisTemplate) {
        this.redisTemplate = redisTemplate;
    }

    public void addLocation(String key, double longitude, double latitude, String member) {
        GeoOperations<String, Object> geoOps = redisTemplate.opsForGeo();
        geoOps.add(key, new RedisGeoCommands.GeoLocation<>(member, new Point(longitude, latitude)));
    }

    public Distance getDistance(String key, String member1, String member2, DistanceMetric metric) {
        GeoOperations<String, Object> geoOps = redisTemplate.opsForGeo();
        // Check if both members exist
        List<Point> positions = geoOps.position(key, member1, member2);
        if (positions.stream().allMatch(Objects::nonNull)) {
            return geoOps.distance(key, member1, member2, metric.getMetric());
        } else {
            throw new IllegalArgumentException("One or both members do not exist.");
        }
    }

    public GeoResults<RedisGeoCommands.GeoLocation<Object>> getLocationsWithinRadius(String key, double longitude, double latitude, double radius, DistanceMetric metric) {
        GeoOperations<String, Object> geoOps = redisTemplate.opsForGeo();
        Circle within = new Circle(new Point(longitude, latitude), new Distance(radius, metric.getMetric()));
        return geoOps.radius(key, within);
    }

    public List<Point> getPositions(String key, String... members) {
        GeoOperations<String, Object> geoOps = redisTemplate.opsForGeo();
        return geoOps.position(key, members);
    }
}

3.3 controller构建

接口测试

package com.wty.javaspringboot3.controller;

import com.wty.javaspringboot3.service.GeoService;
import com.wty.javaspringboot3.utils.DistanceMetric;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.geo.Distance;
import org.springframework.data.geo.GeoResults;
import org.springframework.data.geo.Point;
import org.springframework.data.redis.connection.RedisGeoCommands;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;

@RestController
public class GeoController {

    private final GeoService geoService;

    @Autowired
    public GeoController(GeoService geoService) {
        this.geoService = geoService;
    }

    // 给member添加位置信息
    @GetMapping("/addLocation")
    public String addLocation(@RequestParam String key, @RequestParam double longitude, @RequestParam double latitude, @RequestParam String member) {
        geoService.addLocation(key, longitude, latitude, member);
        return "Location added.";
    }

    // 求出两个members的举例
    @GetMapping("/getDistance")
    public Distance getDistance(@RequestParam String key, @RequestParam String member1, @RequestParam String member2, @RequestParam String metric) {
        DistanceMetric distanceMetric = DistanceMetric.fromString(metric);
        return geoService.getDistance(key, member1, member2, distanceMetric);
    }

    // 根据经纬度 + 范围,求出范围内的member
    @GetMapping("/getLocationsWithinRadius")
    public GeoResults<RedisGeoCommands.GeoLocation<Object>> getLocationsWithinRadius(@RequestParam String key, @RequestParam double longitude, @RequestParam double latitude, @RequestParam double radius, @RequestParam String metric) {
        DistanceMetric distanceMetric = DistanceMetric.fromString(metric);
        return geoService.getLocationsWithinRadius(key, longitude, latitude, radius, distanceMetric);
    }

    // 获取members的经纬度
    @GetMapping("/getPositions")
    public List<Point> getPositions(@RequestParam String key, @RequestParam String... members) {
        return geoService.getPositions(key, members);
    }
}

4.postman测试

4.1 添加北京、上海位置

# 添加北京的位置
curl "http://localhost:8080/addLocation?key=cities&longitude=116.4074&latitude=39.9042&member=Beijing"

# 添加上海的位置
curl "http://localhost:8080/addLocation?key=cities&longitude=121.4737&latitude=31.2304&member=Shanghai"

在redis可视化工具中可以看到:

4.2 获取北京、上海之间的距离

4.3 获取范围内的地点

4.4 获取成员的经纬度

Tips:

- 以上只是学习中做一个demo,跑通流程,初步了解GEO数据类型,没有深度进行了解

- 有其它错误,查看自己的redis客户端是否支持GEO数据类型

  • 5
    点赞
  • 9
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值