|
| 1 | +package org.cp.javaredis; |
| 2 | + |
| 3 | +import java.util.concurrent.ExecutionException; |
| 4 | +import org.junit.jupiter.api.Test; |
| 5 | +import org.redisson.api.GeoEntry; |
| 6 | +import org.redisson.api.GeoUnit; |
| 7 | +import org.redisson.api.RGeo; |
| 8 | +import org.redisson.api.RedissonClient; |
| 9 | +import org.springframework.beans.factory.annotation.Autowired; |
| 10 | +import org.springframework.boot.test.context.SpringBootTest; |
| 11 | + |
| 12 | +/** |
| 13 | + * 在一个地图应用中,车的数据、餐馆的数据、人的数据可能会有百万千万条,如果使用 |
| 14 | + * Redis 的 Geo 数据结构,它们将全部放在一个 zset 集合中。在 Redis 的集群环境中,集合 |
| 15 | + * 可能会从一个节点迁移到另一个节点,如果单个 key 的数据过大,会对集群的迁移工作造成 |
| 16 | + * 较大的影响,在集群环境中单个 key 对应的数据量不宜超过 1M,否则会导致集群迁移出现 |
| 17 | + * 卡顿现象,影响线上服务的正常运行。 |
| 18 | + * 所以,这里建议 Geo 的数据使用单独的 Redis 实例部署,不使用集群环境。 |
| 19 | + * 如果数据量过亿甚至更大,就需要对 Geo 数据进行拆分,按国家拆分、按省拆分,按 |
| 20 | + * 市拆分,在人口特大城市甚至可以按区拆分。这样就可以显著降低单个 zset 集合的大小。 |
| 21 | + */ |
| 22 | +@SpringBootTest |
| 23 | +public class GeoHashTest { |
| 24 | + |
| 25 | + @Autowired |
| 26 | + RedissonClient redissonClient; |
| 27 | + |
| 28 | + @Test |
| 29 | + public void test() throws ExecutionException, InterruptedException { |
| 30 | + RGeo<Object> geo = redissonClient.getGeo("geo"); |
| 31 | + GeoEntry juejin = new GeoEntry(116.48105, 39.996794, "juejin"); |
| 32 | + geo.add(juejin); |
| 33 | + geo.add(116.514203, 39.905409, "ireader"); |
| 34 | + geo.add(116.489033, 40.007669, "meituan"); |
| 35 | + geo.add(116.562108, 39.787602, "jd"); |
| 36 | + geo.add(116.334255, 40.027400, "xiaomi"); |
| 37 | + geo.add(1.2, "test"); |
| 38 | + |
| 39 | + // 两点距离 |
| 40 | + System.out.println(geo.dist("meituan", "jd", GeoUnit.METERS)); |
| 41 | + // 坐标 |
| 42 | + System.out.println(geo.pos("xiaomi")); |
| 43 | + // hash |
| 44 | + System.out.println(geo.hash("ireader")); |
| 45 | + // 附近 |
| 46 | + System.out.println(geo.radiusWithPositionAsync("jd", 20, GeoUnit.KILOMETERS).get()); |
| 47 | + // 附近 |
| 48 | + System.out.println( |
| 49 | + geo.radiusWithPositionAsync(116.334255, 40.027400, 20, GeoUnit.KILOMETERS).get()); |
| 50 | + } |
| 51 | + |
| 52 | +} |
0 commit comments