Database Reference
In-Depth Information
String phone = "555-1234";
byte[] key = Bytes.toBytes(userid);
Put put = new Put(key);
put.add(Bytes.toBytes(familyName),
Bytes.toBytes("email"), Bytes.toBytes(email));
put.add(Bytes.toBytes(familyName),
Bytes.toBytes("phone"), Bytes.toBytes(phone));
htable.put(put);
}
long t2b = System.currentTimeMillis();
System.out.println("inserted " + total + " users in " +
(t2b - t2a) + " ms");
htable.close();
}
}
The code we just saw inserts some sample user data into HBase. We are profiling two
operations, that is, connection time and actual insert time.
A sample run of the Java application yields the following:
Connected to HTable in : 1139 ms
inserted 100 users in 350 ms
We spent a lot of time in connecting to HBase. This makes sense. The connection
process has to go to ZooKeeper first and then to HBase. So, it is an expensive
operation. How can we minimize the connection cost?
The answer is by using connection pooling. Luckily, for us, HBase comes with a
connection pool manager. The Java class for this is HConnectionManager . It is very
simple to use.
Let's update our class to use HConnectionManager :
Code : File name: hbase_dp.ch8.UserInsert2.java
package hbase_dp.ch8;
 
Search WWH ::




Custom Search