gpt4 book ai didi

java - ERR未知命令 'GEOADD'来自java spring中的嵌入式redis

转载 作者:IT王子 更新时间:2023-10-29 06:08:52 27 4
gpt4 key购买 nike

我正在 Java Spring Framework 中进行单元测试,其中代码需要使用 GEOADD 操作来过滤距离原点 25 公里以内对齐的位置。

实现工作正常(连接到真正的 redis 节点)但单元测试(连接到嵌入式 redis)显示以下错误

org.springframework.dao.InvalidDataAccessApiUsageException: ERR unknown command 'GEOADD'; nested exception is redis.clients.jedis.exceptions.JedisDataException: ERR unknown command 'GEOADD'

这是我的pom

<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>1.5.9.RELEASE</version>
<relativePath/>
</parent>

...

<dependencies>

...

<dependency>
<groupId>com.github.kstyrc</groupId>
<artifactId>embedded-redis</artifactId>
<version>0.6</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-data-redis</artifactId>
</dependency>
</dependencies>

在junit测试中初始化embedded redis

public class EmbeddedRedisServer {

static RedisServer redisServer = null;

static {
try {
redisServer = new RedisServer(6479);
} catch (IOException e) {
e.printStackTrace();
}
}

public static RedisServer redisServer() throws IOException {
return redisServer;
}

}

测试用例

@BeforeClass
public static void init() throws Exception {
EmbeddedRedisServer.redisServer().start();
System.setProperty("spring.cloud.bootstrap.enabled", "false");
}

@Test
public void testFindNearByPoint() {
for (CurrentGeoLocation currentGeoLocation : getCurrentLocationList()) {
currentGeoLocationService.createOrUpdate(currentGeoLocation);
}
List<CurrentGeoLocation> list = nearByDriverService.findNearByPoint(13.392900, 52.491560, null);
Assert.assertThat(list, hasSize(2));
}

@AfterClass
public static void destroy() throws IOException {
EmbeddedRedisServer.redisServer().stop();
}

需要测试的方法

public List<CurrentGeoLocation> findNearByPoint(final Double longitude, final Double latitude,
final List<Long> driverIds) {
redisTemplate.delete(GeoTrackingConstants.GEO_KEY);
List<CurrentGeoLocation> geoLocationList = new ArrayList<>();
final GeoOperations<String, CurrentGeoLocation> geoOpt = redisTemplate.opsForSet().getOperations().opsForGeo();
List<CurrentGeoLocation> currentGeoLocations =(CollectionUtils.isEmpty(driverIds))? currentGeoLocationService.findAll()
: currentGeoLocationService.findAll(driverIds);
for (CurrentGeoLocation currentGeoLocation : currentGeoLocations) {
Point point = new Point(currentGeoLocation.getLongitude(), currentGeoLocation.getLatitude());
geoOpt.geoAdd(GeoTrackingConstants.GEO_KEY, point, currentGeoLocation);
}
Point originPoint = new Point(longitude, latitude);
Circle circle = new Circle(originPoint, new Distance(radius, RedisGeoCommands.DistanceUnit.KILOMETERS));
final GeoResults<RedisGeoCommands.GeoLocation<CurrentGeoLocation>> result =
(CollectionUtils.isEmpty(driverIds))? geoOpt.geoRadius(GeoTrackingConstants.GEO_KEY, circle)
: geoOpt.geoRadius(GeoTrackingConstants.GEO_KEY, circle, RedisGeoCommands.GeoRadiusCommandArgs.newGeoRadiusArgs().sortAscending());
List<GeoResult<RedisGeoCommands.GeoLocation<CurrentGeoLocation>>> locations = result.getContent();
for (GeoResult<RedisGeoCommands.GeoLocation<CurrentGeoLocation>> location : locations) {
if (location.getContent().getName() != null) {
geoLocationList.add(location.getContent().getName());
}
}
return geoLocationList;
}

有什么建议吗?谢谢

最佳答案

我猜可能是因为你的redis版本太低,只有redis 3.2支持geo操作。

关于java - ERR未知命令 'GEOADD'来自java spring中的嵌入式redis,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/50154806/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com