Rebuilding the Enterprise - Software, Hardware and Peopleware Migrations for the Systems Architect

5/03/2014

HBase, Thrift & C# - First Connections

5/03/2014 Posted by William Berry , , No comments
I seemingly have a bad habit of never writing part two of a series.  So to buck the trend, this piece is a follow up to NuGet Servers, HBase, Thrift Code Generation and one sweet Jenkins CI Build.  You don't need to read the whole post; but, familiarizing yourself with section III and on is important.  Don't forget to check other posts in this series.

Part 1 - NuGet Servers, HBase, Thrift Code Generation and one sweet Jenkins CI Build
Part 2 - HBase, Thrift & C# - First Connections
Part 3 - HBase, Thrift, & C# - Managing Sessions
Part 4 - HBase, Thrift, & C# - First Scanner and Leveraging Generics

***

Let's take a quick look at establishing connectivity to our HBase Thrift server.  The example connection code in the thrift C# solution is a great primer and what we will use as the base here.

For the majority of the code in the rest of this series, we will take a simplistic page from NHibernate's architecture.  Let's start with a simple Interface called ISession which implements IDisposable.  The implementation of IDisposable is so we can build sessions within a using statement, letting the framework do our heavy lifting.

Further, we will build a class called Session that implements the Interface ISession.  Its constructor will take a reference to our Pool Manager class and will take a Connection object that has configuration data in it.

  1 public class Session : ISession
  2 { 
  3     private readonly IConnection _connection;
  4     private readonly ISessionPoolManager _manager;
  5     private readonly TSocket _transport;
  6     private Hbase.Hbase.Client _client; 
  7  
  8     public Session(ISessionPoolManager manager, IConnection connection)
  9     { 
 10          Guid = Guid.NewGuid(); 
 11          _connection = connection; 
 12          _manager = manager; 
 13          _transport = new TSocket(Connection.Name, Connection.Port);
 14          var protocol = new TBinaryProtocol(_transport);
 15          _client = new Hbase.Hbase.Client(protocol);
 16     }

The class has a private method called OpenConnection() that is used to, well, open a connection to HBase via Thrift. This code is very straight forward:

  1 private Hbase.Hbase.Client OpenConnection()
  2 { 
  3      _transport.Open(); 
  4      IsConnected = _transport.IsOpen; 
  5      return _client; 
  6 }

For completeness let's implement a CloseConnection():

  1 private void CloseConnection()
  2 { 
  3       _transport.Close(); 
  4       IsConnected = _transport.IsOpen; 
  5 }

Because the Session class implements the IDisposable interface we are required to have a Dispose method.  The Dispose method calls our internal close on the transport and re-queues this object in Pool Manager's queue.

  1 public void Dispose()
  2 { 
  3      CloseConnection(); 
  4      _manager.Requeue(this); 
  5 }

In the next article we will look at implementing a table scanner for HBase and the structure to support it.


0 comments:

Post a Comment