diff --git a/beeline/src/java/org/apache/hive/beeline/BeeLine.java b/beeline/src/java/org/apache/hive/beeline/BeeLine.java index ecd60f6..7be7ccc 100644 --- a/beeline/src/java/org/apache/hive/beeline/BeeLine.java +++ b/beeline/src/java/org/apache/hive/beeline/BeeLine.java @@ -82,11 +82,9 @@ import jline.console.history.History; import jline.console.history.FileHistory; -import org.apache.commons.cli.CommandLine; -import org.apache.commons.cli.GnuParser; +import org.apache.commons.cli.*; import org.apache.commons.cli.OptionBuilder; import org.apache.commons.cli.Options; -import org.apache.commons.cli.ParseException; import org.apache.hadoop.fs.FSDataInputStream; import org.apache.hadoop.fs.FileSystem; import org.apache.hadoop.hive.conf.HiveConf; @@ -316,11 +314,13 @@ .create('n')); // -p - options.addOption(OptionBuilder - .hasArg() - .withArgName("password") - .withDescription("the password to connect as") - .create('p')); + Option passOption = OptionBuilder + .hasArg() + .withArgName("password") + .withDescription("the password to connect as") + .create('p'); + passOption.setOptionalArg(true); + options.addOption(passOption); // -w (or) --password-file options.addOption(OptionBuilder @@ -388,6 +388,164 @@ .withLongOpt("property-file") .withDescription("the file to read configuration properties from") .create()); + + // --force=true/false + options.addOption(OptionBuilder + .hasArg() + .withArgName("=value") + .withLongOpt("force") + .withDescription("whether continue run when errors occur") + .create()); + + // --color=true/false + options.addOption(OptionBuilder + .hasArg() + .withLongOpt("color") + .withDescription("whether color is used for display") + .create()); + + // --showHeader=true/false + options.addOption(OptionBuilder + .hasArg() + .withLongOpt("showHeader") + .withDescription("whether show column names in query results") + .create()); + + // --headerInterval=ROWS + options.addOption(OptionBuilder + .hasArg() + .withLongOpt("headerInterval") + .withDescription("the interval for redisplaying column headers, " + + "in number of rows, when outputformat is table. Default is 100") + .create()); + + // --fastConnect=true/false + options.addOption(OptionBuilder + .hasArg() + .withLongOpt("fastConnect") + .withDescription("when connecting, skip building a list of all" + + " tables and columns for tab-completion of HiveQL" + + " statements (true) or build the list (false). Default is true.") + .create()); + + // --autoCommit=true/false + options.addOption(OptionBuilder + .hasArg() + .withLongOpt("autoCommit") + .withDescription("enable/disable automatic transaction commit. Default is false.") + .create()); + + // --verbose=true/false + options.addOption(OptionBuilder + .hasArg() + .withLongOpt("verbose") + .withDescription("show verbose error messages and debug information (true) " + + "or do not show (false).") + .create()); + + // --showWarnings=true/false + options.addOption(OptionBuilder + .hasArg() + .withLongOpt("showWarnings") + .withDescription("display warnings that are reported on the " + + "connection after issuing any HiveQL commands.") + .create()); + + // --showDbInPrompt=true/false + options.addOption(OptionBuilder + .hasArg() + .withLongOpt("showDbInPrompt") + .withDescription("display the current database name in prompt." + + " Default is false.") + .create()); + + // --showNestedErrs=true/false + options.addOption(OptionBuilder + .hasArg() + .withLongOpt("showNestedErrs") + .withDescription("display nested errors.") + .create()); + + // --numberFormat=true/false + options.addOption(OptionBuilder + .hasArg() + .withLongOpt("numberFormat") + .withDescription("format numbers using a DecimalFormat pattern.") + .create()); + + // --maxWidth=MAXWIDTH + options.addOption(OptionBuilder + .hasArg() + .withLongOpt("maxWidth") + .withDescription("the maximum width to display before truncating data," + + " in characters, when outputformat is table. Default is to query" + + " the terminal for current width, then fall back to 80.") + .create()); + + // --maxColumnWidth=MAXCOLWIDTH + options.addOption(OptionBuilder + .hasArg() + .withLongOpt("maxColumnWidth") + .withDescription("the maximum column width, in characters, " + + "when outputformat is table. Default is 50 in " + + "Hive version 2.2.0+ or 15 in earlier versions.") + .create()); + + // --silent=true/false + options.addOption(OptionBuilder + .hasArg() + .withLongOpt("silent") + .withDescription("reduce the amount of informational messages displayed (true) or not (false).") + .create()); + + // --autosave=true/false + options.addOption(OptionBuilder + .hasArg() + .withLongOpt("autosave") + .withDescription("automatically save preferences (true) or do not autosave (false)") + .create()); + + // --outputformat=table/vertical/csv/tsv/dsv/csv2/tsv2 + options.addOption(OptionBuilder + .hasArg() + .withLongOpt("outputformat") + .withDescription("format mode for result display. Default is table.") + .create()); + + // --delimiterForDSV=DELIMITER + options.addOption(OptionBuilder + .hasArg() + .withLongOpt("delimiterForDSV") + .withDescription("the delimiter for delimiter-separated values output format.") + .create()); + + // --isolation=LEVEL + options.addOption(OptionBuilder + .hasArg() + .withLongOpt("isolation") + .withDescription("set the transaction isolation level to TRANSACTION_READ_COMMITTED" + + "or TRANSACTION_SERIALIZABLE. ") + .create()); + + // --nullemptystring=true/false + options.addOption(OptionBuilder + .hasArg() + .withLongOpt("nullemptystring") + .withDescription("use historic behavior of printing null as empty string (true) or" + + " use current behavior of printing null as NULL (false).") + .create()); + + // --incremental=true/false + options.addOption(OptionBuilder + .hasArg() + .withLongOpt("incremental") + .withDescription("when set to false, the entire result set is " + + "fetched and buffered before being displayed, yielding " + + "optimal display column sizing. When set to true, result" + + " rows are displayed immediately as they are fetched," + + " yielding lower latency and memory usage at the " + + "price of extra display column padding. ") + .create()); } @@ -636,7 +794,16 @@ ColorBuffer getColorBuffer(String msg) { @Override protected void processOption(final String arg, final ListIterator iter) throws ParseException { if ((arg.startsWith("--")) && !(arg.equals(HIVE_VAR_PREFIX) || (arg.equals(HIVE_CONF_PREFIX)) - || (arg.equals("--help") || (arg.equals(PROP_FILE_PREFIX))))) { + || (arg.equals("--help") || (arg.equals(PROP_FILE_PREFIX)) || (arg + .equals("--incremental")) || (arg.equals("--force")) || (arg.equals("--color")) || (arg + .equals("--showHeader")) || (arg.equals("--headerInterval")) || (arg + .equals("--fastConnect")) || (arg.equals("--autoCommit")) || (arg.equals("--verbose")) + || (arg.equals("--showWarnings")) || (arg.equals("--showDbInPrompt")) || (arg + .equals("--showNestedErrs")) || (arg.equals("--numberFormat")) || (arg + .equals("--maxWidth")) || (arg.equals("--maxColumnWidth")) || (arg.equals("--silent")) + || (arg.equals("--autosave")) || (arg.equals("--outputformat")) || (arg + .equals("--delimiterForDSV")) || (arg.equals("--isolation")) || (arg + .equals("--nullemptystring"))))) { String stripped = arg.substring(2, arg.length()); String[] parts = split(stripped, "="); debug(loc("setting-prop", Arrays.asList(parts))); @@ -727,7 +894,7 @@ int initArgs(String[] args) { return -1; } - String driver = null, user = null, pass = null, url = null; + String driver = null, user = null, pass = "", url = null; String auth = null; @@ -753,7 +920,7 @@ int initArgs(String[] args) { getOpts().setAuthType(auth); if (cl.hasOption("w")) { pass = obtainPasswordFromFile(cl.getOptionValue("w")); - } else { + } else if (cl.hasOption("p")){ pass = cl.getOptionValue("p"); } url = cl.getOptionValue("u"); @@ -839,8 +1006,8 @@ private String constructCmd(String url, String user, String pass, String driver, + (user == null || user.length() == 0 ? "''" : user) + " "; if (stripPasswd) { com += PASSWD_MASK + " "; - } else { - com += (pass == null || pass.length() == 0 ? "''" : pass) + " "; + } else if(pass != null){ + com += (pass.length() == 0 ? "''" : pass) + " "; } com += (driver == null ? "" : driver); return com; @@ -881,6 +1048,7 @@ public int begin(String[] args, InputStream inputStream) throws IOException { } try { + ConsoleReader reader = getConsoleReader(inputStream); if (isBeeLine) { int code = initArgs(args); if (code != 0) { @@ -905,7 +1073,7 @@ public int begin(String[] args, InputStream inputStream) throws IOException { } catch (Exception e) { // ignore } - ConsoleReader reader = getConsoleReader(inputStream); + return execute(reader, false); } finally { close(); @@ -1077,17 +1245,18 @@ public ConsoleReader getConsoleReader(InputStream inputStream) throws IOExceptio // add shutdown hook to flush the history to history file Runtime.getRuntime().addShutdownHook(new Thread(new Runnable() { - @Override - public void run() { - History h = consoleReader.getHistory(); - if (h instanceof FileHistory) { - try { - ((FileHistory) h).flush(); - } catch (IOException e) { - error(e); - } + @Override public void run() { + if (consoleReader != null) { + History h = consoleReader.getHistory(); + if (h instanceof FileHistory) { + try { + ((FileHistory) h).flush(); + } catch (IOException e) { + error(e); } + } } + } })); consoleReader.addCompleter(new BeeLineCompleter(this));