向hbase的rowkey中插入‘\x00’

由于场景需要,需要向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时的特殊符号转译!

 

 

 

 

 

评论 3
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值