Basic HBase Java Classes and Methods – Part 3: Table Creation

We will cover these basic steps:

Instantiating a configuration object
Establishing a connection to HBase
Manipulating tables using an administration object
Manipulating data within a table using a table instance

Creating our table

I am using Maven, and below is my pom.xml that I will be using for all of these examples.

You may also want to optionally create a log4j.properties file if using Maven.  Maven uses this file for logging.  We will create a basic properties file and store it in our main/resources folder.  For more information on log4j visit the project site.  Here is the basic properties file we will use.

AdminCreateTable basic libraries and main method.

Instantiating a Configuration object 

We will instantiate a Configuration object using the HBaseConfiguration static method.  The configuration Class is the base class for all config objects in the Hadoop ecosystem.

Establish a connection to the HBase cluster

We use the ConnectionFactory to create a connection object by passing in our Configuration object.

All code which uses our connection should be in try / finally blocks so that you can manage the connection manually and close the connection when you are done using it.

Instantiate an Admin object

Because functions such as Creating or Removing tables are administrative functions, these must be done using the Admin object.  We will create an Admin object from our Connection object.

You can see we put the instantiation of the Admin object inside of our try/finally clause.  The rest of our commands will also be inside this clause.  We added a command to close our connection in the finally clause.

Create the table schema using an HTableDescriptor

We use an HTableDescriptor to define our table, its properties such as column families, performance settings, etc.  We will create a table of name employee and two column families: personal and professional.

Create the table

We check to see if the table exists using our Admin object.  If it does not exist, we create it, and it does exist we print so.  We use the createTable method on our Admin object and pass in the HTableDescriptor we created previously.

Putting it all together we have the following:

Build and Run the code. You can verify the table has been created by going to the HBase Shell and verifying it exists.

See you in the next part Basic HBase Java Classes and Methods – Part 4: Putting Data into a Table

This entry was posted in Data Analytics and tagged , . Bookmark the permalink.

Leave a Reply