diff --git a/beeline/src/java/org/apache/hive/beeline/BeeLine.java b/beeline/src/java/org/apache/hive/beeline/BeeLine.java index 7c53997..610c8f0 100644 --- a/beeline/src/java/org/apache/hive/beeline/BeeLine.java +++ b/beeline/src/java/org/apache/hive/beeline/BeeLine.java @@ -87,6 +87,7 @@ import org.apache.commons.cli.OptionBuilder; import org.apache.commons.cli.Options; import org.apache.commons.cli.ParseException; +import org.apache.hadoop.hive.conf.HiveConf; import org.apache.hadoop.io.IOUtils; import org.apache.hive.beeline.cli.CliOptionsProcessor; import org.apache.hive.jdbc.Utils; @@ -1079,7 +1080,7 @@ boolean dispatch(String line) { boolean needsUpdate = isConfNeedsUpdate(line); boolean res = commands.sql(line, getOpts().getEntireLineAsCommand()); if (needsUpdate) { - getOpts().setHiveConf(getCommands().getHiveConf(true)); + getOpts().setHiveConf(getCommands().getHiveConf(false)); } return res; } @@ -1351,19 +1352,35 @@ void showWarnings(SQLWarning warn) { } } - String getPrompt() { + if (isBeeLine) { + return getPromptForBeeline(); + } else { + return getPromptForCli(); + } + } + + String getPromptForCli() { + String prompt; + // read prompt configuration and substitute variables. + HiveConf conf = getCommands().getHiveConf(true); + prompt = conf.getVar(HiveConf.ConfVars.CLIPROMPT); + prompt = getCommands().substituteVariables(conf, prompt); + return prompt + "> "; + } + + String getPromptForBeeline() { if (getDatabaseConnection() == null || getDatabaseConnection().getUrl() == null) { return "beeline> "; } else { String printClosed = getDatabaseConnection().isClosed() ? " (closed)" : ""; - return getPrompt(getDatabaseConnections().getIndex() + return getPromptForBeeline(getDatabaseConnections().getIndex() + ": " + getDatabaseConnection().getUrl()) + printClosed + "> "; } } - static String getPrompt(String url) { + static String getPromptForBeeline(String url) { if (url == null || url.length() == 0) { url = "beeline"; } diff --git a/beeline/src/java/org/apache/hive/beeline/BeeLineOpts.java b/beeline/src/java/org/apache/hive/beeline/BeeLineOpts.java index 894f74f..0a86c24 100644 --- a/beeline/src/java/org/apache/hive/beeline/BeeLineOpts.java +++ b/beeline/src/java/org/apache/hive/beeline/BeeLineOpts.java @@ -117,7 +117,7 @@ public BeeLineOpts(BeeLine beeLine, Properties props) { public String[] possibleSettingValues() { List vals = new LinkedList(); - vals.addAll(Arrays.asList(new String[] {"yes", "no"})); + vals.addAll(Arrays.asList(new String[] { "yes", "no" })); return vals.toArray(new String[vals.size()]); } @@ -538,5 +538,9 @@ public char getDelimiterForDSV() { public void setDelimiterForDSV(char delimiterForDSV) { this.delimiterForDSV = delimiterForDSV; } + + public HiveConf getConf() { + return conf; + } } diff --git a/beeline/src/java/org/apache/hive/beeline/Commands.java b/beeline/src/java/org/apache/hive/beeline/Commands.java index b07388a..8c406a3 100644 --- a/beeline/src/java/org/apache/hive/beeline/Commands.java +++ b/beeline/src/java/org/apache/hive/beeline/Commands.java @@ -737,6 +737,15 @@ public boolean sql(String line) { * @return the hive configuration from server side */ public HiveConf getHiveConf(boolean call) { + HiveConf hiveConf = beeLine.getOpts().getConf(); + if (hiveConf != null && call) { + return hiveConf; + } else { + return getHiveConfHelper(call); + } + } + + public HiveConf getHiveConfHelper(boolean call) { HiveConf conf = new HiveConf(); BufferedRows rows = getConfInternal(call); while (rows != null && rows.hasNext()) { @@ -1015,7 +1024,7 @@ public boolean sql(String line, boolean entireLineAsCommand) { return execute(line, false, entireLineAsCommand); } - private String substituteVariables(HiveConf conf, String line) { + public String substituteVariables(HiveConf conf, String line) { if (!beeLine.isBeeLine()) { // Substitution is only supported in non-beeline mode. return new VariableSubstitution(new HiveVariableSource() { 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 fa94c89..fc8ed0c 100644 --- a/beeline/src/test/org/apache/hive/beeline/cli/TestHiveCli.java +++ b/beeline/src/test/org/apache/hive/beeline/cli/TestHiveCli.java @@ -78,7 +78,8 @@ private void verifyCMD(String CMD, String keywords, OutputStream os, String[] op int retCode) { executeCMD(options, CMD, retCode); String output = os.toString(); - Assert.assertTrue("The expected keyword doesn't occur in the output: " + output, + LOG.debug(output); + Assert.assertTrue("The expected keyword " + keywords + "doesn't occur in the output: " + output, output.contains(keywords)); } @@ -88,6 +89,11 @@ public void testInValidCmd() { } @Test + public void testSetPromptValue() { + verifyCMD("set hive.cli.prompt=MYCLI;SHOW\nTABLES;", "MYCLI> ", os, null, ERRNO_OK); + } + + @Test public void testHelp() { verifyCMD(null, "usage: hive", os, new String[] { "-H" }, ERRNO_ARGS); }