我正在尝试一些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
考虑到我将只在hbase中存储数字和字符串数据,将数字数据存储为byte[]
(如上述情况)或作为字符串存储确实有什么好处:
lPut.add(Bytes.toBytes("f"), Bytes.toBytes("age"), Bytes.toBytes("25")); //instead of toBytes(25)
另外,为什么字符串按原样存储,byte[]
即使使用Java API放置时也不能序列化?
我认为您需要阅读有关hbase的更多信息。Hbase将所有内容存储为byte []。扫描表时,您会看到shell输出转换为字符串。有时,诸如整数之类的非字符串数据无法正确转换。但这只是hbase shell试图使其易于理解,在内部,所有内容都是byte []。所以
1-如果存储整数,则需要将它们存储为整数,因此它们始终使用4个字节;如果存储为字符串,则每个长度使用1个字节,也许是2个字节。
2-字符串如上所述被转换为byte [],所以这只是shell使您以这种方式考虑。
本文收集自互联网,转载请注明来源。
如有侵权,请联系[email protected] 删除。
我来说两句