Java(HBase) API:如何知道以字节存储的值的数据类型

发布于 2024-12-26 13:10:06 字数 421 浏览 3 评论 0原文

使用 HBase Java API 时,我有一行代码如下:

byte[] value = scanner.next().getValue(Bytes.toBytes(family), Bytes.toBytes(qualifier));

假设我不知道该值是 Int 还是 String 类型,我应该在 Byte.toInt(value 之间使用哪个) 和 Byte.toString(value) 正确打印值?

这不是一个真正的 HBase/Hadoop 问题,而是一个 Java 问题,但我用 google 搜索并找不到一种方法来获取它。有可能知道吗?

另一方面,从 HBase Java API 中,我如何知道存储在 family:qualifier 中的给定值的数据类型?

谢谢!

when working with HBase Java API, I have a line of the code as below:

byte[] value = scanner.next().getValue(Bytes.toBytes(family), Bytes.toBytes(qualifier));

Assume I don't know if it's a Int or String type for this value, which should I use between Byte.toInt(value) and Byte.toString(value) to print the value correctly?

This is not a really HBase/Hadoop question, and rather a Java one, but I googled and can't find a way to get it. Is it possible to know it?

In another direction, from the HBase Java API, how can I know the data type for a given value stored in a family:qualifier?

Thanks!

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

陪你搞怪i 2025-01-02 13:10:06

与传统的 RDBMS 不同,HBase 不支持“类型化列”,即数据存储跟踪所存储数据的类型。 HBase 本身并不跟踪(因此无法本地告知)列中存储的数据类型。使用 HBase 的开发人员负责自行跟踪列数据类型。

对于许多应用程序来说,应用程序对每列的类型进行“硬编码”是可以接受的。这样,HBase 表往往比 RDBMS 表更特定于应用程序。开发人员还可以创建专用于行的数据类型架构的列族或列(例如,序列化为字符串的 Avro 架构)。

HBase 文档的“架构”页面在这里详细解释了 HBase 和传统 RDBMS 之间的差异:

https ://hbase.apache.org/book/architecture.html#arch.overview.when

Unlike a traditional RDBMS, HBase doesn't support "typed columns", where the data store keeps track of the types of data being stored. HBase does not natively keep track of - so there is no way to natively tell - the type of data stored in a column. The developer using HBase is responsible for keep track of column data types on their own.

For many applications, it is acceptable for the application to "hard-code" the types of each column. In this way, HBase tables tend to be more application-specific than RDBMS tables. A developer can also create a column family or column dedicated to a data type schema for the row (for example, an Avro schema serialized as a string).

The HBase documentation's "Architecture" pages explains the differences between HBase and a traditional RDBMS a bit more here:

https://hbase.apache.org/book/architecture.html#arch.overview.when

墨洒年华 2025-01-02 13:10:06

对于第一个问题,您可以尝试转换为 int,如果出现异常,您就知道它是字符串。但这并不是一个好办法。

For your first question you can try to convert to int and if you have an exception, you know that it's a String. But this is not a good way.

春风十里 2025-01-02 13:10:06

存储值时使用 OrderedBytes。这确保了每种类型的数据都以一些数值为前缀。
请参阅 https://hbase.apache.org/ apidocs/org/apache/hadoop/hbase/util/OrderedBytes.html

byte[] value = scanner.next().getValue(Bytes.toBytes(family), Bytes.toBytes(qualifier));
int typeByte = value[0]
if(typeByte == 52)
    // do operation for String
else if(typeByte == 43)
    // do operation for Integer
else if (typeByte == 45)
    // do operation for Double

注意:根据数据类型将数据写入 hbase 时附加值 43,45 和 52。

请参阅 http://davidgreenshtein 中的一个示例。 blogspot.co.uk/2015/03/geo-spatial-search-in-hbase.html

Use OrderedBytes while storing the values. Which ensures that each type of data prefixed with some numeric values.
Refer, https://hbase.apache.org/apidocs/org/apache/hadoop/hbase/util/OrderedBytes.html

byte[] value = scanner.next().getValue(Bytes.toBytes(family), Bytes.toBytes(qualifier));
int typeByte = value[0]
if(typeByte == 52)
    // do operation for String
else if(typeByte == 43)
    // do operation for Integer
else if (typeByte == 45)
    // do operation for Double

Note: Values 43,45 and 52 are appended while writing the data to hbase as per datatype.

Refer one example in http://davidgreenshtein.blogspot.co.uk/2015/03/geo-spatial-search-in-hbase.html

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文