【问题标题】:Is it ok to store all integers as Strings instead of byte[] in hbase?可以将所有整数存储为字符串而不是 hbase 中的 byte[] 吗?
【发布时间】:2016-07-08 21:00:27
【问题描述】:

我正在尝试一些 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 25mahesh 存储为 \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[]

【问题讨论】:

    标签: hbase


    【解决方案1】:

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

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

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

    【讨论】:

    • 哦,太好了,我明白了...我不必要地认为它的存储字符串是原样的。还有\x00\x00\x00\x19=(11001)bin=(25)dec 25 的byte[] 形式似乎是正确的,除非在将int 转换为byte[] 时没有更多的内容。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2022-12-01
    • 2021-03-27
    • 1970-01-01
    • 2020-10-14
    • 2017-11-05
    • 1970-01-01
    相关资源
    最近更新 更多