Migrate java code from hbase 0.92 to 0.98.0-hadoop

2019-05-26 04:12发布

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?

标签: java hbase
1条回答
迷人小祖宗
2楼-- · 2019-05-26 04:55

In Hbase 0.92 the Scan class implemented the Writable 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 a org.apache.hadoop.hbase.protobuf.generated.ClientProtos.Scan. You can do this conversion from one to the other and reverse using the two overloaded ProtobufUtil.toScan() methods. ClientProtos.Scan has methods for serialization and deserialization, like toByteArray and parseFrom.

Using Protobuf your code could be rewritten to something like this (did not check results).

Write:

 public static String convertScanToString(Scan scan) throws IOException {
   return Base64.encodeBytes( ProtobufUtil.toScan(scan).toByteArray() );
 }

Read:

  static Scan convertStringToScan(String base64) throws IOException {
    return ProtobufUtil.toScan(ClientProtos.Scan.parseFrom(Base64.decode(base64)));
  }

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.

查看更多
登录 后发表回答