简体   繁体   English

如何在Redis中存储嵌套的Hashmap?

[英]How can I store nested Hashmap in Redis?

I want to store netsted HashMap in Redis having single key.我想将 netsted HashMap存储在具有单个键的Redis中。

For example :例如 :

HashMap<String, HashMap<String,String>> map = new  HashMap<>();

Please Suggest :请建议:

  • Is there any way to store the above-mentioned data structure?有没有办法存储上述数据结构?
  • How can we achieve this?我们怎样才能做到这一点?

Redis doesn't support it as of now. Redis 目前不支持它。 However there is a way to do it, other than rejson .但是,除了rejson ,还有一种方法可以做到这rejson

You can convert it into JSON and store in Redis and retrieve.您可以将其转换为 JSON 并存储在 Redis 中并检索。 Following utility methods, which I use in Jackson.遵循我在 Jackson 中使用的实用方法。

To convert Object to String :将 Object 转换为 String :

public static String stringify(Object object) {
    ObjectMapper jackson = new ObjectMapper();
    jackson.setSerializationInclusion(JsonSerialize.Inclusion.NON_NULL);
    try {
        return jackson.writeValueAsString(object);
    } catch (Exception ex) {
        LOG.log(Level.SEVERE, "Error while creating json: ", ex);
    }
    return null;
}

Example : stringify(obj);示例: stringify(obj);

To convert String to Object :将 String 转换为 Object :

public static <T> T objectify(String content, TypeReference valueType) {
    try {
        ObjectMapper mapper = new ObjectMapper();
        mapper.configure(Feature.WRITE_DATES_AS_TIMESTAMPS, false);
        DateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd'T'hh:mm:ss.SSS");
        dateFormat.setTimeZone(Calendar.getInstance().getTimeZone());
        mapper.setDateFormat(dateFormat);
        return mapper.readValue(content, valueType);
    } catch (Exception e) {
        LOG.log(Level.WARNING, "returning null because of error : {0}", e.getMessage());
        return null;
    }
}

Example : List<Object> list = objectify("Your Json", new TypeReference<List<Object>>(){})示例: List<Object> list = objectify("Your Json", new TypeReference<List<Object>>(){})

You can update this method as per your requirement.您可以根据需要更新此方法。 I am sure, you know, how to add and update in Redis.我确定,您知道如何在 Redis 中添加和更新。

Redis doesn't support storing hash inside hash. Redis 不支持在哈希中存储哈希。 But there is REDIS as a JSON store that can store JSON in REDIS, It allows storing, updating and fetching JSON values from Redis keys.但是有REDIS 作为 JSON 存储,可以在 REDIS 中存储 JSON,它允许从 Redis 键存储、更新和获取 JSON 值。 I think this can help you to store your data.我认为这可以帮助您存储数据。

REDIS now allows nested HashMap https://redis.io/topics/data-types REDIS 现在允许嵌套 HashMap https://redis.io/topics/data-types

<dependency>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-starter-data-redis</artifactId>
    <version>1.5.22.RELEASE</version>
</dependency>
public class Redis {

    @Autowired
    RedisService redisService;

    @Cacheable(value = "name", key = "keyvalue")
    public Map<String, HashMap<String, String>> redisNestedMap(String keyvalue) {
        return redisService.getNestedMap();
    }

}
@Component
public class RedisService {

    public Map<String, HashMap<String, String>> getNestedMap() {
        Map<String, HashMap<String, String>> nestedMap = new HashMap<>();
        HashMap<String, String> value = new HashMap<>();
        value.put("key", "value");
        nestedMap.put("one", value);
        return nestedMap;
    }

}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM