How can I store nested Hashmap in Redis?
Asked Answered
A

4

7

I want to store netsted HashMap in Redis having single key.

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?
Algy answered 10/9, 2018 at 10:39 Comment(1)
If you have the same key and multiple key-value, you also can use HashOperations of RedisTemplate like: redisTemplate.opsForHash().put(key, hashKey, value); Check this for more details: docs.spring.io/spring-data/data-keyvalue/docs/current/api/org/…Malo
M
5

Redis doesn't support it as of now. However there is a way to do it, other than rejson.

You can convert it into JSON and store in Redis and retrieve. Following utility methods, which I use in Jackson.

To convert Object to 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);

To convert String to 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>>(){})

You can update this method as per your requirement. I am sure, you know, how to add and update in Redis.

Myronmyrrh answered 10/9, 2018 at 13:16 Comment(0)
F
4

REDIS now allows nested 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;
    }

}
Fungous answered 23/7, 2020 at 23:23 Comment(1)
Redis does not support nested HashMap. Its the library that is doing all the magic I perassume.Hui
T
3

Redis doesn't support storing hash inside hash. 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. I think this can help you to store your data.

Titulary answered 10/9, 2018 at 11:59 Comment(0)
D
0

According to my recent research Redis does'nt support nested hasmap. So on my own solution I used ObjectMapper to convert nestedHasmap as Json string for storing in Redis.

Diffractive answered 26/9, 2021 at 10:28 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.