我试图在Java代码中编写一个简单的HBase数据操作
守则如下
public static void main(String[] args) {
Configuration hBconfig = HBaseConfiguration.create();
hBconfig.addResource(new Path("/home/circar/hbase/conf/hbase-site.xml"));
try {
HTable hTable = new HTable(hBconfig, "exampleHbTable");
Result result = hTable.get(new Get(Bytes.toBytes("ExampleRow1")));
List<KeyValue> keyValList = result.list();
for(KeyValue keyValue : keyValList){
System.out.println("a. Key:"+keyValue.getKeyString());
System.out.println("a. KeyType:"+keyValue.getType());
System.out.println("a. Row:"+keyValue.getRow().toString());
System.out.println("a. Family:"+keyValue.getFamily().toString());
System.out.println("a. Qualifier:"+keyValue.getQualifier().toString());
System.out.println("a. Value:"+keyValue.getValue().toString());
System.out.println("a. TotalColLength:"+keyValue.getTotalColumnLength());
System.out.println("a. TimeStamp:"+keyValue.getTimestamp());
SplitKeyValue splitKeyValue = keyValue.split();
System.out.println("b. KeyType:"+splitKeyValue.getType());
System.out.println("b. Row:"+splitKeyValue.getRow().toString());
System.out.println("b. Family:"+splitKeyValue.getFamily().toString());
System.out.println("b. Qualifier:"+splitKeyValue.getQualifier().toString());
System.out.println("b. Value:"+splitKeyValue.getValue().toString());
System.out.println("b. TimeStamp:"+splitKeyValue.getTimestamp());
}
} catch (IOException e) {
e.printStackTrace();
}
}
结果显示为
13/06/21 18:15:29 INFO zookeeper.ClientCnxn: Socket connection established to localhost/127.0.0.1:2181, initiating session
13/06/21 18:15:29 INFO zookeeper.ClientCnxn: Session establishment complete on server localhost/127.0.0.1:2181, sessionid = 0x13f66c41fc90003, negotiated timeout = 40000
a. Key:\x00\x0BExampleRow1\x10CircarConsultingMapReduce\x00\x00\x01?f\x16m\x0D\x04
a. KeyType:4
a. Row:[B@15718f2
a. Family:[B@126f827
a. Qualifier:[B@16dfa45
a. Value:[B@149eb9f
a. TotalColLength:25
a. TimeStamp:1371807313165
b. KeyType:[B@289d2e
b. Row:[B@6754d6
b. Family:[B@1f2cea2
b. Qualifier:[B@1dc0e7a
b. Value:[B@3a9bba
b. TimeStamp:[B@1c5ddc9
a. Key:\x00\x0BExampleRow1\x08ProjectsMapReduce\x00\x00\x01?f\x16m\x0D\x04
a. KeyType:4
a. Row:[B@163f7a1
a. Family:[B@1690ab
a. Qualifier:[B@173ec72
a. Value:[B@1a85d38
a. TotalColLength:17
a. TimeStamp:1371807313165
b. KeyType:[B@8046f4
b. Row:[B@1b273cc
b. Family:[B@d05c13
b. Qualifier:[B@14d1d41
b. Value:[B@1bbd7b2
b. TimeStamp:[B@14df764
但它具有HBase Shell中显示的值如下:
~-desktop:~$ sudo hbase/bin/hbase shell
HBase Shell; enter 'help<RETURN>' for list of supported commands.
Type "exit<RETURN>" to leave the HBase Shell
Version 0.94.8, r1485407, Wed May 22 20:53:13 UTC 2013
hbase(main):001:0>
hbase(main):002:0* list
TABLE
exampleHbTable
1 row(s) in 0.7540 seconds
hbase(main):003:0> scan 'exampleHbTable'
ROW COLUMN+CELL
ExampleRow1 column=CircarConsulting:MapReduce, timestamp=1371807313165, value=Hadoop
ExampleRow1 column=Projects:MapReduce, timestamp=1371807313165, value=MyProject
1 row(s) in 0.1170 seconds
hbase(main):004:0>
有人会提出使用Java Code获取实际可读值的想法吗?
答案 0 :(得分:0)
使用HBase API提供的Bytes.toString()
将这些byte []转换为String表示形式。 Java的toString()在这里不起作用。
Example:
System.out.println("a. Row:" + Bytes.toString(keyValue.getRow()));