diff --git a/beeline/src/java/org/apache/hive/beeline/BeeLine.java b/beeline/src/java/org/apache/hive/beeline/BeeLine.java index 1e4759b..4aff051 100644 --- a/beeline/src/java/org/apache/hive/beeline/BeeLine.java +++ b/beeline/src/java/org/apache/hive/beeline/BeeLine.java @@ -695,6 +695,7 @@ int initArgsFromCliVars(String[] args) { if (!commands.isEmpty()) { embeddedConnect(); connectDBInEmbededMode(); + updateOptsForCli(); for (Iterator i = commands.iterator(); i.hasNext(); ) { String command = i.next().toString(); debug(loc("executing-command", command)); @@ -811,6 +812,13 @@ private String obtainPasswordFromFile(String passwordFilePath) { } } + private void updateOptsForCli() { + getOpts().updateBeeLineOptsFromConf(); + getOpts().setShowHeader(false); + getOpts().setOutputFormat("dsv"); + getOpts().setDelimiterForDSV(' '); + } + /** * Start accepting input from stdin, and dispatch it * to the appropriate {@link CommandHandler} until the @@ -836,10 +844,7 @@ public int begin(String[] args, InputStream inputStream) throws IOException { return code; } defaultConnect(false); - getOpts().updateBeeLineOptsFromConf(); - getOpts().setShowHeader(false); - getOpts().setOutputFormat("dsv"); - getOpts().setDelimiterForDSV(' '); + updateOptsForCli(); processInitFiles(opts.getInitFiles()); } @@ -958,6 +963,8 @@ private int execute(ConsoleReader reader, boolean exitOnError) { // trim line line = (line == null) ? null : line.trim(); + line = cliToBeelineCmd(line); + if (!dispatch(line) && exitOnError) { return ERRNO_OTHER; } @@ -1063,6 +1070,20 @@ void usage() { return cmd.split("\\s+"); } + private String cliToBeelineCmd(String cmd) { + if (isBeeLine) { + // For beeline, do nothing + return cmd; + } + if (cmd != null && cmd.startsWith("!")) { + String shell_cmd = cmd.substring(1); + return "!sh " + shell_cmd; + } else { + // command like dfs + return cmd; + } + } + /** * Dispatch the specified line to the appropriate {@link CommandHandler}. * diff --git a/beeline/src/test/org/apache/hive/beeline/cli/TestHiveCli.java b/beeline/src/test/org/apache/hive/beeline/cli/TestHiveCli.java index 088f1cd..6477339 100644 --- a/beeline/src/test/org/apache/hive/beeline/cli/TestHiveCli.java +++ b/beeline/src/test/org/apache/hive/beeline/cli/TestHiveCli.java @@ -95,14 +95,19 @@ public void verifyCMD(String CMD, String keywords, OutputStream os, String[] opt output.contains(keywords)); } else { Assert.assertFalse( - "The expected keyword \"" + keywords + "\" doesn't occur in the output: " + output, + "The expected keyword \"" + keywords + "\" are't occur in the output: " + output, output.contains(keywords)); } } @Test public void testInValidCmd() { - verifyCMD("!lss\n", "Unknown command: lss", errS, null, ERRNO_OK, true); + verifyCMD("!lss\n", "Failed to execute lss", errS, null, ERRNO_OK, true); + } + + @Test + public void testCmd() { + verifyCMD("!ls\n", "src", os, null, ERRNO_OK, true); } @Test @@ -192,8 +197,7 @@ public void testVariablesForSource() { @Test public void testErrOutput() { - verifyCMD( - "show tables;set system:xxx=5;set system:yyy=${system:xxx};\nlss;", + verifyCMD("show tables;set system:xxx=5;set system:yyy=${system:xxx};\nlss;", "cannot recognize input near 'lss' '' ''", errS, null, ERRNO_OK, true); } @@ -224,6 +228,12 @@ public void testUseInvalidDB() { "hive (invalidDB)>", os, null, ERRNO_OK, false); } + @Test + public void testNoErrorDB() { + verifyCMD(null, "Error: Method not supported (state=,code=0)", errS, new String[] { "-e", "show tables;" }, + ERRNO_OK, false); + } + private void redirectOutputStream() { // Setup output stream to redirect output to os = new ByteArrayOutputStream();