Redis学习(五):GEO数据结构的应用
1. 基本命令
- GEOADD
127.0.0.1:6379> GEOADD university 118.095049 24.579985 "jmu" 118.304729 24.608969 "hqu" 118.083051 24.608003 "xmu"
(integer) 3
127.0.0.1:6379> GEOPOS university jmu
1) 1) "118.09504777193069458"
2) "24.57998571719326009"
- GEOPOS
127.0.0.1:6379> GEODIST university jmu xmu
"3343.9496"
127.0.0.1:6379> GEODIST university jmu xmu km
"3.3439"
- GEOSEARCH
127.0.0.1:6379> GEOSEARCH university FROMMEMBER jmu BYRADIUS 10 KM ASC WITHDIST
1) 1) "jmu"
2) "0.0000"
2) 1) "xmu"
2) "3.3439"
127.0.0.1:6379> GEOSEARCH university FROMMEMBER jmu BYRADIUS 10 KM DESC
1) "xmu"
2) "jmu"
2. 以导入店铺数据为例
分析:
- 以店铺类型为key
- 以店铺id作为member
@Test
void loadShopLocData() {
// 1. 查询店铺数据
List<Shop> shopList = shopService.list();
// 2. 按照typeId对店铺进行分组
Map<Long, List<Shop>> shopMap = shopList.stream().collect(Collectors.groupingBy(Shop::getTypeId));
// 3. 分批导入到redis
for (Map.Entry<Long, List<Shop>> entry : shopMap.entrySet()) {
// 3.1 获取typeId
Long typeId = entry.getKey();
String key = SHOP_GEO_KEY + typeId;
// 3.2 获取同类型的店铺集合
List<Shop> shops = entry.getValue();
/* 插入多条时,效率不高
for (Shop shop : shops) { stringRedisTemplate.opsForGeo().add(key, new Point(shop.getX(), shop.getY()), shop.getId().toString()); } */
List<RedisGeoCommands.GeoLocation<String>> locations = new ArrayList<>(shops.size());
for (Shop shop : shops) {
locations.add(new RedisGeoCommands.GeoLocation<>(
shop.getId().toString(),
new Point(shop.getX(), shop.getY())
));
}
stringRedisTemplate.opsForGeo().add(key, locations);
}
}
3. 实现附近商户功能
@Override
public Result queryShopByType(Integer typeId, Integer current, Double x, Double y) {
// 1. 判断是否需要进行坐标查询
if (x == null || y == null) {
// 不需要进行坐标查询,直接进行数据库分页查询
Page<Shop> 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,按照距离排序,分页
String key = SHOP_GEO_KEY + typeId;
GeoResults<RedisGeoCommands.GeoLocation<String>> results = stringRedisTemplate.opsForGeo()
.search(key,
GeoReference.fromCoordinate(x, y),
new Distance(5000),
RedisGeoCommands.GeoSearchCommandArgs.newGeoSearchArgs().includeDistance().limit(end));
// 4. 解析id
// 4.1 判断查询出的结果是否为空
if (results == null) {
return Result.ok(Collections.emptyList());
}
// 4.2 从结果中取出数据,并判断是否已经是最后一页
List<GeoResult<RedisGeoCommands.GeoLocation<String>>> shopList = results.getContent();
if (shopList.size() <= from) {
return Result.ok(Collections.emptyList());
}
// 4.3 截取from ~ end的部分
ArrayList<Long> idList = new ArrayList<>(list().size());
HashMap<String, Distance> distanceMap = new HashMap<>(list().size());
shopList.stream().skip(from).forEach(result -> {
// 4.4 获取店铺id
String shopIdStr = result.getContent().getName();
idList.add(Long.valueOf(shopIdStr));
// 4.5 获取距离
Distance distance = result.getDistance();
distanceMap.put(shopIdStr, distance);
});
// 5. 根据id查询shop
String idListStr = StrUtil.join(",", idList);
List<Shop> shops = query().in("id", idList).last("order by field(" +
idListStr + ")").list();
for (Shop shop : shops) {
shop.setDistance(distanceMap.get(shop.getId().toString()).getValue());
}
// 6. 返回
return Result.ok(shops);
}
本文是原创文章,转载请注明来自 Lazyking.site
评论
匿名评论
隐私政策
你无需删除空行,直接评论以获取最佳展示效果
Steam卡片