I hava some code, wrote with hbase 0.92:
/**
* Writes the given scan into a Base64 encoded string.
*
* @param scan The scan to write out.
* @return The scan saved in a Base64 encoded string.
* @throws IOException When writing the scan fails.
*/
public static String convertScanToString(Scan scan) throws IOException {
ByteArrayOutputStream out = new ByteArrayOutputStream();
DataOutputStream dos = new DataOutputStream(out);
scan.write(dos);
return Base64.encodeBytes(out.toByteArray());
}
/**
* Converts the given Base64 string back into a Scan instance.
*
* @param base64 The scan details.
* @return The newly created Scan instance.
* @throws IOException When reading the scan instance fails.
*/
static Scan convertStringToScan(String base64) throws IOException {
ByteArrayInputStream bis = new ByteArrayInputStream(Base64.decode(base64));
DataInputStream dis = new DataInputStream(bis);
Scan scan = new Scan();
scan.readFields(dis);
return scan;
}
I need to migrate this code to hbase0.98.0-hadoop2. There is no longer write() readFields()
in Scan class. Can someone please help me figure this out?
In Hbase 0.92 the
Scan
class implemented theWritable
interface which handled serialization. With Hbase 0.96 this type manual serialization was deprecated in favor of Google's protobuf (see issue hbase-6477).In order to serialize a
org.apache.hadoop.hbase.client.Scan
with Hbase 0.96+ you need to convert it first to aorg.apache.hadoop.hbase.protobuf.generated.ClientProtos.Scan
. You can do this conversion from one to the other and reverse using the two overloadedProtobufUtil.toScan()
methods. ClientProtos.Scan has methods for serialization and deserialization, liketoByteArray
andparseFrom
.Using Protobuf your code could be rewritten to something like this (did not check results).
Write:
Read:
The Protobuf serialization is not compatible with the old one. If you need to convert I would recommend just getting the old serialization code from the Scan class.