问题
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