diff --git a/beeline/src/java/org/apache/hive/beeline/BeeLine.java b/beeline/src/java/org/apache/hive/beeline/BeeLine.java index c47b9fe..9f84771 100644 --- a/beeline/src/java/org/apache/hive/beeline/BeeLine.java +++ b/beeline/src/java/org/apache/hive/beeline/BeeLine.java @@ -22,7 +22,6 @@ */ package org.apache.hive.beeline; -import java.io.BufferedReader; import java.io.ByteArrayInputStream; import java.io.ByteArrayOutputStream; import java.io.Closeable; @@ -31,7 +30,6 @@ import java.io.FileInputStream; import java.io.IOException; import java.io.InputStream; -import java.io.InputStreamReader; import java.io.PrintStream; import java.io.SequenceInputStream; import java.lang.reflect.InvocationTargetException; @@ -672,6 +670,10 @@ int initArgsFromCliVars(String[] args) { getOpts().setScriptFile(commandLine.getOptionValue("f")); + if (commandLine.getOptionValues("i") != null) { + getOpts().setInitFiles(commandLine.getOptionValues("i")); + } + dbName = commandLine.getOptionValue("database"); getOpts().setVerbose(Boolean.valueOf(commandLine.getOptionValue("verbose"))); getOpts().setSilent(Boolean.valueOf(commandLine.getOptionValue("slient"))); @@ -747,7 +749,7 @@ int initArgs(String[] args) { pass = cl.getOptionValue("p"); } url = cl.getOptionValue("u"); - getOpts().setInitFile(cl.getOptionValue("i")); + getOpts().setInitFiles(cl.getOptionValues("i")); getOpts().setScriptFile(cl.getOptionValue("f")); if (cl.getOptionValues('e') != null) { commands = Arrays.asList(cl.getOptionValues('e')); @@ -835,6 +837,8 @@ public int begin(String[] args, InputStream inputStream) throws IOException { getOpts().setShowHeader(false); getOpts().setOutputFormat("dsv"); getOpts().setDelimiterForDSV(' '); + + processInitFiles(opts.getInitFiles()); } if (getOpts().getScriptFile() != null) { @@ -853,13 +857,15 @@ public int begin(String[] args, InputStream inputStream) throws IOException { } int runInit() { - String initFile = getOpts().getInitFile(); - if (initFile != null) { - info("Running init script " + initFile); - try { - return executeFile(initFile); - } finally { - exit = false; + String initFiles[] = getOpts().getInitFiles(); + if (initFiles != null && initFiles.length != 0) { + for (String initFile : initFiles) { + info("Running init script " + initFile); + try { + return executeFile(initFile); + } finally { + exit = false; + } } } return ERRNO_OK; @@ -920,6 +926,24 @@ private int executeFile(String fileName) { } } + /** + * Only initial files specified by i option will be executed. The hiverc file will be processed by session manager. + * + * @param initFiles + * @throws IOException + */ + public void processInitFiles(String[] files) throws IOException { + if (files == null || files.length == 0) { + return; + } + for (String initFile : files) { + int rc = executeFile(initFile); + if (rc != 0) { + System.exit(rc); + } + } + } + private int execute(ConsoleReader reader, boolean exitOnError) { String line; while (!exit) { diff --git a/beeline/src/java/org/apache/hive/beeline/BeeLineOpts.java b/beeline/src/java/org/apache/hive/beeline/BeeLineOpts.java index fec95f2..d9f726d 100644 --- a/beeline/src/java/org/apache/hive/beeline/BeeLineOpts.java +++ b/beeline/src/java/org/apache/hive/beeline/BeeLineOpts.java @@ -93,7 +93,7 @@ private String historyFile = new File(saveDir(), "history").getAbsolutePath(); private String scriptFile = null; - private String initFile = null; + private String[] initFiles = null; private String authType = null; private char delimiterForDSV = DEFAULT_DELIMITER_FOR_DSV; @@ -388,12 +388,12 @@ public String getScriptFile() { return scriptFile; } - public String getInitFile() { - return initFile; + public String[] getInitFiles() { + return initFiles; } - public void setInitFile(String initFile) { - this.initFile = initFile; + public void setInitFiles(String[] initFiles) { + this.initFiles = initFiles; } public void setColor(boolean color) {