是否可以将所有整数存储为字符串而不是hbase中的byte []?

时间:2016-07-08 08:16:45

标签: hbase

我正在尝试一些hbase代码。我意识到当我使用put命令通过hbase shell插入数据时,所有内容(数字和字符串)都将作为字符串放置:

hbase(main):001:0> create 'employee', {NAME => 'f'}
hbase(main):003:0> put 'employee', 'ganesh','f:age',30
hbase(main):004:0> put 'employee', 'ganesh','f:desg','mngr'
hbase(main):005:0> scan 'employee'
ROW                   COLUMN+CELL
ganesh               column=f:age, timestamp=1467926618738, value=30
ganesh               column=f:desg, timestamp=1467926639557, value=mngr

但是,当我使用Java API放置数据时,非字符串内容被序列化为byte[]

Cluster lNodes = new Cluster();
lNodes.add("digitate-VirtualBox:8090");
Client lClient= new Client(lNodes);
RemoteHTable remoteht = new RemoteHTable(lClient, "employee");

Put lPut = new Put(Bytes.toBytes("mahesh"));
lPut.add(Bytes.toBytes("f"), Bytes.toBytes("age"), Bytes.toBytes(25));
lPut.add(Bytes.toBytes("f"), Bytes.toBytes("desg"), Bytes.toBytes("dev"));
remoteht.put(lPut);

在hbase shell中扫描显示age 25 mahesh存储为\x00\x00\x00\x19

hbase(main):006:0> scan 'employee'
ROW                   COLUMN+CELL
ganesh               column=f:age, timestamp=1467926618738, value=30
ganesh               column=f:desg, timestamp=1467926639557, value=mngr
mahesh               column=f:age, timestamp=1467926707712, value=\x00\x00\x00\x19
mahesh               column=f:desg, timestamp=1467926707712, value=dev
  1. 考虑到我将只在hbase中存储数字和字符串数据,它会将数值数据存储为byte[](如上所述)或字符串:

    lPut.add(Bytes.toBytes("f"), Bytes.toBytes("age"), Bytes.toBytes("25"));  //instead of toBytes(25)
    
  2. 另外,为什么字符串按原样存储,并且即使使用Java API也不会序列化为byte[]

1 个答案:

答案 0 :(得分:2)

我认为您需要阅读有关hbase的更多信息。 Hbase将所有内容存储为byte []。您在扫描表时看到shell输出转换为字符串。有时像整数这样的非字符串数据无法正确转换。但这只是hbase shell试图让人类可读,内部一切都是byte []。 所以

1-如果存储整数,则需要将它们存储为整数,因此它们总是使用4个字节,如果将它们存储为字符串,则每个长度使用1个字节,可能是2个字节。

2-如上所述,字符串被转换为byte [],所以这只是shell让你这么想。

相关问题