Index: CliDriver.java =================================================================== --- CliDriver.java (revision 125579) +++ CliDriver.java (working copy) @@ -18,18 +18,32 @@ package org.apache.hadoop.hive.cli; -import jline.*; +import java.io.BufferedReader; +import java.io.File; +import java.io.FileNotFoundException; +import java.io.FileReader; +import java.io.IOException; +import java.io.PrintStream; +import java.io.UnsupportedEncodingException; +import java.util.LinkedList; +import java.util.List; +import java.util.Map; +import java.util.Vector; +import java.util.regex.Matcher; +import java.util.regex.Pattern; -import java.io.*; -import java.util.*; +import jline.ArgumentCompletor; +import jline.ConsoleReader; +import jline.History; +import jline.SimpleCompletor; +import org.apache.commons.logging.Log; +import org.apache.commons.logging.LogFactory; import org.apache.hadoop.hive.conf.HiveConf; +import org.apache.hadoop.hive.ql.Driver; import org.apache.hadoop.hive.ql.exec.Utilities; import org.apache.hadoop.hive.ql.exec.Utilities.StreamPrinter; import org.apache.hadoop.hive.ql.session.SessionState; -import org.apache.hadoop.hive.ql.Driver; -import org.apache.commons.logging.Log; -import org.apache.commons.logging.LogFactory; import org.apache.hadoop.hive.ql.session.SessionState.LogHelper; public class CliDriver { @@ -47,13 +61,14 @@ } public static int processCmd(String cmd) { - String[] tokens = cmd.split("\\s+"); - String cmd_1 = cmd.substring(tokens[0].length()); + String tcmd= cmd.trim(); + String[] tokens = tcmd.split("\\s+"); int ret = 0; - if(tokens[0].equals("set")) { + if(tokens.length > 0 && tokens[0].equals("set")) { + String cmd_1 = tcmd.substring(tokens[0].length()); ret = sp.run(cmd_1); - } else if (cmd.equals("quit") || cmd.equals("exit")) { + } else if (tcmd.equals("quit") || tcmd.equals("exit")) { // if we have come this far - either the previous commands // are all successful or this is command line. in either case // this counts as a successful run @@ -118,9 +133,10 @@ public static int processLine(String line) { int ret = 0; for(String oneCmd: line.split(";")) { - oneCmd = oneCmd.trim(); - if(oneCmd.equals("")) - continue; + + //ignore empty commands + if(oneCmd.isEmpty()) + continue; ret = processCmd(oneCmd); if(ret != 0) { @@ -134,13 +150,45 @@ public static int processReader(BufferedReader r) throws IOException { String line; int ret = 0; + int nb=0, nbcommand =0; + boolean hasFoundCommand=false; //to avoid sending empty commands to Driver + Pattern p = Pattern.compile("^(.*);\\s*$"); + Pattern comment_p = Pattern.compile("^\\s*-{2}.*$"); + StringBuffer buf = new StringBuffer(""); + Log LOG = LogFactory.getLog("CliDriver"); while((line = r.readLine()) != null) { - ret = processLine(line); - if(ret != 0) { - return ret; - } + nb++; + nbcommand++; + //just append comments and empty lines + Matcher m = comment_p.matcher(line); + if (line.isEmpty() || m.matches()){ + buf.append(line+"\n"); + continue; + } + hasFoundCommand=true; + + //simply using ; at the end of a line as separator + m = p.matcher(line); + if(m.matches()){ + buf.append(m.group(1)); + LOG.info("command line offset="+(nb-nbcommand)); + ret = processCmd(buf.toString()); + if(ret != 0) + return ret; + hasFoundCommand=false; + buf = new StringBuffer(""); + nbcommand=0; + }else{ + buf.append(line+"\n"); + } + } + + //remaining lines + if (hasFoundCommand && buf.length()!=0){ + LOG.info("Command nb line offset="+(nb-nbcommand)); + ret = processCmd(buf.toString()); } - return 0; + return ret; } public static void main(String[] args) throws IOException { @@ -191,9 +239,6 @@ System.exit(3); } - Character mask = null; - String trigger = null; - ConsoleReader reader = new ConsoleReader(); reader.setBellEnabled(false); //reader.setDebug(new PrintWriter(new FileWriter("writer.debug", true))); @@ -205,7 +250,6 @@ reader.addCompletor(new ArgumentCompletor(completors)); String line; - PrintWriter out = new PrintWriter(System.out); final String HISTORYFILE = ".hivehistory"; String historyFile = System.getProperty("user.home") + File.separator + HISTORYFILE; reader.setHistory(new History(new File(historyFile)));