I am trying to insert age as number int Bytes.toBytes(44) to HTable but value is not being persisted. Same is happening with Row key also. I am using Cloudera Quick Start VM: Here is my code :
package com.sohi.put;
import java.io.IOException;
import org.apache.hadoop.conf.Configuration;
import org.apache.hadoop.hbase.HBaseConfiguration;
import org.apache.hadoop.hbase.HColumnDescriptor;
import org.apache.hadoop.hbase.HTableDescriptor;
import org.apache.hadoop.hbase.MasterNotRunningException;
import org.apache.hadoop.hbase.TableName;
import org.apache.hadoop.hbase.ZooKeeperConnectionException;
import org.apache.hadoop.hbase.client.Get;
import org.apache.hadoop.hbase.client.HBaseAdmin;
import org.apache.hadoop.hbase.client.HTable;
import org.apache.hadoop.hbase.client.Put;
import org.apache.hadoop.hbase.client.Result;
import org.apache.hadoop.hbase.util.Bytes;
public class TestClass {
public static void main(String[] args) throws MasterNotRunningException, ZooKeeperConnectionException, IOException {
Configuration conf = HBaseConfiguration.create();
HBaseAdmin hBaseAdmin = new HBaseAdmin(conf);
TableName tableName = TableName.valueOf("SampleDataTable") ;
HTableDescriptor tableDescriptor = new HTableDescriptor(tableName) ;
HColumnDescriptor personalColFam = new HColumnDescriptor("personal");
HColumnDescriptor officeColFam = new HColumnDescriptor("office");
tableDescriptor.addFamily(personalColFam);
tableDescriptor.addFamily(officeColFam);
if(!hBaseAdmin.tableExists(tableName)){
hBaseAdmin.createTable(tableDescriptor);
}
HTable hTable = new HTable(conf, tableName);
byte [] row = Bytes.toBytes(121);
byte [] personalCf = Bytes.toBytes("personal");
byte [] officeCf = Bytes.toBytes("office");
byte [] qual1 = Bytes.toBytes("name");
byte [] qual2 = Bytes.toBytes("age");
byte [] qual3 = Bytes.toBytes("location");
byte [] name = Bytes.toBytes("John");
byte [] age = Bytes.toBytes(44);
byte [] loc = Bytes.toBytes("USA");
Put data = new Put(row);
data.add(personalCf, qual1, name);
data.add(personalCf, qual2, age);
data.add(officeCf, qual3, loc);
hTable.put(data);
// giving issue with integer values like age and row key
System.out.println("Done");
}
}
Here is the output :
Note the Key value and Age Value.
Hue's HBase Browser does a good job of displaying strings correctly - but not other data types. The reason is pretty simple: HBase doesn't know what data type you've stored - but you do! For example, if you read 8 bytes from an HBase column, it could be a 64-bit integer or a float or 8 single byte characters.
Now, to come back to your question: Your code is working perfectly fine!
The comma you're seeing is ASCII representation of 44
(see http://www.ascii-code.com/)
When you read the data back from HBase in your code, use Bytes.toInt()
and you'll be able to correctly read it as integer 44
.