You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

Java读取Redis ReJSON-RL数据遇WRONGTYPE错误求助

Redis集群中ReJSON-RL类型Key的查询解决方案

问题背景

已成功建立Redis集群连接,但查询Key 1006110582时触发WRONGTYPE错误——该Key类型为ReJSON-RL,使用标准get命令无法读取;此前尝试command.json.get也报错,需要正确的查询方法。

原代码

import io.lettuce.core.RedisClient;
import io.lettuce.core.RedisURI;
import io.lettuce.core.api.StatefulRedisConnection;
import io.lettuce.core.api.sync.RedisCommands;
import io.lettuce.core.cluster.ClusterClientOptions;
import io.lettuce.core.cluster.ClusterTopologyRefreshOptions;
import io.lettuce.core.cluster.RedisClusterClient;
import io.lettuce.core.cluster.api.StatefulRedisClusterConnection;
import io.lettuce.core.cluster.api.sync.RedisAdvancedClusterCommands;
import io.lettuce.core.cluster.api.sync.RedisClusterCommands;
import io.lettuce.core.resource.ClientResources;
import io.lettuce.core.resource.DefaultClientResources;
import org.json.JSONObject;
import io.lettuce.core.cluster.ClusterTopologyRefreshOptions.RefreshTrigger;
import java.time.Duration; 
public class redissClass {
     public static void main(String[] args) {
         String name = "name";
            String password = "password";
            String host="host.com";
            String query="1006110582";
            ClientResources clientResources = DefaultClientResources.create();
            RedisClusterClient clusterClient = RedisClusterClient.create(clientResources, RedisURI.builder().redis(host,6379)
                    .withSsl(true)
                    .withAuthentication(name,password)
                    .withVerifyPeer(false).build());
            clusterClient.setOptions(ClusterClientOptions.builder()
                    .topologyRefreshOptions(ClusterTopologyRefreshOptions.builder()
                            .enablePeriodicRefresh(Duration.ofSeconds(5))
                            .build())
                    .build());
            StatefulRedisClusterConnection<String, String> connection = clusterClient.connect();
            RedisClusterCommands<String, String> syncCommands = connection.sync();
            String response = syncCommands.ping();
            System.out.println("Redis response: " + response);
            System.out.println("type"+syncCommands.type(query));
            String jsonString=syncCommands.get(query);
            JSONObject jsonObject = new JSONObject(jsonString);
            String euid=jsonObject.getString("euid");
            System.out.println("euid"+euid);
            connection.close();
            clientResources.shutdown();
        } }

错误信息

Redis response: PONG typeReJSON-RL Exception in thread "main" io.lettuce.core.RedisCommandExecutionException: WRONGTYPE Operation against a key holding the wrong kind of value    
at io.lettuce.core.internal.ExceptionFactory.createExecutionException(ExceptionFactory.java:137)    
at io.lettuce.core.internal.Exceptions.bubble(Exceptions.java:72)  
at io.lettuce.core.internal.Futures.awaitOrCancel(Futures.java:250)
at io.lettuce.core.cluster.ClusterFutureSyncInvocationHandler.handleInvocation(ClusterFutureSyncInvocationHandler.java:130)    
at io.lettuce.core.internal.AbstractInvocationHandler.invoke(AbstractInvocationHandler.java:80)    
at jdk.proxy1/jdk.proxy1.$Proxy23.get(Unknown Source)    
at redissClass.main(redissClass.java:44)

解决方案

1. 添加ReJSON依赖

Lettuce核心库不支持RedisJSON模块命令,需引入专用扩展包。以Maven为例:

<dependency>
    <groupId>io.lettuce</groupId>
    <artifactId>lettuce-core</artifactId>
    <version>6.3.2.RELEASE</version> <!-- 使用最新稳定版 -->
</dependency>
<dependency>
    <groupId>com.redislabs</groupId>
    <artifactId>lettusearch</artifactId>
    <version>4.6.0</version> <!-- 包含ReJSON命令支持 -->
</dependency>

2. 修改代码使用JSON.GET命令

替换原代码中的查询逻辑,改用RedisJSON模块的JSON.GET命令:

方式一:通过高级命令接口执行自定义命令

// 替换RedisClusterCommands为RedisAdvancedClusterCommands
RedisAdvancedClusterCommands<String, String> syncCommands = connection.sync();

// 执行JSON.GET获取完整JSON字符串
String jsonString = syncCommands.command("JSON.GET", query);

// 处理返回结果(JSON.GET返回的字符串带外层引号,需去除)
if (jsonString != null) {
    jsonString = jsonString.replaceFirst("^\"", "").replaceFirst("\"$", "");
    JSONObject jsonObject = new JSONObject(jsonString);
    String euid = jsonObject.getString("euid");
    System.out.println("euid: " + euid);
}

方式二:使用ReJSON专用API

// 引入ReJSON相关类
import com.redislabs.lettusearch.ReJSONCommands;
import com.redislabs.lettusearch.RedisReJSONCommands;

// 获取ReJSON命令实例
RedisReJSONCommands<String, String> jsonCommands = connection.sync().getInstance(ReJSONCommands.class);

// 直接获取完整JSON
String jsonString = jsonCommands.get(query);
// 或者直接指定路径获取单个字段,无需解析整个JSON
String euid = jsonCommands.get(query, ".euid");
System.out.println("euid: " + euid);

3. 关键说明

  • WRONGTYPE错误的原因:get命令仅适用于Redis原生String类型Key,而ReJSON-RL是RedisJSON模块定义的特殊类型,必须使用模块提供的JSON.GET命令查询。
  • 此前尝试的command.json.get写法错误,正确的Redis命令格式是JSON.GET key [路径]。

内容的提问来源于stack exchange,提问作者samik sengupta

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.07.30 01:59:58