Querying Hive from Tomcat

Below is a very simple example to get started on how Hive can be queried from a web front end.

The idea behind this would be to present the user with a csv file mime type that would be opened in Excel.

Given the long start up time on Hive queries (they are Map Reduce jobs “under the hood”), it may be a better idea to run the Hive query (map reduce job “under the hood”) and store the intermediate results in a database such as MySQL and query that.

Conversely, use the newest Impala release from Cloudera.

As such, to a large extent, this is simply a learning exercise.

Be sure to copy all *.jar files from your HIVE_INSTALLATION/lib to the common/lib directory under tomcat before running this.

<%@page import="java.sql.*;"%>

<%
try {
  Class.forName("org.apache.hadoop.hive.jdbc.HiveDriver");
  Connection con = DriverManager.getConnection("jdbc:hive://192.168.3.50:10000/default", "", "");
  Statement stmt = con.createStatement();
  ResultSet res = stmt.executeQuery("select count(*) from requests");
  while (res.next()) {
    out.println(res.getInt(1) + "
"); } stmt.close(); con.close(); } catch (Exception e) { e.printStackTrace(); out.println(e.getMessage()); } %>

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.