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!
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
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.
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