将 java 代码从 hbase 0.92 迁移到 0.98.0-hadoop2
Migrate java code from hbase 0.92 to 0.98.0-hadoop2
我有一些代码,用 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。扫描 class 中不再有 write() readFields()
。有人可以帮我解决这个问题吗?
在 Hbase 0.92 中,Scan
class 实现了处理序列化的 Writable
接口。在 Hbase 0.96 中,这种类型的手动序列化已被弃用,取而代之的是 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
。
使用 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 序列化与旧版本不兼容。如果您需要转换,我建议您只从扫描 class.
中获取旧的序列化代码
我有一些代码,用 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。扫描 class 中不再有 write() readFields()
。有人可以帮我解决这个问题吗?
在 Hbase 0.92 中,Scan
class 实现了处理序列化的 Writable
接口。在 Hbase 0.96 中,这种类型的手动序列化已被弃用,取而代之的是 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
。
使用 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 序列化与旧版本不兼容。如果您需要转换,我建议您只从扫描 class.
中获取旧的序列化代码