网站培训班有哪些课程,沈阳网站的优化,网站建设用哪种语言最好,WordPress即时群聊GEO数据结构的基本用法
GEO就是Geolocation的简写形式#xff0c;代表地理坐标。Redis在3.2版本中加入了对GEO的支持#xff0c;允许存储地理坐标信息#xff0c;帮助我们根据经纬度来检索数据。常见的命令有#xff1a;
GEOADD#xff1a;添加一个地理空间信息#xf…GEO数据结构的基本用法
GEO就是Geolocation的简写形式代表地理坐标。Redis在3.2版本中加入了对GEO的支持允许存储地理坐标信息帮助我们根据经纬度来检索数据。常见的命令有
GEOADD添加一个地理空间信息包含经度longitude、纬度latitude、值memberGEODIST计算指定的两个点之间的距离并返回GEOHASH将指定member的坐标转为hash字符串形式并返回GEOPOS返回指定member的坐标GEORADIUS指定圆心、半径找到该圆内包含的所有member并按照与圆心之间的距离排序后返回。6.以后已废弃GEOSEARCH在指定范围内搜索member并按照与指定点之间的距离排序后返回。范围可以是圆形或矩形。6.2.新功能GEOSEARCHSTORE与GEOSEARCH功能一致不过可以把结果存储到一个指定的key。 6.2.新功能
导入店铺数据到GEO
当我们点击美食之后会出现一系列的商家商家中可以按照多种排序方式我们此时关注的是距离这个地方就需要使用到我们的GEO向后台传入当前app收集的地址(我们此处是写死的) 以当前坐标作为圆心同时绑定相同的店家类型type以及分页信息把这几个条件传入后台后台查询出对应的数据再返回。
我们要做的事情是将数据库表中的数据导入到redis中去redis中的GEOGEO在redis中就一个menber和一个经纬度我们把x和y轴传入到redis做的经纬度位置去但我们不能把所有的数据都放入到menber中去毕竟作为redis是一个内存级数据库如果存海量数据redis还是力不从心所以我们在这个地方存储他的id即可。
但是这个时候还有一个问题就是在redis中并没有存储type所以我们无法根据type来对数据进行筛选所以我们可以按照商户类型做分组类型相同的商户作为同一组以typeId为key存入同一个GEO集合中即可
Test
void loadShopData() {// 1.查询店铺信息ListShop list shopService.list();// 2.把店铺分组按照typeId分组typeId一致的放到一个集合MapLong, ListShop map list.stream().collect(Collectors.groupingBy(Shop::getTypeId));// 3.分批完成写入Redisfor (Map.EntryLong, ListShop entry : map.entrySet()) {// 3.1.获取类型idLong typeId entry.getKey();String key SHOP_GEO_KEY typeId;// 3.2.获取同类型的店铺的集合ListShop value entry.getValue();ListRedisGeoCommands.GeoLocationString locations new ArrayList(value.size());// 3.3.写入redis GEOADD key 经度 纬度 memberfor (Shop shop : value) {// stringRedisTemplate.opsForGeo().add(key, new Point(shop.getX(), shop.getY()), shop.getId().toString());locations.add(new RedisGeoCommands.GeoLocation(shop.getId().toString(),new Point(shop.getX(), shop.getY())));}stringRedisTemplate.opsForGeo().add(key, locations);}
}
实现附近商户功能
SpringDataRedis的2.3.9版本并不支持Redis 6.2提供的GEOSEARCH命令因此我们需要提示其版本修改自己的POM
dependencygroupIdorg.springframework.boot/groupIdartifactIdspring-boot-starter-data-redis/artifactIdexclusionsexclusionartifactIdspring-data-redis/artifactIdgroupIdorg.springframework.data/groupId/exclusionexclusionartifactIdlettuce-core/artifactIdgroupIdio.lettuce/groupId/exclusion/exclusions
/dependency
dependencygroupIdorg.springframework.data/groupIdartifactIdspring-data-redis/artifactIdversion2.6.2/version
/dependency
dependencygroupIdio.lettuce/groupIdartifactIdlettuce-core/artifactIdversion6.1.6.RELEASE/version
/dependency
ShopController
GetMapping(/of/type)
public Result 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
) {return shopService.queryShopByType(typeId, current, x, y);
}
ShopServiceImpl
Overridepublic Result queryShopByType(Integer typeId, Integer current, Double x, Double y) {// 1.判断是否需要根据坐标查询if (x null || y null) {// 不需要坐标查询按数据库查询PageShop page query().eq(type_id, typeId).page(new Page(current, SystemConstants.DEFAULT_PAGE_SIZE));// 返回数据return Result.ok(page.getRecords());}// 2.计算分页参数int from (current - 1) * SystemConstants.DEFAULT_PAGE_SIZE;int end current * SystemConstants.DEFAULT_PAGE_SIZE;// 3.查询redis、按照距离排序、分页。结果shopId、distanceString key SHOP_GEO_KEY typeId;GeoResultsRedisGeoCommands.GeoLocationString results stringRedisTemplate.opsForGeo() // GEOSEARCH key BYLONLAT x y BYRADIUS 10 WITHDISTANCE.search(key,GeoReference.fromCoordinate(x, y),new Distance(5000),RedisGeoCommands.GeoSearchCommandArgs.newGeoSearchArgs().includeDistance().limit(end));// 4.解析出idif (results null) {return Result.ok(Collections.emptyList());}ListGeoResultRedisGeoCommands.GeoLocationString list results.getContent();if (list.size() from) {// 没有下一页了结束return Result.ok(Collections.emptyList());}// 4.1.截取 from ~ end的部分ListLong ids new ArrayList(list.size());MapString, Distance distanceMap new HashMap(list.size());list.stream().skip(from).forEach(result - {// 4.2.获取店铺idString shopIdStr result.getContent().getName();ids.add(Long.valueOf(shopIdStr));// 4.3.获取距离Distance distance result.getDistance();distanceMap.put(shopIdStr, distance);});// 5.根据id查询ShopString idStr StrUtil.join(,, ids);ListShop shops query().in(id, ids).last(ORDER BY FIELD(id, idStr )).list();for (Shop shop : shops) {shop.setDistance(distanceMap.get(shop.getId().toString()).getValue());}// 6.返回return Result.ok(shops);}