Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How can I store nested Hashmap in Redis?

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?
like image 759
Minal Avatar asked Sep 10 '18 10:09

Minal


People also ask

How are HashMaps stored?

HashMap stores entries into multiple singly linked lists, called buckets or bins. Default number of bins is 16 and it's always power of 2. HashMap uses hashCode() and equals() methods on keys for get and put operations. So HashMap key object should provide good implementation of these methods.

What is nested HashMap?

A nested HashMap is Map inside a Map. For HashMap , the key or the value can be of any type (object). For Nested HashMap , the key can be of any type (object), but the value is another HashMap object only.

Can Redis list store objects?

Object Storing Procedure. In the Redis, Everything can be stored as only key-value pair format. Key must be unique and storing an object in a string format is not a good practice anyway. Objects are usually stored in a binary array format in the databases.


3 Answers

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;
    }

}
like image 38
Deepak Rajan Avatar answered Oct 10 '22 22:10

Deepak Rajan


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.

like image 36
mastisa Avatar answered Oct 10 '22 23:10

mastisa


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.

like image 167
Darshan Patel Avatar answered Oct 10 '22 22:10

Darshan Patel