Query HBase from Tomcat

We installed Tomcat 6.0.39 (we weren’t running JDK 1.7), as well as hbase. To get this to work, we copied the following jar’s from the hadoop and hbase installation directories to the tomcat lib directory.

-rw-r--r-- 1 serengeti serengeti 5212641 Mar 11 19:28 hbase-0.94.10.jar
-rw-r--r-- 1 serengeti serengeti 2536366 Mar 11 19:28 hbase-0.94.10-tests.jar
-rw-rw-r-- 1 serengeti serengeti 4203147 Mar 11 19:37 hadoop-core-1.2.1.jar
-rw-r--r-- 1 serengeti serengeti    1521 Mar 11 19:41 hbase-site.xml
-rw-rw-r-- 1 serengeti serengeti   60686 Mar 11 19:48 commons-logging-1.1.1.jar
-rw-rw-r-- 1 serengeti serengeti  261809 Mar 11 19:49 commons-lang-2.4.jar
-rw-rw-r-- 1 serengeti serengeti  298829 Mar 11 19:50 commons-configuration-1.6.jar
-rw-r--r-- 1 serengeti serengeti  779974 Mar 11 19:52 zookeeper-3.4.5.jar
-rw-r--r-- 1 serengeti serengeti   15345 Mar 11 19:53 slf4j-api-1.4.3.jar
-rw-r--r-- 1 serengeti serengeti    8601 Mar 11 19:54 slf4j-log4j12-1.4.3.jar
-rw-r--r-- 1 serengeti serengeti  481535 Mar 11 19:54 log4j-1.2.16.jar
-rw-r--r-- 1 serengeti serengeti  449818 Mar 11 19:55 protobuf-java-2.4.0a.jar

We also copied the hbase-site.xml file from the hbase installation into the tomcat lib directory.

We then create our table in HBase…

hbase(main):006:0> create 'stores', 'f'
0 row(s) in 1.4600 seconds

hbase(main):008:0> put 'stores', '12345', 'f:lat', '10.0'
0 row(s) in 0.2490 seconds

hbase(main):018:0> put 'stores', '12345', 'f:lon', '20.0'
0 row(s) in 0.0060 seconds

hbase(main):019:0> scan 'stores'
ROW                                         COLUMN+CELL
 12345                                      column=f:lat, timestamp=1394570512251, value=10.0
 12345                                      column=f:lon, timestamp=1394570720417, value=20.0
1 row(s) in 0.0280 seconds

…and finally, create the following JSP…

<%@page import="java.util.*"%>
<%@page import="org.apache.hadoop.hbase.client.*"%>
<%@page import="org.apache.hadoop.hbase.util.*"%>
<%@page import="org.apache.hadoop.hbase.*"%>
<%@page import="org.apache.hadoop.conf.*;"%>

<%
  Configuration conf = HBaseConfiguration.create();
  HTable hTable = new HTable(conf, "stores");  //this is your table name

  byte[] rowId = null;
  byte[] famA = Bytes.toBytes("f");  //this is your column family
  byte[] col1 = Bytes.toBytes("lat"); //this is your on the fly column name in the table
  byte[] col2 = Bytes.toBytes("lon"); //this is your on the fly column name in the table

  for (int j = 9; j < 10; j++) {  //each thread loops 1000 times
    rowId = Bytes.toBytes(Integer.toString(12345));
    Get get = new Get(rowId);
    Result result = hTable.get(get);
    byte[] value = result.getValue(famA, col1);
    out.println(j);
    out.println(Bytes.toString(value));
    value = result.getValue(famA, col2);
    out.println(j);
    out.println(Bytes.toString(value));
  }

%>

This should “just work”, so please reply in the comments section if you have issues.

Leave a Reply

Your email address will not be published. Required fields are marked *

This site uses Akismet to reduce spam. Learn how your comment data is processed.