@Test
public void loadShopData(){
//1 查询店铺信息
List<Shop> list = shopservice.list();
//2 把店铺分组 ,按照typeid分组 typeid一致的放到一个集合里面
Map<Long, List<Shop>> map = list.stream().collect(Collectors.groupingBy(Shop::getTypeId));
for (Map.Entry<Long, List<Shop>> entry : map.entrySet()) {
//左边是id,右边是 商铺
Long typeId = entry.getKey();
String key ="shop:geo:"+typeId.toString();
List<Shop> shops = entry.getValue();
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);
}
}
根据typeid进行分类。
知识点:
Map<Long, List<Shop>> map = list.stream().collect(Collectors.groupingBy(Shop::getTypeId));
可以将List类型转为map类型
GeoLocation类型的创建和使用
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()))); }