How to store a complex class object in hbase .......

If we can store a class object in hbase and retrieve it back, then we can easily resolve the problem of persistant storage of variables. As a result we don't need the object to be reinitialized again. Pick it it from the previous saved instant and start using it. And again save it back for future use with latest updates.

This is very useful when we are using a ArrayList or Hashmap. which has to be populated again and again every time your program is intialized for the same set of values. You can save the List as it is and pick it from Hbase/HDFS. As a result, you will be getting a faster performance. 

So here we are basically talking about serialization and deserialization column value of hbase/HDFS.

So lets start with code part.

public static String serializeObjectToString(Object object) throws Exception
{
        ByteArrayOutputStream arrayOutputStream = new ByteArrayOutputStream();
        GZIPOutputStream gzipOutputStream = new GZIPOutputStream(arrayOutputStream);
        ObjectOutputStream objectOutputStream = new ObjectOutputStream(gzipOutputStream);
        objectOutputStream.writeObject(object);
        objectOutputStream.flush();
        gzipOutputStream.close();
        arrayOutputStream.close();
        objectOutputStream.close();
        String objectString = new String(base64.encode(arrayOutputStream.toByteArray()));
        return objectString;
}


This function basically takes any object and converts them to serializaed string.


public static Object deserializeObjectFromString(String objectString) throws Exception
{
 ByteArrayInputStream arrayInputStream = new ByteArrayInputStream(base64.decode(objectString));
    GZIPInputStream gzipInputStream = new GZIPInputStream(arrayInputStream);
    ObjectInputStream objectInputStream = new ObjectInputStream(gzipInputStream);
    Object object = objectInputStream.readObject();
    objectInputStream.close();
    gzipInputStream.close();
    arrayInputStream.close();
    return object;

}


This function basically reads the serialized string and returns as object. That object can be used to type cast to the corresponding class, for further use.

Try this out, it reduces lot of loops, if your code has some logical groupings.

Comments

Popular posts from this blog

Configure FAIR Schedular in Spark

Introduction