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

cmt 阅读:233 2023-09-07 20:43:31 评论:0

我有一些代码,用 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 类中获取旧的序列化代码。


标签:hadoop
声明

1.本站遵循行业规范,任何转载的稿件都会明确标注作者和来源;2.本站的原创文章,请转载时务必注明文章作者和来源,不尊重原创的行为我们将追究责任;3.作者投稿可能会经我们编辑修改或补充。

关注我们

一个IT知识分享的公众号