由于场景需要,需要向hbase中插入包含‘\x00’的rowkey,例:
起初以为直接以拼接字符串的形式往里插就可以,但是读源码发现hbase会对\做二进制转译,如下:
‘\’会被转译为\x5c
解决办法:我们在插入数据的时候直接以byte[] 的形式往hbase表插,代码如下:
public class Test_DESC {
public static void main(String[] args) throws IOException {
System.setProperty("spark.serializer", "org.apache.spark.serializer.KryoSerializer");
System.setProperty("hadoop.home.dir", "C:\\hadoop-2.6.0-cdh5.11.0");
Configuration hbaseConf = HBaseConfiguration.create();
hbaseConf.set("hbase.zookeeper.quorum","zt01:2181,zt02:2181,zt03:2181");
HTable htabe = new HTable(hbaseConf,"INDEX_SDC_TEST");
ByteArrayOutputStream bout = new ByteArrayOutputStream();
byte[] a = Bytes.toBytes("hello");
byte b = (byte) 0;
byte[] c = new byte[] {b};
byte[] d = Bytes.toBytes("999");
byte[] e = ArrayUtils.addAll(a,c);
byte[] indexColumnKey1 = ArrayUtils.addAll(e,d);
Put put = new Put(indexColumnKey1);
put.add(Bytes.toBytes("0"), Bytes.toBytes("_0"), Bytes.toBytes("x"));
htabe.put(put);
}
}
这样就避免中间字符串转byte时的特殊符号转译!