java hbase 读写 16进制字节数组

环境
  • centos 6.5
  • CDH 5.15
  • JDK 1.8
  • hbase 1.2
读写\x12\x00\x00\x00\x03\x03

HBase 存储数据是使用byte[]的,要求写入都要转为byte[]类型;
参见: JAVA byte 类型

import java.io.IOException;
import org.apache.hadoop.conf.Configuration;
import org.apache.hadoop.hbase.HBaseConfiguration;
import org.apache.hadoop.hbase.client.Get;
import org.apache.hadoop.hbase.client.Put;
import org.apache.hadoop.hbase.client.ConnectionFactory;
import org.apache.hadoop.hbase.client.Connection;
import org.apache.hadoop.hbase.client.Table;
import org.apache.hadoop.hbase.client.Result;
import org.apache.hadoop.hbase.TableName;
import org.apache.hadoop.hbase.util.Bytes;
import org.apache.commons.lang.ArrayUtils;


public class Test {

    public static void main(String[] args) throws IOException {

        Configuration config = HBaseConfiguration.create();
        Connection connection = ConnectionFactory.createConnection(config);

        Table table = connection.getTable(TableName.valueOf("test"));


        //# 插入 \x12\x00\x00\x00\x03\x03
        byte[] rowKey = new byte[6];
        rowKey[0] = 0x12;
        rowKey[1] = 0x00;
        rowKey[2] = 0x00;
        rowKey[3] = 0x00;
        rowKey[4] = 0x03;
        rowKey[5] = 0x03;
        //# 如果是10进制的,hbase put会转为16进制
        Put put = new Put(rowKey);
        //# addColumn(byte[] family,byte[] qualifier,byte[] value)
        put.addColumn(Bytes.toBytes("cf"), Bytes.toBytes("_5"), Bytes.toBytes("x"));
        table.put(put);

        //# 读取 \x12\x00\x00\x00\x03\x03
        Get g = new Get(rowKey);
        Result result = table.get(g);
        //# 要读取的列族和列
        byte [] value = result.getValue(Bytes.toBytes("cf"),Bytes.toBytes("_5"));
        //# byte[]转为字符串输出
        String valueRes = Bytes.toString(value);
        System.out.println("valueRes: " + valueRes );
    }

}

编译运行

$ javac -classpath $(hbase classpath) Test.java
$ java  -classpath $(hbase classpath) Test
valueRes: x

结果

> scan 'test'
\x12\x00\x00\x00\x03\x03                        column=cf:_5, timestamp=1569572415866, value=x
10进制转16进制的数据
import java.io.IOException;
import org.apache.hadoop.conf.Configuration;
import org.apache.hadoop.hbase.HBaseConfiguration;
import org.apache.hadoop.hbase.client.Get;
import org.apache.hadoop.hbase.client.Put;
import org.apache.hadoop.hbase.client.ConnectionFactory;
import org.apache.hadoop.hbase.client.Connection;
import org.apache.hadoop.hbase.client.Table;
import org.apache.hadoop.hbase.client.Result;
import org.apache.hadoop.hbase.TableName;
import org.apache.hadoop.hbase.util.Bytes;
import org.apache.commons.lang.ArrayUtils;


public class Test {

    public static void main(String[] args) throws IOException {

        Configuration config = HBaseConfiguration.create();
        Connection connection = ConnectionFactory.createConnection(config);

        Table table = connection.getTable(TableName.valueOf("test"));


        //# 插入 \x12\x00\x00\x00\x03\x03
        byte byteB1 = 18; //# 10进制的18,转为16进制为12
        byte byteB2 = 0;
        byte byteB3 = 3;
        byte[] rowKey = new byte[] {byteB1,byteB2,byteB2,byteB2,byteB3,byteB3};
        
        //# 如果是10进制的,hbase put会转为16进制
        Put put = new Put(rowKey);
        put.addColumn(Bytes.toBytes("cf"), Bytes.toBytes("_6"), Bytes.toBytes("x"));
        table.put(put);
    }
}

结果

> scan 'test'
\x12\x00\x00\x00\x03\x03                        column=cf:_6, timestamp=1569572905616, value=x

参考
Package org.apache.hadoop.hbase.client

  • 1
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值