Index: service/src/java/org/apache/hadoop/hive/service/HiveServer.java =================================================================== --- service/src/java/org/apache/hadoop/hive/service/HiveServer.java (revision 901519) +++ service/src/java/org/apache/hadoop/hive/service/HiveServer.java (working copy) @@ -25,10 +25,16 @@ import org.apache.commons.logging.Log; import org.apache.commons.logging.LogFactory; import org.apache.hadoop.hive.conf.HiveConf; -import org.apache.hadoop.hive.service.ThriftHive; -import org.apache.hadoop.hive.service.HiveServerException; +import org.apache.hadoop.hive.metastore.HiveMetaStore; +import org.apache.hadoop.hive.metastore.api.MetaException; +import org.apache.hadoop.hive.metastore.api.Schema; +import org.apache.hadoop.hive.ql.Driver; +import org.apache.hadoop.hive.ql.plan.api.QueryPlan; +import org.apache.hadoop.hive.ql.processors.CommandProcessor; +import org.apache.hadoop.hive.ql.processors.CommandProcessorFactory; import org.apache.hadoop.hive.ql.session.SessionState; - +import org.apache.hadoop.mapred.ClusterStatus; +import org.apache.hadoop.mapred.JobTracker; import org.apache.thrift.TException; import org.apache.thrift.TProcessor; import org.apache.thrift.TProcessorFactory; @@ -40,16 +46,6 @@ import org.apache.thrift.transport.TTransport; import org.apache.thrift.transport.TTransportFactory; -import org.apache.hadoop.hive.ql.plan.api.Query; -import org.apache.hadoop.hive.ql.plan.api.QueryPlan; -import org.apache.hadoop.hive.ql.processors.CommandProcessor; -import org.apache.hadoop.hive.ql.processors.CommandProcessorFactory; -import org.apache.hadoop.hive.ql.Driver; -import org.apache.hadoop.hive.metastore.api.*; -import org.apache.hadoop.hive.metastore.*; -import org.apache.hadoop.mapred.ClusterStatus; -import org.apache.hadoop.mapred.JobTracker; - /** * Thrift Hive Server Implementation */ @@ -57,23 +53,18 @@ private final static String VERSION = "0"; /** - * Handler which implements the Hive Interface - * This class can be used in lieu of the HiveClient class - * to get an embedded server + * Handler which implements the Hive Interface This class can be used in lieu + * of the HiveClient class to get an embedded server */ - public static class HiveServerHandler extends HiveMetaStore.HMSHandler implements HiveInterface { + public static class HiveServerHandler extends HiveMetaStore.HMSHandler + implements HiveInterface { /** - * Hive server uses org.apache.hadoop.hive.ql.Driver for run() and + * Hive server uses org.apache.hadoop.hive.ql.Driver for run() and * getResults() methods. */ - private Driver driver; + private final Driver driver; /** - * Stores state per connection - */ - private SessionState session; - - /** * Flag that indicates whether the last executed command was a Hive query */ private boolean isHiveQuery; @@ -97,24 +88,25 @@ /** * Executes a query. - * - * @param cmd HiveQL query to execute + * + * @param cmd + * HiveQL query to execute */ public void execute(String cmd) throws HiveServerException, TException { HiveServerHandler.LOG.info("Running the query: " + cmd); - SessionState ss = SessionState.get(); + SessionState.get(); String cmd_trimmed = cmd.trim(); String[] tokens = cmd_trimmed.split("\\s"); String cmd_1 = cmd_trimmed.substring(tokens[0].length()).trim(); - + int ret = 0; String errorMessage = ""; String SQLState = null; try { CommandProcessor proc = CommandProcessorFactory.get(tokens[0]); - if(proc != null) { + if (proc != null) { if (proc instanceof Driver) { isHiveQuery = true; Driver.DriverResponse response = driver.runCommand(cmd); @@ -133,15 +125,16 @@ } if (ret != 0) { - throw new HiveServerException("Query returned non-zero code: " + ret + - ", cause: " + errorMessage, ret, SQLState); + throw new HiveServerException("Query returned non-zero code: " + ret + + ", cause: " + errorMessage, ret, SQLState); } } /** * Return the status information about the Map-Reduce cluster */ - public HiveClusterStatus getClusterStatus() throws HiveServerException, TException { + public HiveClusterStatus getClusterStatus() throws HiveServerException, + TException { HiveClusterStatus hcs; try { ClusterStatus cs = driver.getClusterStatus(); @@ -150,26 +143,21 @@ // Convert the ClusterStatus to its Thrift equivalent: HiveClusterStatus int state; switch (jbs) { - case INITIALIZING: - state = JobTrackerState.INITIALIZING; - break; - case RUNNING: - state = JobTrackerState.RUNNING; - break; - default: - String errorMsg = "Unrecognized JobTracker state: " + jbs.toString(); - throw new Exception(errorMsg); + case INITIALIZING: + state = JobTrackerState.INITIALIZING; + break; + case RUNNING: + state = JobTrackerState.RUNNING; + break; + default: + String errorMsg = "Unrecognized JobTracker state: " + jbs.toString(); + throw new Exception(errorMsg); } - hcs = new HiveClusterStatus( - cs.getTaskTrackers(), - cs.getMapTasks(), - cs.getReduceTasks(), - cs.getMaxMapTasks(), - cs.getMaxReduceTasks(), + hcs = new HiveClusterStatus(cs.getTaskTrackers(), cs.getMapTasks(), cs + .getReduceTasks(), cs.getMaxMapTasks(), cs.getMaxReduceTasks(), state); - } - catch (Exception e) { + } catch (Exception e) { LOG.error(e.toString()); e.printStackTrace(); HiveServerException ex = new HiveServerException(); @@ -183,9 +171,10 @@ * Return the Hive schema of the query result */ public Schema getSchema() throws HiveServerException, TException { - if (!isHiveQuery) + if (!isHiveQuery) { // Return empty schema if the last command was not a Hive query return new Schema(); + } try { Schema schema = driver.getSchema(); @@ -194,8 +183,7 @@ } LOG.info("Returning schema: " + schema); return schema; - } - catch (Exception e) { + } catch (Exception e) { LOG.error(e.toString()); e.printStackTrace(); HiveServerException ex = new HiveServerException(); @@ -208,9 +196,10 @@ * Return the Thrift schema of the query result */ public Schema getThriftSchema() throws HiveServerException, TException { - if (!isHiveQuery) + if (!isHiveQuery) { // Return empty schema if the last command was not a Hive query return new Schema(); + } try { Schema schema = driver.getThriftSchema(); @@ -219,8 +208,7 @@ } LOG.info("Returning schema: " + schema); return schema; - } - catch (Exception e) { + } catch (Exception e) { LOG.error(e.toString()); e.printStackTrace(); HiveServerException ex = new HiveServerException(); @@ -229,16 +217,17 @@ } } - /** * Fetches the next row in a query result set. - * - * @return the next row in a query result set. null if there is no more row to fetch. + * + * @return the next row in a query result set. null if there is no more row + * to fetch. */ public String fetchOne() throws HiveServerException, TException { - if (!isHiveQuery) + if (!isHiveQuery) { // Return no results if the last command was not a Hive query return ""; + } Vector result = new Vector(); driver.setMaxRows(1); @@ -259,23 +248,27 @@ /** * Fetches numRows rows. - * - * @param numRows Number of rows to fetch. - * @return A list of rows. The size of the list is numRows if there are at least - * numRows rows available to return. The size is smaller than numRows if - * there aren't enough rows. The list will be empty if there is no more - * row to fetch or numRows == 0. - * @throws HiveServerException Invalid value for numRows (numRows < 0) + * + * @param numRows + * Number of rows to fetch. + * @return A list of rows. The size of the list is numRows if there are at + * least numRows rows available to return. The size is smaller than + * numRows if there aren't enough rows. The list will be empty if + * there is no more row to fetch or numRows == 0. + * @throws HiveServerException + * Invalid value for numRows (numRows < 0) */ - public List fetchN(int numRows) throws HiveServerException, TException { + public List fetchN(int numRows) throws HiveServerException, + TException { if (numRows < 0) { HiveServerException ex = new HiveServerException(); ex.setMessage("Invalid argument for number of rows: " + numRows); throw ex; } - if (!isHiveQuery) + if (!isHiveQuery) { // Return no results if the last command was not a Hive query return new Vector(); + } Vector result = new Vector(); driver.setMaxRows(numRows); @@ -291,16 +284,19 @@ /** * Fetches all the rows in a result set. - * - * @return All the rows in a result set of a query executed using execute method. - * - * TODO: Currently the server buffers all the rows before returning them - * to the client. Decide whether the buffering should be done in the client. + * + * @return All the rows in a result set of a query executed using execute + * method. + * + * TODO: Currently the server buffers all the rows before returning + * them to the client. Decide whether the buffering should be done + * in the client. */ public List fetchAll() throws HiveServerException, TException { - if (!isHiveQuery) + if (!isHiveQuery) { // Return no results if the last command was not a Hive query return new Vector(); + } Vector rows = new Vector(); Vector result = new Vector(); @@ -341,8 +337,7 @@ // will be returned in a single QueryPlan try { qp.addToQueries(driver.getQueryPlan()); - } - catch (Exception e) { + } catch (Exception e) { HiveServerException ex = new HiveServerException(); ex.setMessage(e.toString()); throw ex; @@ -353,10 +348,11 @@ } public static class ThriftHiveProcessorFactory extends TProcessorFactory { - public ThriftHiveProcessorFactory (TProcessor processor) { + public ThriftHiveProcessorFactory(TProcessor processor) { super(processor); } + @Override public TProcessor getProcessor(TTransport trans) { try { Iface handler = new HiveServerHandler(); Index: service/src/java/org/apache/hadoop/hive/service/HiveClient.java =================================================================== --- service/src/java/org/apache/hadoop/hive/service/HiveClient.java (revision 901519) +++ service/src/java/org/apache/hadoop/hive/service/HiveClient.java (working copy) @@ -18,31 +18,11 @@ package org.apache.hadoop.hive.service; -import org.apache.commons.logging.Log; -import org.apache.commons.logging.LogFactory; -import org.apache.hadoop.conf.Configuration; -import org.apache.hadoop.fs.Path; -import org.apache.hadoop.hive.conf.HiveConf; -import org.apache.hadoop.hive.service.ThriftHive; -import org.apache.hadoop.hive.service.ThriftHive.*; -import org.apache.hadoop.hive.service.HiveServerException; -import org.apache.hadoop.hive.serde2.Deserializer; -import org.apache.hadoop.hive.serde2.SerDeException; -import org.apache.hadoop.util.ReflectionUtils; -import org.apache.hadoop.util.StringUtils; - -import com.facebook.fb303.FacebookBase; -import com.facebook.fb303.FacebookService; -import com.facebook.fb303.fb_status; -import org.apache.thrift.TException; import org.apache.thrift.protocol.TProtocol; -import org.apache.hadoop.hive.metastore.api.*; - /** - * Thrift Hive Client - * Just an empty class that can be used to run queries - * on a stand alone hive server + * Thrift Hive Client Just an empty class that can be used to run queries on a + * stand alone hive server */ public class HiveClient extends ThriftHive.Client implements HiveInterface { public HiveClient(TProtocol prot) { Index: service/src/java/org/apache/hadoop/hive/service/HiveInterface.java =================================================================== --- service/src/java/org/apache/hadoop/hive/service/HiveInterface.java (revision 901519) +++ service/src/java/org/apache/hadoop/hive/service/HiveInterface.java (working copy) @@ -20,15 +20,17 @@ /** * HiveInterface extends 2 interfaces, ThriftHive and ThriftHiveMetastore. - * - * ThriftHive.Iface is defined in: - * service/src/gen-javabean/org/apache/hadoop/hive/service/ThriftHive.java - * ThriftHiveMetastore.Iface is defined in: - * metastore/src/gen-javabean/org/apache/hadoop/hive/metastore/api/ThriftHiveMetastore.java - * + * + * ThriftHive.Iface is defined in: + * service/src/gen-javabean/org/apache/hadoop/hive/service/ThriftHive.java + * ThriftHiveMetastore.Iface is defined in: + * metastore/src/gen-javabean/org/apache + * /hadoop/hive/metastore/api/ThriftHiveMetastore.java + * * These interfaces are generated by Thrift. The thrift files are in: - * ThriftHive: service/if/hive_service.thrift - * ThriftHiveMetastore: metastore/if/hive_metastore.thrift + * ThriftHive: service/if/hive_service.thrift ThriftHiveMetastore: + * metastore/if/hive_metastore.thrift */ -public interface HiveInterface extends ThriftHive.Iface, org.apache.hadoop.hive.metastore.api.ThriftHiveMetastore.Iface { +public interface HiveInterface extends ThriftHive.Iface, + org.apache.hadoop.hive.metastore.api.ThriftHiveMetastore.Iface { }