Is this method for HBase data storage correct?

雨燕双飞 提交于 2019-12-25 02:53:20

问题


Here what is want to do is i want to store and retrieve serialized data in HBase table and later i want to retrieve them as it is. I thought to follow method. Please tell me if i'm wrong.

put.add(streamColumnFamily,streamColumnName,serializedData);

Here serializedData attribute will be handle by HBaseSerialization class. what is want to is, is this method correct. will i be able to retrieve stored data as it was. (int as int, float as float, String as String etc)


回答1:


Yes, the method is correct. HBase stores everything in bytes. You basically do something like

byte[] key = createSomeKey();

Put put = new Put(key);
put.add(streamColumnFamily,streamColumnName,serializedData); 

HTable h = .... // create HTable from HAdmin 

h.put(put);

You can also use native java serialization mechanism for serializing and deserializing objects like this:

   public byte[] serialize(Serializable object) throws IOException {


      ByteArrayOutputStream byteArrayOutputStream = new ByteArrayOutputStream();

      ObjectOutput stream = new ObjectOutputStream(byteArrayOutputStream);

      stream.writeObject(object);

      stream.flush();

      return byteArrayOutputStream.toByteArray()
    }


public Object deserialize(byte[] bytes){

     ByteArrayInputStream byteArrayInputStream = new ByteArrayInputStream(bytes);

     ObjectInputStream objectInputStream = new ObjectInputStream(byteArrayInputStream);

     objectInputStream.readObject(); 
   }

Also if you are serializing and deserializing basic object like Integer,Long, String ... there is a good utility class called Bytes in org.apache.hadoop.hbase.util



来源:https://stackoverflow.com/questions/24237274/is-this-method-for-hbase-data-storage-correct

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!