0

尝试通过 Lettuce (Java) 向 Redis 发送 RedisTimeSeries 命令。它适用于简单的命令,例如 TS.Create,但我无法使用稍微复杂的命令(例如 TS.ADD,它将键、分数、值作为 args)或 TS.Range(它采用 args 并返回 List)工作。

Redis 在 Linux 上运行(Ubuntu 通过 WSL 在 Windows 10 上运行),RedisTimeSeries 安装在 Redis 上。Redis 和 RedisTimeSeries 命令已在 Linux 上使用 Redis-cli 进行了测试,它们工作正常。我使用 VS Code + JDK 13.0 + Maven 为 Redis 构建和测试 Java 客户端。到目前为止,Lettuce 支持的 Redis 命令通过客户端运行,以及一些简单的 RedisTimeSeries 命令。

代码片段:

    RedisCommands<String, String> syncCommands = MyRedisClient.getSyncCommands(connection);

    // this works:
    RedisCodec<String, String> codec = StringCodec.UTF8;
    String result = syncCommands.dispatch(TS.CREATE, new StatusOutput<>(codec),new CommandArgs<>(codec).addKey("myTS"));    
    System.out.println("Custom Command TS.CREATE " + result);

    //custom command definition:
    public enum TS implements ProtocolKeyword{
        CREATE;
        public final byte[] bytes;
        private TS() {
            bytes = "TS.CREATE".getBytes(StandardCharsets.US_ASCII);
        }
        @Override
        public byte[] getBytes() {
            return bytes;
        }   
    }

但是当我将所有东西都切换到测试 TS.ADD 时,即使我相应地添加了额外的参数,它也不起作用。例如

    String result = syncCommands.dispatch(TS.ADD, new StatusOutput<>(codec),new CommandArgs<>(codec).addKey("myTS").addValue("1000001").addValue("2.199")); 

这是运行的异常:

    Exception in thread "main" io.lettuce.core.RedisException: java.lang.IllegalStateException
        at io.lettuce.core.LettuceFutures.awaitOrCancel(LettuceFutures.java:129)
        at io.lettuce.core.FutureSyncInvocationHandler.handleInvocation(FutureSyncInvocationHandler.java:69)
        at io.lettuce.core.internal.AbstractInvocationHandler.invoke(AbstractInvocationHandler.java:80)
        at com.sun.proxy.$Proxy0.dispatch(Unknown Source)
        at MyRedisClient.main(MyRedisClient.java:72)
4

1 回答 1

1

抱歉这么晚才看到。如果您还没有找到解决方案,我最初使用动态命令实现了 RediSearch 命令。

public interface RediSearchCommands extends Commands {

    @Command("FT.SUGADD ?0 ?1 ?2")
    Long sugadd(String key, String string, double score);
 
    ...
}

public void testSuggestions() {
    RedisCommandFactory factory = new RedisCommandFactory(client.connect());
    RediSearchCommands commands = factory.getCommands(RediSearchCommands.class);
    commands.sugadd(key, "Herbie Hancock", 1.0);
}

完整来源:https ://github.com/RediSearch/lettusearch/commit/567de42c147e0f07184df444cd1ae9798ae2514e

于 2020-07-05T23:47:24.350 回答