Saturday, July 25, 2015

HBase Spring Data Sample

Document  Version 1.0

<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns=""

    <context:component-scan base-package="springdt" />
    <context:property-placeholder location=""/>

    <hdp:configuration id="hadoopConfiguration">

    <hdp:hbase-configuration configuration-ref="hadoopConfiguration" zk-quorum="" zk-port="2181"/>

    <bean id="hbaseTemplate" class="">
        <property name="configuration" ref="hbaseConfiguration"/>
<bean id="hBaseService" class="springdt.HBaseService"/>


The "HbaseService" bean is the only bean of this sample. We will show the code right away.

4. Write code accessing HBase

In this sample we will create a table in HBase called "report", and try store some data in the table. The table has one column family called "data", and only one column named "file". The value of "file" is file name, which could be report name in real life.

package springdt;

import javax.inject.Inject;

import org.apache.hadoop.conf.Configuration;
import org.apache.hadoop.hbase.HColumnDescriptor;
import org.apache.hadoop.hbase.HTableDescriptor;
import org.apache.hadoop.hbase.client.HBaseAdmin;
import org.apache.hadoop.hbase.client.HTableInterface;
import org.apache.hadoop.hbase.client.Put;
import org.apache.hadoop.hbase.util.Bytes;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

 * Demonstrating how to access HBase using Spring Data for Hadoop.
 * @author swang
public class HBaseService {

private Configuration hbaseConfiguration;

private HbaseTemplate hbTemplate;

// Table info
final String tableName = "report";
final String columnFamilyData = "data";
final String colFile = "file";
final String rowNamePattern = "row";
final String value = "report24.csv-";


* @throws Exception

public void run() throws Exception {
// 1. create table
// 2. add data entry

* Creates HBase table

* @throws Exception
public void createTable() throws Exception {
HBaseAdmin admin = new HBaseAdmin(hbaseConfiguration);

if (admin.tableExists(tableName)) {

HTableDescriptor tableDes = new HTableDescriptor(tableName);
HColumnDescriptor cf1 = new HColumnDescriptor(columnFamilyData);

* Adds data entry for report.
private void addData() {
hbTemplate.execute(tableName, new TableCallback<Boolean>() {

public Boolean doInTable(HTableInterface table) throws Throwable {
for (int i = 0; i < 1000; i++) {
Put p = new Put(Bytes.toBytes(rowNamePattern + i));
Bytes.toBytes(colFile), Bytes.toBytes(value + i));
return new Boolean(true);

public static void main(String[] args) throws Exception {
AbstractApplicationContext ctx = new ClassPathXmlApplicationContext(

HBaseService hBaseService = (HBaseService) ctx.getBean("hBaseService");;

The "SpringBeans.xml" we created in step 3 should be put in "src/main/resources", so that it could be found at runtime.

5. Run the sample

We assume you have HBase over Hadoop installed. Make sure the HBase version is compatible with Hadoop. In my case I have "hbase-" and "hadoop-2.7.0".
Now start HBase server.
Run "HBaseService" in Eclipse as java application. In Eclipse console  you will see output like this:

2015-07-25 15:10:52 INFO  ClientCnxn:852 - Socket connection established to, initiating session
2015-07-25 15:10:52 INFO  ClientCnxn:1235 - Session establishment complete on server, sessionid = 0x14ec4b8cf30000e, negotiated timeout = 90000
2015-07-25 15:10:53 INFO  HBaseAdmin:978 - Started disable of report
2015-07-25 15:10:55 INFO  HBaseAdmin:1033 - Disabled report
2015-07-25 15:10:55 INFO  HBaseAdmin:738 - Deleted report

"HBaseService" should have created 1000 ehtries in HBase table "report". Check this with hbase shell:

hbase(main):003:0> scan "report"
row0          column=data:file, timestamp=1437823593865, value=report24.csv-0
row1          column=data:file, timestamp=1437823593897, value=report24.csv-1
row999     column=data:file, timestamp=1437823603565, value=report24.csv-999 
1000 row(s) in 2.0600 seconds

Now we are done with the simple example. Have fun!


  1. Hi, Great.. Tutorial is just awesome..It is really helpful for a newbie like me.. I am a regular follower of your blog. Really very informative post you shared here. Kindly keep blogging. If anyone wants to become a Java developer learn from Java Training in Chennai. or learn thru Java EE Online Training from India . Nowadays Java has tons of job opportunities on various vertical industry.

  2. Excellent post. Very interesting to read. I really love to read such a nice post. Thanks! keep rocking.Hadoop Admin Online Course Hyderabad