环境
- 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