org.springframework.data.hadoop.hive
Class HiveTemplate

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

public class HiveTemplate
extends Object
implements InitializingBean, HiveOperations, 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  DataAccessException convertHiveAccessException(Exception ex)
          Converts the given Hive exception to an appropriate exception from the org.springframework.dao hierarchy.
protected  HiveClient createHiveClient()
           
<T> T
execute(HiveClientCallback<T> action)
          Executes the action specified by the given callback object within an active HiveClient.
 List<String> executeScript(HiveScript script)
          Executes a Hive script.
 List<String> executeScript(Iterable<HiveScript> scripts)
          Executes multiple Hive scripts.
 List<String> query(String query)
          Executes the given HiveQL that results in a list of objects.
 List<String> query(String query, Map<?,?> arguments)
          Executes the given HiveQL using the list of arguments, expecting a list of objects.
 Integer queryForInt(String query)
          Executes the given HiveQL that results in a single int value.
 Integer queryForInt(String query, Map<?,?> arguments)
          Executes the given HiveQL using the list of arguments, that results in a single int value.
 Long queryForLong(String query)
          Executes the given HiveQL that results in a single long value.
 Long queryForLong(String query, Map<?,?> arguments)
          Executes the given HiveQL using the list of arguments, that results in a single long value.
 String queryForString(String query)
          Executes the given HiveQL that results in a single object.
 String queryForString(String query, 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(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 InitializingBean

execute

public <T> T execute(HiveClientCallback<T> action)
          throws 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:
DataAccessException

convertHiveAccessException

protected DataAccessException convertHiveAccessException(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 List<String> query(String query)
                   throws 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:
DataAccessException

query

public List<String> query(String query,
                          Map<?,?> arguments)
                   throws 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:
DataAccessException

queryForString

public String queryForString(String query)
                      throws 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:
DataAccessException

queryForString

public String queryForString(String query,
                             Map<?,?> arguments)
                      throws 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:
DataAccessException

queryForInt

public Integer queryForInt(String query)
                    throws 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:
DataAccessException

queryForInt

public Integer queryForInt(String query,
                           Map<?,?> arguments)
                    throws 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:
DataAccessException

queryForLong

public Long queryForLong(String query)
                  throws 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:
DataAccessException

queryForLong

public Long queryForLong(String query,
                         Map<?,?> arguments)
                  throws 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:
DataAccessException

executeScript

public List<String> executeScript(HiveScript script)
                           throws DataAccessException
Executes a Hive script.

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

executeScript

public List<String> executeScript(Iterable<HiveScript> scripts)
                           throws DataAccessException
Executes multiple Hive scripts.

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

createHiveClient

protected HiveClient createHiveClient()

setHiveClientFactory

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

Parameters:
hiveClientFactory - hive client factory to set

setResourceLoader

public void setResourceLoader(ResourceLoader resourceLoader)
Specified by:
setResourceLoader in interface ResourceLoaderAware