I have a small HBase
cluster running with a Java
frontend. I have it working successfully with test code that establishes a connection, runs test data, and disconnects, but I need to extend it into a webapp as part of a larger project. It was suggested that I use Tomcat
as the HTTP
service to interface with the database, and as part of this I need to set up a connection pool.
I understand connection pools on a conceptual level (a pile of persistent connections to the DB handed out to clients as needed and returned to the pool when discarded), but I've never written a webapp before and Tomcat
is vexxing me. I can find loads of documentation on how to setup a connection pool to a SQL
server using JDBC
, but nothing on HBase
(or, for that matter, anything BUT SQL
) and nothing on how to (or whether I can or should) write a custom interface to get it working with Tomcat
. Is this doable, or even possible, or should I be taking a different approach?
I think it would be hard to find something that is made for Tomcat
, I don't think that such thing exist. Most people are probably using custom classes. That being said, you should probably build something around the HTablePool
that comes with HBase
.
Here an example of something you can use in your project :
public class HTableManager {
public static int HTABLE_POOL_SIZE = 15;
private static HTableManager instance;
private static HTablePool hTablePool;
/**
* Private Constructor
*/
private HTableManager() {
try {
Configuration config = HBaseConfiguration.create();
config.set("hbase.defaults.for.version.skip", "false");
hTablePool = new HTablePool(config, HTABLE_POOL_SIZE);
} catch (IOException ex) {
// Error handling
}
}
/**
* @return The HbaseTableManager instance
*/
public static HTableManager getInstance() {
if (instance == null) {
instance = new HTableManager();
}
return instance;
}
/**
* Method used to retrieve a HTable instance.
*
* @param tableName The table name
* @return The HTableInterface instance
* @throws IOException
*/
public synchronized HTableInterface getHTable(String tableName) throws IOException {
return hTablePool.getTable(tableName);
}
}
And then you can use it like this :
HTableManager hTableManager = htableManager.getInstance();
HTableInterface hTable = hTableManager.getHTable("yourTableName");
...
// Work with the hTable instance
...
// A call to the close method returns it to the pool
hTable.close();
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.