男孩子怎么做網(wǎng)站賺錢百度推廣售后服務(wù)電話
GEO數(shù)據(jù)結(jié)構(gòu)的基本用法
GEO就是Geolocation的簡寫形式,代表地理坐標(biāo)。Redis在3.2版本中加入了對GEO的支持,允許存儲地理坐標(biāo)信息,幫助我們根據(jù)經(jīng)緯度來檢索數(shù)據(jù)。常見的命令有:
- GEOADD:添加一個(gè)地理空間信息,包含:經(jīng)度(longitude)、緯度(latitude)、值(member)
- GEODIST:計(jì)算指定的兩個(gè)點(diǎn)之間的距離并返回
- GEOHASH:將指定member的坐標(biāo)轉(zhuǎn)為hash字符串形式并返回
- GEOPOS:返回指定member的坐標(biāo)
- GEORADIUS:指定圓心、半徑,找到該圓內(nèi)包含的所有member,并按照與圓心之間的距離排序后返回。6.以后已廢棄
- GEOSEARCH:在指定范圍內(nèi)搜索member,并按照與指定點(diǎn)之間的距離排序后返回。范圍可以是圓形或矩形。6.2.新功能
- GEOSEARCHSTORE:與GEOSEARCH功能一致,不過可以把結(jié)果存儲到一個(gè)指定的key。 6.2.新功能
導(dǎo)入店鋪數(shù)據(jù)到GEO ?
當(dāng)我們點(diǎn)擊美食之后,會出現(xiàn)一系列的商家,商家中可以按照多種排序方式,我們此時(shí)關(guān)注的是距離,這個(gè)地方就需要使用到我們的GEO,向后臺傳入當(dāng)前app收集的地址(我們此處是寫死的) ,以當(dāng)前坐標(biāo)作為圓心,同時(shí)綁定相同的店家類型type,以及分頁信息,把這幾個(gè)條件傳入后臺,后臺查詢出對應(yīng)的數(shù)據(jù)再返回。
我們要做的事情是:將數(shù)據(jù)庫表中的數(shù)據(jù)導(dǎo)入到redis中去,redis中的GEO,GEO在redis中就一個(gè)menber和一個(gè)經(jīng)緯度,我們把x和y軸傳入到redis做的經(jīng)緯度位置去,但我們不能把所有的數(shù)據(jù)都放入到menber中去,畢竟作為redis是一個(gè)內(nèi)存級數(shù)據(jù)庫,如果存海量數(shù)據(jù),redis還是力不從心,所以我們在這個(gè)地方存儲他的id即可。
但是這個(gè)時(shí)候還有一個(gè)問題,就是在redis中并沒有存儲type,所以我們無法根據(jù)type來對數(shù)據(jù)進(jìn)行篩選,所以我們可以按照商戶類型做分組,類型相同的商戶作為同一組,以typeId為key存入同一個(gè)GEO集合中即可
@Test
void loadShopData() {// 1.查詢店鋪信息List<Shop> list = shopService.list();// 2.把店鋪分組,按照typeId分組,typeId一致的放到一個(gè)集合Map<Long, List<Shop>> map = list.stream().collect(Collectors.groupingBy(Shop::getTypeId));// 3.分批完成寫入Redisfor (Map.Entry<Long, List<Shop>> entry : map.entrySet()) {// 3.1.獲取類型idLong typeId = entry.getKey();String key = SHOP_GEO_KEY + typeId;// 3.2.獲取同類型的店鋪的集合List<Shop> value = entry.getValue();List<RedisGeoCommands.GeoLocation<String>> locations = new ArrayList<>(value.size());// 3.3.寫入redis GEOADD key 經(jīng)度 緯度 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);}
}
實(shí)現(xiàn)附近商戶功能
SpringDataRedis的2.3.9版本并不支持Redis 6.2提供的GEOSEARCH命令,因此我們需要提示其版本,修改自己的POM
<dependency><groupId>org.springframework.boot</groupId><artifactId>spring-boot-starter-data-redis</artifactId><exclusions><exclusion><artifactId>spring-data-redis</artifactId><groupId>org.springframework.data</groupId></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><groupId>io.lettuce</groupId><artifactId>lettuce-core</artifactId><version>6.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.判斷是否需要根據(jù)坐標(biāo)查詢if (x == null || y == null) {// 不需要坐標(biāo)查詢,按數(shù)據(jù)庫查詢Page<Shop> page = query().eq("type_id", typeId).page(new Page<>(current, SystemConstants.DEFAULT_PAGE_SIZE));// 返回?cái)?shù)據(jù)return Result.ok(page.getRecords());}// 2.計(jì)算分頁參數(shù)int from = (current - 1) * SystemConstants.DEFAULT_PAGE_SIZE;int end = current * SystemConstants.DEFAULT_PAGE_SIZE;// 3.查詢r(jià)edis、按照距離排序、分頁。結(jié)果:shopId、distanceString key = SHOP_GEO_KEY + typeId;GeoResults<RedisGeoCommands.GeoLocation<String>> 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());}List<GeoResult<RedisGeoCommands.GeoLocation<String>>> list = results.getContent();if (list.size() <= from) {// 沒有下一頁了,結(jié)束return Result.ok(Collections.emptyList());}// 4.1.截取 from ~ end的部分List<Long> ids = new ArrayList<>(list.size());Map<String, 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.根據(jù)id查詢ShopString idStr = StrUtil.join(",", ids);List<Shop> 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);}