可以将所有整数存储为字符串而不是 hbase 中的 byte[] 吗?
Is it ok to store all integers as Strings instead of byte[] in 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
of 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)
还有为什么即使使用 Java API?
我认为您需要阅读更多有关 hbase 的内容。 Hbase 将所有内容存储为 byte[]。当您扫描 table 时,您会看到 shell 输出转换为字符串。有时像整数这样的非字符串数据不能正确转换。但这只是 hbase shell 试图成为人类可读的,在内部一切都是 byte[]。
所以
1- 如果你存储整数,你需要将它们存储为整数,所以它们总是使用 4 个字节,如果你将它们存储为字符串,它们每个长度使用 1 个字节,可能是 2 个字节。
2- 正如我上面所说,字符串被转换为 byte[],所以这只是 shell 让你这样想。
我正在尝试一些 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
of 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)
还有为什么即使使用 Java API?
我认为您需要阅读更多有关 hbase 的内容。 Hbase 将所有内容存储为 byte[]。当您扫描 table 时,您会看到 shell 输出转换为字符串。有时像整数这样的非字符串数据不能正确转换。但这只是 hbase shell 试图成为人类可读的,在内部一切都是 byte[]。 所以
1- 如果你存储整数,你需要将它们存储为整数,所以它们总是使用 4 个字节,如果你将它们存储为字符串,它们每个长度使用 1 个字节,可能是 2 个字节。
2- 正如我上面所说,字符串被转换为 byte[],所以这只是 shell 让你这样想。