38.附近商户实现

geo数据结构geolocation,地理坐标。

存储地理坐标信息

根据经纬度检索数据。

bash 复制代码
[root@localhost ~]# redis-cli
127.0.0.1:6379> geoadd g1 116.378248 39.865275 bjn 116.42803 39.903738 bjz 116.322287 39.893729 bjx
(integer) 3
127.0.0.1:6379>

获取北京南到北京西的距离,默认单位为米,指定单位km为千米。

返回北京站的坐标

返回北京站坐标的hash值

score存的是店铺经纬度坐标。这里的值可以代表和转化为真正的经纬度。

key 存入店铺类型type id,将同类型的数据放到一起。

value存入店铺的id.

修改pom文件

java 复制代码
<!--redis springDataRedis2.3.9 并不支持redis6.2提供的GEOSearch命令,因此需要修改pom文件-->
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-data-redis</artifactId>
            <exclusions>
                <exclusion>
                    <groupId>org.springframework.data</groupId>
                    <artifactId>spring-data-redis</artifactId>
                </exclusion>
                <exclusion>
                    <artifactId>lettuce-core</artifactId>
                    <groupId>io.lettuce</groupId>
                </exclusion>
            </exclusions>
        </dependency>

        <dependency>
            <groupId>org.springframework.data</groupId>
            <artifactId>spring-data-redis</artifactId>
            <version>2.6.2</version>
        </dependency>

        <dependency>
            <artifactId>lettuce-core</artifactId>
            <groupId>io.lettuce</groupId>
            <version>6.1.6.RELEASE</version>
        </dependency>

向redis中添加数据

java 复制代码
 @Test
    public void loadShopData() {
        //1.查询店铺信息
        List<Shop> shops = shopService.list();
        //2.按照店铺类型分组,typeId一致的放入一个集合
        Map<Long, List<Shop>> typeIdShops = shops.stream().collect(Collectors.groupingBy(Shop::getTypeId));
        //3.分批完成写入redis
        for (Map.Entry<Long, List<Shop>> entry : typeIdShops.entrySet()) {
            Long typeId = entry.getKey();
            String key = "shop:geo:" + typeId;
            List<Shop> shopList = entry.getValue();
            List<RedisGeoCommands.GeoLocation<String>> stringGeoLocation = new ArrayList<>(typeIdShops.size());
            //写入redis geoadd key jd wd member
            for (Shop shop : shopList) {
                //一个一个添加
//                stringRedisTemplate.opsForGeo().add(key,
//                        new Point(shop.getY(), shop.getX()),
//                        shop.getId().toString());
                //批量组装数据
                RedisGeoCommands.GeoLocation<String> geoLocation =
                        new RedisGeoCommands.GeoLocation<>(shop.getId().toString(),
                                new Point(shop.getY(), shop.getX()));
                stringGeoLocation.add(geoLocation);
            }
            //批量写入redis
            stringRedisTemplate.opsForGeo().add(key, stringGeoLocation);
        }

    }
java 复制代码
 @GetMapping("/of/type")
    public ApiResponse queryShopByType(@RequestParam("typeId") Integer typeId,
                                       @RequestParam(value = "current", defaultValue = "1") Integer current,
                                       @RequestParam(value = "x", required = false) Double x,
                                       @RequestParam(value = "y", required = false) Double y
                                       ) {
        List<Shop> shops = shopService.queryShopByType(typeId, current, x, y);
        return ApiResponse.success(shops);
    }
java 复制代码
@Override
    public List<Shop> queryShopByType(Integer typeId, Integer current, Double x, Double y) {
        int pageSize = 5;
        //1.判断是否需要根据坐标查询
        if(x == null || y == null) {
            Page<Shop> page = query().eq("type_id", typeId).page(new Page<>(current, pageSize));
            return page.getRecords();
        }
        //2.计算分页参数
        int from = (current -1) * pageSize;
        int end = current * pageSize;
        //3.查询redis,按照距离排序、分页。结果:shopId,distance
        // geosearch key fromlonlat x y byradius 10 km with dist
        String key = "shop:geo:" + typeId;
        GeoResults<RedisGeoCommands.GeoLocation<String>> results = stringRedisTemplate.opsForGeo().search(key,
                GeoReference.fromCoordinate(x, y),
                //默认单位米,寻找五千米范围内的店铺
                new Distance(5000),
                //返回包含距离
                RedisGeoCommands.GeoSearchCommandArgs.newGeoSearchArgs().includeDistance()
                        //默认从地0-end条的数据记录
                        .limit(end));
        //4.解析出id
        if(results == null) {
            return new ArrayList<>();
        }
        List<GeoResult<RedisGeoCommands.GeoLocation<String>>> content = results.getContent();
        if(content.size() <= from) {
            //说明没有下一页了,直接返回空集合
            return new ArrayList<>();
        }
        //截取从from-end部分的数据
        List<Long> ids = new ArrayList<>(content.size());
        Map<String, Distance> distanceMap = new HashMap<>(content.size());
        content.stream().skip(from).forEach(e -> {
            //member值,店铺id
            String shopIdStr = e.getContent().getName();
            ids.add(Long.valueOf(shopIdStr));
            //获取距离
            Distance distance = e.getDistance();
            distanceMap.put(shopIdStr, distance);
        });

        //5.根据id查询shop
        String idStr = StrUtil.join(",", ids);
        List<Shop> shopList = query().in("id", ids).last("order by field (id," + idStr + ")").list();

        //6.返回shop集合
        shopList.stream().forEach(e -> e.setDistance(distanceMap.get(e.getId().toString()).getValue()));
        return shopList;

    }
相关推荐
独好紫罗兰1 分钟前
对python的再认识-基于数据结构进行-a005-元组-CRUD
开发语言·数据结构·python
曾经的三心草5 分钟前
redis-9-集群
java·redis·mybatis
sun032210 分钟前
【架构基础】Spring中的PropertySourcesPlaceholderConfigurer介绍 (并非新知识,比较古老的一种使用方式)
java·spring·架构
chilavert31812 分钟前
技术演进中的开发沉思-356:重排序(中)
java·开发语言
毕设源码-邱学长12 分钟前
【开题答辩全过程】以 基于SSM的儿童福利院管理系统为例,包含答辩的问题和答案
java·eclipse
devmoon16 分钟前
为 Pallet 搭建最小化 Mock Runtime 并编写单元测试环境
开发语言·单元测试·区块链·智能合约·polkadot
TT哇19 分钟前
【实习】数字营销系统 银行经理端(interact_bank)前端 Vue 移动端页面的 UI 重构与优化
java·前端·vue.js·ui
Elieal29 分钟前
SpringBoot 数据层开发与企业信息管理系统实战
java·spring boot·后端
识君啊29 分钟前
MyBatis-Plus 逻辑删除导致唯一索引冲突的解决方案
java·spring boot·mybatis·mybatis-plus·唯一索引·逻辑删除
Coder_Boy_30 分钟前
Java开发者破局指南:跳出内卷,借AI赋能,搭建系统化知识体系
java·开发语言·人工智能·spring boot·后端·spring