将java代码从hbase 0.92迁移到0.98.0-hadoop2 [英] Migrate java code from hbase 0.92 to 0.98.0-hadoop2

查看:198
本文介绍了将java代码从hbase 0.92迁移到0.98.0-hadoop2的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一些代码,用hbase 0.92写道:

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

我需要将此代码迁移到hbase0.98.0-hadoop2。 Scan类中不再有 write()readFields()。有人可以帮我解决这个问题吗?

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?

推荐答案

在Hbase 0.92中扫描 class实现了处理序列化的 Writable 接口。使用Hbase 0.96时,不推荐使用此类型的手动序列化,以支持 Google的protobuf (请参阅问题 hbase-6477 )。

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

为了使用Hbase 0.96+序列化 org.apache.hadoop.hbase.client.Scan ,您需要首先将它转换为 org.apache.hadoop.hbase.protobuf.generated.ClientProtos.Scan 。您可以使用两个重载的 ProtobufUtil.toScan()方法进行从一个到另一个的转换。 ClientProtos.Scan具有序列化和反序列化的方法,如 toByteArray parseFrom

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.

使用Protobuf可以将代码重写为这样的代码(不检查结果)。

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

写:

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

阅读:

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

Protobuf序列化与旧序列化不兼容。如果你需要转换我建议只从Scan类中获取旧的序列化代码。

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.

这篇关于将java代码从hbase 0.92迁移到0.98.0-hadoop2的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆