【问题标题】:How does Lettuce dynamic command interface work with Redis modules?Lettuce 动态命令接口如何与 Redis 模块配合使用?
【发布时间】:2019-11-04 02:35:10
【问题描述】:

尝试通过 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 命令。

代码sn-p:

    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)

【问题讨论】:

    标签: redis lettuce redistimeseries


    【解决方案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

    【讨论】:

      猜你喜欢
      • 2019-01-22
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2020-02-01
      • 2018-06-01
      • 2020-01-21
      • 2014-05-12
      • 2023-03-21
      相关资源
      最近更新 更多