/** * 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); } }
private void checkedCompile() throws BeeswaxException { // Run through configuration commands for (String cmd : query.configuration) { // This is pretty whacky; SET and ADD get treated differently // than CREATE TEMPORARY FUNCTION... The trimming logic // here is borrowed from CliDriver; oy. String cmd_trimmed = cmd.trim(); String[] tokens = cmd_trimmed.split("\\s+"); String cmd1 = cmd_trimmed.substring(tokens[0].length()).trim(); CommandProcessor p = CommandProcessorFactory.get(tokens[0]); int res; if (p instanceof Driver) { res = p.run(cmd).getResponseCode(); } else { res = p.run(cmd1).getResponseCode(); } if (res != 0) { throwException(new RuntimeException(getErrorStreamAsString())); } } // Note that driver.compile() talks to HDFS, so it's // not as quick as one might think. int compileRes = driver.compile(query.query); if (compileRes != 0) { throwException( new BeeswaxException(getErrorStreamAsString(), this.logContext.getName(), this.handle)); } }
public int processCmd(String cmd) throws TException, HiveException { SessionState ss = SessionState.get(); ss.setiscli(true); String cmd_trimmed = cmd.trim(); String[] tokens = cmd_trimmed.split("\\s+"); String cmd_1 = cmd_trimmed.substring(tokens[0].length()).trim(); int ret = 0; if (tokens[0].equalsIgnoreCase("delete")) { Vector<String> nexttoken = new Vector<String>(); nexttoken.add("jar"); nexttoken.add("file"); nexttoken.add("from"); if (tokens.length < 2 || !nexttoken.contains(tokens[1].toLowerCase())) { String errorMessage = "\nif delete resource:\n" + "Usage: delete [FILE|JAR] <value> [<value>]*\n" + "if delete table rows:\n" + "Usage: delete from tableName [where searchCondition]"; console.printError(errorMessage); ret = 1; return ret; } } if (tokens[0].equalsIgnoreCase("dfs") || tokens[0].equalsIgnoreCase("zktest")) { String errorMessage = "\ntdw hive do not support " + tokens[0].toLowerCase() + " operation\n"; throw new HiveException(errorMessage); } if (cmd_trimmed.toLowerCase().equals("quit") || cmd_trimmed.toLowerCase().equals("exit")) { System.exit(0); } else if (cmd_trimmed.startsWith("!")) { String shell_cmd = cmd_trimmed.substring(1); try { Process executor = Runtime.getRuntime().exec(shell_cmd); StreamPrinter outPrinter = new StreamPrinter(executor.getInputStream(), null, ss.out); StreamPrinter errPrinter = new StreamPrinter(executor.getErrorStream(), null, ss.err); outPrinter.start(); errPrinter.start(); ret = executor.waitFor(); if (ret != 0) { console.printError("Command failed with exit code = " + ret); } } catch (Exception e) { console.printError( "Exception raised from Shell command " + e.getLocalizedMessage(), org.apache.hadoop.util.StringUtils.stringifyException(e)); ret = 1; } } else if (tokens[0].toLowerCase().equals("list")) { SessionState.ResourceType t; if (tokens.length < 2 || (t = SessionState.find_resource_type(tokens[1])) == null) { console.printError( "Usage: list [" + StringUtils.join(SessionState.ResourceType.values(), "|") + "] [<value> [<value>]*]"); ret = 1; } else { List<String> filter = null; if (tokens.length >= 3) { System.arraycopy(tokens, 2, tokens, 0, tokens.length - 2); filter = Arrays.asList(tokens); } Set<String> s = ss.list_resource(t, filter); if (s != null && !s.isEmpty()) ss.out.println(StringUtils.join(s, "\n")); } } else { CommandProcessor proc = CommandProcessorFactory.get(tokens); if (proc != null) { if (proc instanceof Driver) { Driver qp = (Driver) proc; PrintStream out = ss.out; long start = System.currentTimeMillis(); try { ret = qp.run(cmd); } catch (Exception e1) { e1.printStackTrace(); } if (ret != 0) { qp.close(); return ret; } Vector<String> res = new Vector<String>(); try { while (qp.getResults(res)) { for (String r : res) { out.println(r); } res.clear(); if (out.checkError()) { break; } } } catch (IOException e) { console.printError( "Failed with exception " + e.getClass().getName() + ":" + e.getMessage(), "\n" + org.apache.hadoop.util.StringUtils.stringifyException(e)); ret = 1; } int cret = qp.close(); if (ret == 0) { ret = cret; } long end = System.currentTimeMillis(); if (end > start) { double timeTaken = (double) (end - start) / 1000.0; console.printInfo("Time taken: " + timeTaken + " seconds", null); } } else { try { ret = proc.run(cmd_1); } catch (Exception e) { e.printStackTrace(); } } } } return ret; }