/** Return the status information about the Map-Reduce cluster */ public HiveClusterStatus getClusterStatus() throws HiveServerException, TException { HiveClusterStatus hcs; try { ClusterStatus cs = driver.getClusterStatus(); JobTracker.State jbs = cs.getJobTrackerState(); // 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); } hcs = new HiveClusterStatus( cs.getTaskTrackers(), cs.getMapTasks(), cs.getReduceTasks(), cs.getMaxMapTasks(), cs.getMaxReduceTasks(), state); } catch (Exception e) { LOG.error(e.toString()); e.printStackTrace(); throw new HiveServerException("Unable to get cluster status: " + e.toString()); } return hcs; }
/** * Executes a query. * * @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(); String cmd_trimmed = cmd.trim(); String[] tokens = cmd_trimmed.split("\\s"); String cmd_1 = cmd_trimmed.substring(tokens[0].length()).trim(); int ret = 0; try { CommandProcessor proc = CommandProcessorFactory.get(tokens[0]); if (proc != null) { if (proc instanceof Driver) { isHiveQuery = true; ret = driver.run(cmd); } else { isHiveQuery = false; ret = proc.run(cmd_1); } } } catch (Exception e) { throw new HiveServerException("Error running query: " + e.toString()); } if (ret != 0) { throw new HiveServerException("Query returned non-zero code: " + ret); } }
/** Return the Thrift schema of the query result */ public Schema getThriftSchema() throws HiveServerException, TException { if (!isHiveQuery) // Return empty schema if the last command was not a Hive query return new Schema(); try { Schema schema = driver.getThriftSchema(); if (schema == null) { schema = new Schema(); } LOG.info("Returning schema: " + schema); return schema; } catch (Exception e) { LOG.error(e.toString()); e.printStackTrace(); throw new HiveServerException("Unable to get schema: " + e.toString()); } }