javaspring-bootredisspring-data-redisredistimeseries

How to use RedisTimeSeriesCommands with jedis in Java Spring Boot


I am currently utilizing jedis version 4.3.0 in my Java Spring Boot project. I am interested in utilizing the Redis time series commands interface to store time series data in Redis. However, I am unsure whether I can directly employ Redis time series commands to interact with the time series data without the need for additional dependencies such as JRedisTimeSeries. If direct utilization is possible, how should I establish a connection and implement methods from that interface?

I have tried to create bean for RedisTimeSeriesCommands

@Bean
    public RedisTimeSeriesCommands redisTimeSeriesCommands() {
        return new RedisTimeSeriesCommands() {
            @Override
            public String tsCreate(String key) {
                return null;
            }

            @Override
            public String tsCreate(String key, TSCreateParams createParams) {
                return null;
            }

            @Override
            public long tsDel(String key, long fromTimestamp, long toTimestamp) {
                return 0;
            }

            @Override
            public String tsAlter(String key, TSAlterParams alterParams) {
                return null;
            }

            @Override
            public long tsAdd(String key, double value) {
                return 0;
            }

            @Override
            public long tsAdd(String key, long timestamp, double value) {
                return 0;
            }

            @Override
            public long tsAdd(String key, long timestamp, double value, TSCreateParams createParams) {
                return 0;
            }

            @Override
            public List<Long> tsMAdd(Map.Entry<String, TSElement>... entries) {
                return null;
            }

            @Override
            public long tsIncrBy(String key, double value) {
                return 0;
            }

            @Override
            public long tsIncrBy(String key, double value, long timestamp) {
                return 0;
            }

            @Override
            public long tsDecrBy(String key, double value) {
                return 0;
            }

            @Override
            public long tsDecrBy(String key, double value, long timestamp) {
                return 0;
            }

            @Override
            public List<TSElement> tsRange(String key, long fromTimestamp, long toTimestamp) {
                return null;
            }

            @Override
            public List<TSElement> tsRange(String key, TSRangeParams rangeParams) {
                return null;
            }

            @Override
            public List<TSElement> tsRevRange(String key, long fromTimestamp, long toTimestamp) {
                return null;
            }

            @Override
            public List<TSElement> tsRevRange(String key, TSRangeParams rangeParams) {
                return null;
            }

            @Override
            public List<TSKeyedElements> tsMRange(long fromTimestamp, long toTimestamp, String... filters) {
                return null;
            }

            @Override
            public List<TSKeyedElements> tsMRange(TSMRangeParams multiRangeParams) {
                return null;
            }

            @Override
            public List<TSKeyedElements> tsMRevRange(long fromTimestamp, long toTimestamp, String... filters) {
                return null;
            }

            @Override
            public List<TSKeyedElements> tsMRevRange(TSMRangeParams multiRangeParams) {
                return null;
            }

            @Override
            public TSElement tsGet(String key) {
                return null;
            }

            @Override
            public TSElement tsGet(String key, TSGetParams getParams) {
                return null;
            }

            @Override
            public List<TSKeyValue<TSElement>> tsMGet(TSMGetParams multiGetParams, String... filters) {
                return null;
            }

            @Override
            public String tsCreateRule(String sourceKey, String destKey, AggregationType aggregationType, long timeBucket) {
                return null;
            }

            @Override
            public String tsCreateRule(String sourceKey, String destKey, AggregationType aggregationType, long bucketDuration, long alignTimestamp) {
                return null;
            }

            @Override
            public String tsDeleteRule(String sourceKey, String destKey) {
                return null;
            }

            @Override
            public List<String> tsQueryIndex(String... filters) {
                return null;
            }

            @Override
            public TSInfo tsInfo(String key) {
                return null;
            }

            @Override
            public TSInfo tsInfoDebug(String key) {
                return null;
            }
        }
    } 

But I dont know how to implement methods.


Solution

  • In your implementation you to inject/expose the right Jedis client to use the commands in https://github.com/redis/jedis/blob/master/src/main/java/redis/clients/jedis/timeseries/RedisTimeSeriesCommands.java which you can get via UnifiedJedis

    At the top level you probably need a RedisConnectionFactory of type JedisConnectionFactory, something like:

    @Bean
    public JedisConnectionFactory jedisConnectionFactory() {
      return new JedisConnectionFactory();
    }
    
    @Bean
    public UnifiedJedis unifiedJedis(RedisConnectionFactory factory) {
      // Assumes factory is JedisConnectionFactory
      JedisConnectionFactory jedisFactory = (JedisConnectionFactory) factory;
    
      // Configure as needed
      JedisPoolConfig poolConfig = new JedisPoolConfig();
      // Set pool config as needed
    
      // Return the UnifiedJedis instance
      return new UnifiedJedis(jedisFactory.getHostName(), jedisFactory.getPort(), poolConfig)
    }
    
    @Bean
    public RedisTemplate<?, ?> redisTemplate(RedisConnectionFactory factory) {
      RedisTemplate<byte[], byte[]> template = new RedisTemplate<>();
      template.setConnectionFactory(factory);
      return template;
    }
    

    Then wire the UnifiedJedis into your Bean and use the TS methods exposed:

    unifiedJedis.tsCreate("myTSKey");