java - 将java代码从hbase 0.92迁移到0.98.0-hadoop2

标签 java hbase

我有一些代码,用 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()。有人可以帮我解决这个问题吗?

最佳答案

在 Hbase 0.92 中,Scan 类实现了处理序列化的 Writable 接口(interface)。在 Hbase 0.96 中,这种类型的手动序列化已被弃用,取而代之的是 Google's protobuf (参见问题 hbase-6477)。

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

使用 Protobuf 你的代码可以重写成这样(没有检查结果)。

写:

 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 类中获取旧的序列化代码。

关于java - 将java代码从hbase 0.92迁移到0.98.0-hadoop2,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/30797065/

相关文章:

apache - 获得 HBase 的 HTable 句柄的最佳方法是什么?

python - Hbase Shell收到此错误:fstat未实现,不受支持或无法加载 native 支持

java - 在 Java 中创建 InetAddress 对象

java - 为什么变量在匿名方法中必须是 final 而类字段不需要

java - 将数据加载到Hbase

hadoop - 不能 ssh 权限被拒绝。 HBase Hadoop

hadoop - HBase LZO表扫描导致RegionServer关闭

java - 如何使用Java反射读取泛型参数的类类型

java - 将Sesame应用程序部署到Tomcat后出错

java - Eclipse 中的 GWT - 加载模块时出错