Reputation: 205
I was encoding every column value as string to load data in HBase using the following:
put.add("columnFamily".getBytes(), new String(columnName).getBytes(), new String(value).getBytes())
That's why I was getting an exception
java.lang.IllegalArgumentException: offset (0) + length (4) exceed the capacity of the array: 1
while reading the data, because one of the columns has float values (if I am not mistaken). There are few methods like org.apache.hadoop.hbase.util.Bytes.toBytes(float)
available but I don't know how to do it.
Could somebody tell me how can I encode float/double/integer value so that further reading data from HBase I will not get the same exception? Any help would be highly appreciated.
Upvotes: 1
Views: 3185
Reputation: 6739
you can do something like below:
import org.apache.hadoop.hbase.util.Bytes
val intNumber: Int = 100
put.addColumn(Bytes.toBytes("column_family_name"), Bytes.toBytes("column_name"), Bytes.toBytes(intNumber))
The above will save the number in HBase table in bytes format, in case if you want to save number in String format you can add toString
to intNumber
like below
put.addColumn(Bytes.toBytes("column_family_name"), Bytes.toBytes("column_name"), Bytes.toBytes(intNumber.toString))
Upvotes: 3