0%

Redis-GEO的Java中使用

配置redis

1
2
3
4
5
<dependency>
<groupId>redis.clients</groupId>
<artifactId>jedis</artifactId>
<version>2.9.0</version>
</dependency>
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
<bean id="stringRedisSerializer" class="org.springframework.data.redis.serializer.StringRedisSerializer"/>
<bean id="jsonRedisSerializer" class="org.springframework.data.redis.serializer.GenericJackson2JsonRedisSerializer"/>

系统内进行使用,hash key使用generic,value使用jdk进行序列化

<!-- redis template definition -->

<bean id="redisTemplate"
class="org.springframework.data.redis.core.RedisTemplate" primary="true">
<property name="connectionFactory" ref="jedisConnFactory"/>
<property name="keySerializer" ref="stringRedisSerializer"/>
<property name="hashKeySerializer" ref="jsonRedisSerializer"/>
</bean>

<bean id="statisticsRedisTemplate" class="org.springframework.data.redis.core.RedisTemplate">
<property name="connectionFactory" ref="jedisConnFactory"/>
<property name="keySerializer" ref="stringRedisSerializer"/>
<property name="valueSerializer" ref="stringRedisSerializer"/>
</bean>

<bean id="redisTemplateSecond" class="org.springframework.data.redis.core.RedisTemplate">
<property name="connectionFactory" ref="jedisConnFactory"/>
<property name="keySerializer" ref="stringRedisSerializer"/>
<property name="valueSerializer" ref="jsonRedisSerializer"/>
</bean>

添加坐标

key ,经度 , 维度 , 距离 ,return m 表示单位为米

1
2
3
4
public Long addReo(String key, double longitude, double latitude, String itemName) {
Point point = new Point(longitude, latitude);
return redisTemplate.getOperations().opsForGeo().geoAdd(key, point, itemName);
}

批量添加地理位置

1
2
3
public Long geoadd(String key, Map<Object, Point> memberCoordinateMap) {
return redisTemplate.getOperations().opsForGeo().geoAdd(key, memberCoordinateMap);
}

根据给定地理位置获取指定范围内的地理位置集合

返回匹配位置的经纬度 + 匹配位置与给定地理位置的距离 + 从近到远排序

1
2
3
public List<GeoRadiusResponse> georadiusByMember(String key, String member, double radius) {
return (List<GeoRadiusResponse>) redisTemplate.getOperations().opsForGeo().geoRadiusByMember(key, member, radius);
}

根据给定地理位置坐标获取指定范围内的地理位置集合

1
2
3
4
5
6
public GeoResults geoRadius(String key, double longitude, double latitude, double radius) {
Point point = new Point(longitude, latitude);
Distance distance = new Distance(radius, Metrics.KILOMETERS);
Circle circle = new Circle(point, distance);
return redisTemplate.getOperations().opsForGeo().geoRadius(key, circle);
}

根据给定地理位置坐标获取指定范围内的地理位置部分集合

1
2
3
4
5
6
7
public GeoResults geoRadiusArgs(String key, double longitude, double latitude, double radius, int limit) {
Point point = new Point(longitude, latitude);
Distance distance = new Distance(radius, RedisGeoCommands.DistanceUnit.KILOMETERS);
Circle circle = new Circle(point, distance);
RedisGeoCommands.GeoRadiusCommandArgs args= RedisGeoCommands.GeoRadiusCommandArgs.newGeoRadiusArgs().includeDistance().includeCoordinates().limit(limit).sortAscending();
return redisTemplate.getOperations().opsForGeo().geoRadius(key, circle, args);
}

查询两位置距离

1
2
3
public Double geoDist(String key, String member1, String member2, Metrics unit) {
return redisTemplate.getOperations().opsForGeo().geoDist(key, member1, member2, unit).getValue();
}

获取地理位置的坐标

1
2
3
public List<GeoCoordinate> geopos(String key, String... members) {
return redisTemplate.getOperations().opsForGeo().geoPos(key, members);
}

可以获取某个地理位置的geohash值

1
2
3
public List<String> geohash(String key, String... members) {
return redisTemplate.getOperations().opsForGeo().geoHash(key, members);
}