Basic HBase Java Classes and Methods – Part 4: Putting Data into a Table

To put data into a table in HBase, we will create a Class very similar in structure to our last Class in Part 3.  Instead of using an Admin object, which is used to create a table or delete it, we will just work with a regular table object.  All data in HBase is stored as byte arrays.  Lets create our imports and basic variables to store our column family names and columns.

Now we create our main method, create a connection to our Table, instantiate a Put object and add columns to it using our addColumn method.   Finally we use the put method on the Table object to put the data into the table.  We have the table defined outside of the try block because we need to check for it in the finally block later on, and we can’t do that if its defined in the try block itself, as then it would be out of scope.

This is a very simple case.  We did not have to insert all of the columns, we could have left many blank just as we did before when using the HBase shell.  The HBase table put method is overloaded and supports either passing in a Put object or a list of Put objects.  We will now put more data in using a list of Put objects.

Last we will use our finally block to close our connection to HBase and check if we have an open table and if so close it.

So the completed program looks like so:

We can see that our data was added properly by checking with scan from the HBase shell.

Next we will explore how we can retrieve column data from the HBase table in Basic HBase Java Classes and Methods – Part 5: Getting Data from a Table.

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

Leave a Reply