本文介绍了这种HBase数据存储方法是否正确?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

这里要做的是我想在HBase表中存储和检索序列化数据,后来我想按照原样检索它们。我想按照方法。请告诉我,如果我错了。

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

这里serializedData属性将由类。想要的是,这种方法是否正确。我将能够以原样检索存储的数据。 (int为int,float为float,String为String等)

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)

推荐答案

是的,该方法是正确的。 HBase以字节存储所有内容。
您基本上可以做类似于

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

您还可以使用本机java序列化机制来序列化和反序列化对象,如下所示:

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

另外,如果您正在序列化和反序列化像Integer,Long,String这样的基本对象。 .. org.apache.hadoop.hbase.util

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

这篇关于这种HBase数据存储方法是否正确?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

10-20 01:20