Spring for Apache Hadoop

org.springframework.data.hadoop.hive
Class HiveTemplate

java.lang.Object
  extended by org.springframework.data.hadoop.hive.HiveTemplate
All Implemented Interfaces:
org.springframework.beans.factory.Aware, org.springframework.beans.factory.InitializingBean, org.springframework.context.ResourceLoaderAware, HiveOperations

public class HiveTemplate
extends java.lang.Object
implements org.springframework.beans.factory.InitializingBean, HiveOperations, org.springframework.context.ResourceLoaderAware

Helper class that simplifies Hive data access code. Automatically handles the creation of a HiveClient (which is non-thread-safe) and converts Hive exceptions into DataAccessExceptions.

Author:
Costin Leau

Constructor Summary
HiveTemplate()
          Constructs a new HiveClient instance.
HiveTemplate(HiveClientFactory hiveClientFactory)
          Constructs a new HiveTemplate instance.
 
Method Summary
 void afterPropertiesSet()
           
protected  org.springframework.dao.DataAccessException convertHiveAccessException(java.lang.Exception ex)
          Converts the given Hive exception to an appropriate exception from the org.springframework.dao hierarchy.
protected  org.apache.hadoop.hive.service.HiveClient createHiveClient()
           
<T> T
execute(HiveClientCallback<T> action)
          Executes the action specified by the given callback object within an active HiveClient.
 java.util.List<java.lang.String> executeScript(HiveScript script)
          Executes a Hive script.
 java.util.List<java.lang.String> executeScript(java.lang.Iterable<HiveScript> scripts)
          Executes multiple Hive scripts.
 java.util.List<java.lang.String> query(java.lang.String query)
          Executes the given HiveQL that results in a list of objects.
 java.util.List<java.lang.String> query(java.lang.String query, java.util.Map<?,?> arguments)
          Executes the given HiveQL using the list of arguments, expecting a list of objects.
 java.lang.Integer queryForInt(java.lang.String query)
          Executes the given HiveQL that results in a single int value.
 java.lang.Integer queryForInt(java.lang.String query, java.util.Map<?,?> arguments)
          Executes the given HiveQL using the list of arguments, that results in a single int value.
 java.lang.Long queryForLong(java.lang.String query)
          Executes the given HiveQL that results in a single long value.
 java.lang.Long queryForLong(java.lang.String query, java.util.Map<?,?> arguments)
          Executes the given HiveQL using the list of arguments, that results in a single long value.
 java.lang.String queryForString(java.lang.String query)
          Executes the given HiveQL that results in a single object.
 java.lang.String queryForString(java.lang.String query, java.util.Map<?,?> arguments)
          Executes the given HiveQL using the list of arguments, that results in a single object.
 void setHiveClientFactory(HiveClientFactory hiveClientFactory)
          Sets the HiveClient factory.
 void setResourceLoader(org.springframework.core.io.ResourceLoader resourceLoader)
           
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Constructor Detail

HiveTemplate

public HiveTemplate()
Constructs a new HiveClient instance. Expects setHiveClientFactory(HiveClientFactory) to be called before using it.


HiveTemplate

public HiveTemplate(HiveClientFactory hiveClientFactory)
Constructs a new HiveTemplate instance.

Parameters:
hiveClientFactory - HiveClient factory
Method Detail

afterPropertiesSet

public void afterPropertiesSet()
Specified by:
afterPropertiesSet in interface org.springframework.beans.factory.InitializingBean

execute

public <T> T execute(HiveClientCallback<T> action)
          throws org.springframework.dao.DataAccessException
Executes the action specified by the given callback object within an active HiveClient.

Specified by:
execute in interface HiveOperations
Parameters:
action - callback object that specifies the Hive action
Returns:
the action result object
Throws:
org.springframework.dao.DataAccessException

convertHiveAccessException

protected org.springframework.dao.DataAccessException convertHiveAccessException(java.lang.Exception ex)
Converts the given Hive exception to an appropriate exception from the org.springframework.dao hierarchy.

Parameters:
ex - hive exception
Returns:
a corresponding DataAccessException

query

public java.util.List<java.lang.String> query(java.lang.String query)
                                       throws org.springframework.dao.DataAccessException
Executes the given HiveQL that results in a list of objects. The script is interpreted as a URL or if that fails, as a HiveQL statement.

Specified by:
query in interface HiveOperations
Parameters:
query - HiveQL
Returns:
list of values returned by the query
Throws:
org.springframework.dao.DataAccessException

query

public java.util.List<java.lang.String> query(java.lang.String query,
                                              java.util.Map<?,?> arguments)
                                       throws org.springframework.dao.DataAccessException
Executes the given HiveQL using the list of arguments, expecting a list of objects. The script is interpreted as a URL or if that fails, as a HiveQL statement.

Specified by:
query in interface HiveOperations
Parameters:
query - HiveQL
arguments - query arguments
Returns:
list of values returned by the query
Throws:
org.springframework.dao.DataAccessException

queryForString

public java.lang.String queryForString(java.lang.String query)
                                throws org.springframework.dao.DataAccessException
Executes the given HiveQL that results in a single object. The script is interpreted as a URL or if that fails, as a HiveQL statement.

Specified by:
queryForString in interface HiveOperations
Parameters:
query - HiveQL
Returns:
query result
Throws:
org.springframework.dao.DataAccessException

queryForString

public java.lang.String queryForString(java.lang.String query,
                                       java.util.Map<?,?> arguments)
                                throws org.springframework.dao.DataAccessException
Executes the given HiveQL using the list of arguments, that results in a single object. The script is interpreted as a URL or if that fails, as a HiveQL statement.

Specified by:
queryForString in interface HiveOperations
Parameters:
query - HiveQL
arguments - query arguments
Returns:
query result
Throws:
org.springframework.dao.DataAccessException

queryForInt

public java.lang.Integer queryForInt(java.lang.String query)
                              throws org.springframework.dao.DataAccessException
Executes the given HiveQL that results in a single int value. The script is interpreted as a URL or if that fails, as a HiveQL statement.

Specified by:
queryForInt in interface HiveOperations
Parameters:
query - HiveQL
Returns:
query int result
Throws:
org.springframework.dao.DataAccessException

queryForInt

public java.lang.Integer queryForInt(java.lang.String query,
                                     java.util.Map<?,?> arguments)
                              throws org.springframework.dao.DataAccessException
Executes the given HiveQL using the list of arguments, that results in a single int value. The script is interpreted as a URL or if that fails, as a HiveQL statement.

Specified by:
queryForInt in interface HiveOperations
Parameters:
query - HiveQL
arguments - query arguments
Returns:
query int result
Throws:
org.springframework.dao.DataAccessException

queryForLong

public java.lang.Long queryForLong(java.lang.String query)
                            throws org.springframework.dao.DataAccessException
Executes the given HiveQL that results in a single long value. The script is interpreted as a URL or if that fails, as a HiveQL statement.

Specified by:
queryForLong in interface HiveOperations
Parameters:
query - HiveQL
Returns:
query long result
Throws:
org.springframework.dao.DataAccessException

queryForLong

public java.lang.Long queryForLong(java.lang.String query,
                                   java.util.Map<?,?> arguments)
                            throws org.springframework.dao.DataAccessException
Executes the given HiveQL using the list of arguments, that results in a single long value. The script is interpreted as a URL or if that fails, as a HiveQL statement.

Specified by:
queryForLong in interface HiveOperations
Parameters:
query - HiveQL
arguments - query arguments
Returns:
query long result
Throws:
org.springframework.dao.DataAccessException

executeScript

public java.util.List<java.lang.String> executeScript(HiveScript script)
                                               throws org.springframework.dao.DataAccessException
Executes a Hive script.

Specified by:
executeScript in interface HiveOperations
Parameters:
script - script resource and arguments
Returns:
script result
Throws:
org.springframework.dao.DataAccessException

executeScript

public java.util.List<java.lang.String> executeScript(java.lang.Iterable<HiveScript> scripts)
                                               throws org.springframework.dao.DataAccessException
Executes multiple Hive scripts.

Specified by:
executeScript in interface HiveOperations
Parameters:
scripts - scripts resources and arguments
Returns:
scripts results
Throws:
org.springframework.dao.DataAccessException

createHiveClient

protected org.apache.hadoop.hive.service.HiveClient createHiveClient()

setHiveClientFactory

public void setHiveClientFactory(HiveClientFactory hiveClientFactory)
Sets the HiveClient factory.

Parameters:
hiveClientFactory - hive client factory to set

setResourceLoader

public void setResourceLoader(org.springframework.core.io.ResourceLoader resourceLoader)
Specified by:
setResourceLoader in interface org.springframework.context.ResourceLoaderAware

Spring for Apache Hadoop