java - Custom Map如何推送到Redis中存储数据?

标签 java redis

我现在将数据存储在 Map 中并获取字段值而不是 Map 我想将数据存储在 Redis 中。我是 Redis 新手,使用以下代码存储在 Redis 中:

 public class CachingRedis {
private static HashMap<String, UserFields> Cache;
private static JedisPool pool = null;
private static final String redisHost = "localhost";
private static final Integer redisPort = 6379;
static Jedis jedis = null;
static User u;

public CachingRedis() {
    pool = new JedisPool(redisHost, redisPort);
    this.Cache = new HashMap<String, UserFields>();

}

public static void main(String[] args) throws ExecutionException {

    CachingRedis gt = new CachingRedis();
    gt.addSets();

}

private void addSets() {
    InputStream in = ClassLoader.class.getResourceAsStream("/users.csv"); 
    u = new User(in);
    String[] consideredUserFields = { "Area","FirstName","LastName","Contact","Level"};
    List<String[]> users = p.getUsers();
    jedis = pool.getResource();
    int count1 = 0;
    String token = null;
    String fieldName = null;
    String fieldVal = null;

    for (int i = 0; i < users.size(); i++) {
        UserFields uFields = new UserFields();
        String tmpId = Integer.toString(p.getUserId(i));
        String[] tmpFields = Users.get(i);
        for (int j = 0; j < tmpFields.length; j++) {

            fieldName = consideredUsersFields[j];
            fieldVal = tmpFields[j];

            if (Cache != null && Cache.containsKey(tmpId)) {
                uFields = Cache.get(tmpId);
                uFields.setFieldKeyValues(fieldName, fieldVal);
                Cache.put(tmpId, uFields);
                 **jedis.hsetnx( tmpId,fieldName,fieldVal );**
            } else {
                uFields.setFieldKeyValues(fieldName, fieldVal);

                Cache.put(tmpId, pFields);
                **jedis.hsetnx( tmpId,fieldName,fieldVal );**
            }

        }
    }

    }  }

我收到以下错误

Exception in thread "main" redis.clients.jedis.exceptions.JedisDataException: WRONGTYPE Operation against a key holding the wrong kind of       value
at redis.clients.jedis.Protocol.processError(Protocol.java:117)
at redis.clients.jedis.Protocol.process(Protocol.java:142)
at redis.clients.jedis.Protocol.read(Protocol.java:196)
at redis.clients.jedis.Connection.readProtocolWithCheckingBroken(Connection.java:288)
at redis.clients.jedis.Connection.getIntegerReply(Connection.java:213)
at redis.clients.jedis.Jedis.lpush(Jedis.java:814)
at net.cintell.CachingRedis.addSets(CachingRedis.java:85)
at net.cintell.CachingRedis.main(CachingRedis.java:48)

谁能告诉我哪里做错了?我想将 csv 文件存储到 redis 中并获取相应的字段?

最佳答案

         public class CachingRedis {
     private static HashMap<String, UserFields> Cache;
     private static JedisPool pool = null;
     private static final String redisHost = "localhost";
     private static final Integer redisPort = 6379;
     static Jedis jedis = null;
     static User u;

    public CachingRedis() {
    pool = new JedisPool(redisHost, redisPort);
    this.Cache = new HashMap<String, UserFields>();

   }

   public static void main(String[] args) throws ExecutionException {

    CachingRedis gt = new CachingRedis();
    gt.addSets();

   }

    private void addSets() {
    InputStream in = ClassLoader.class.getResourceAsStream("/users.csv"); 
    u = new User(in);
      String[] consideredUserFields = {     "Area","FirstName","LastName","Contact","Level"};
    List<String[]> users = p.getUsers();
    jedis = pool.getResource();
    int count1 = 0;
    String token = null;
    String fieldName = null;
    String fieldVal = null;

    for (int i = 0; i < users.size(); i++) {
    UserFields uFields = new UserFields();
    String tmpId = Integer.toString(p.getUserId(i));
    String[] tmpFields = Users.get(i);
    for (int j = 0; j < tmpFields.length; j++) {

        fieldName = consideredUsersFields[j];
        fieldVal = tmpFields[j];

        if (Cache != null && Cache.containsKey(tmpId)) {
            uFields = Cache.get(tmpId);
            uFields.setFieldKeyValues(fieldName, fieldVal);
            Cache.put(tmpId, uFields);

        } else {
            uFields.setFieldKeyValues(fieldName, fieldVal);

            Cache.put(tmpId, pFields);

        }

    }
    }
      Map<String, String> Properties = new HashMap<String, String>();
    for (Map.Entry<String, PersonaFields> entry : Cache.entrySet()) {

            Properties.put("Id", entry.getKey());
        Properties.put("Area", entry.getValue()
            //rest of the fields
       jedis.hmset("Users"+ entry.getKey(), Properties);
}
    }  }

我通过将每个键值加载到其他映射中,将整个缓存映射加载到 Redis 中,这样我就可以基于相同的键值进行检索 来自 Redis

关于java - Custom Map如何推送到Redis中存储数据?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/36699820/

相关文章:

redis - redisGetReply 超时

sockets - 如何将套接字连接更改为超时而不是完全关闭?

node.js - 从未使用redis在child_process.exec中调用回调

java - 当我调用 selenium.waitForPageToLoad 时出现 Internet Explorer 脚本错误

java - 如果不存在记录,如何创建一个删除表的事件和另一个事件

java - 如何指示 yum 安装特定版本的 OpenJDK

java - 尝试从 DBpedia 上传 RDF 文件时 Sesame 2.7 中的 ParserConfig 错误

java - Redisson新手(Redis java客户端)类型io.netty.util.concurrent.Future无法解析

java - JSF 项目如何从其依赖项之一显示 xhtml 文件

Redis - 排序集,按属性值查找项目