Java API (HBase): how to find the data type of a value stored in bytes

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

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

Suppose I donโ€™t know if this value is a value of type Int or String , which should I use between Byte.toInt(value) and Byte.toString(value) to print the value correctly ?

This is not a HBase / Hadoop question, but rather Java, but I googled and cannot find a way to get it. Is it possible to find out?

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

Thanks!

+7
source share
3 answers

Unlike a traditional DBMS, HBase does not support โ€œtyped columnsโ€, where the data warehouse keeps track of the types of data stored. HBase does not track initially - so there is no way to present information - the type of data stored in the column. The developer using HBase is responsible for tracking column data types on their own.

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

The HBase Architecture documentation pages explain the differences between HBase and traditional RDBMS a bit more:

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

+5
source

For your first question, you can try converting to int and, if you have an exception, you know that it is String. But this is not very good.

+3
source

Use OrderedBytes while storing values. This ensures that each data type has a prefix with some numerical values. See 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 added when writing data to hbase according to the data type.

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

0
source

All Articles