HBASE-56 Unnecessary HQLClient Object creation in a shell loop
git-svn-id: https://svn.apache.org/repos/asf/hadoop/hbase/trunk@619678 13f79535-47bb-0310-9956-ffa450edef68
This commit is contained in:
parent
93a31f6a16
commit
aa12579a3c
|
@ -25,6 +25,7 @@ HBase Change Log
|
|||
HBASE-410 Speed up the test suite (make test timeout 5 instead of 15 mins).
|
||||
HBASE-281 Shell should allow deletions in .META. and -ROOT- tables
|
||||
(Edward Yoon & Bryan Duxbury via Stack)
|
||||
HBASE-56 Unnecessary HQLClient Object creation in a shell loop
|
||||
|
||||
Branch 0.1
|
||||
|
||||
|
|
|
@ -51,6 +51,7 @@
|
|||
<property name="build.docs" value="${build.dir}/docs"/>
|
||||
<property name="build.javadoc" value="${build.docs}/api"/>
|
||||
<property name="build.encoding" value="ISO-8859-1"/>
|
||||
<property name="build.src" value="${build.dir}/src"/>
|
||||
|
||||
<property name="test.build.dir" value="${build.dir}/test"/>
|
||||
<property name="test.log.dir" value="${test.build.dir}/logs"/>
|
||||
|
@ -136,7 +137,7 @@
|
|||
<!--Compile whats under src and generated java classes made from jsp-->
|
||||
<javac
|
||||
encoding="${build.encoding}"
|
||||
srcdir="${src.dir}"
|
||||
srcdir="${src.dir};${build.src}"
|
||||
includes="**/*.java"
|
||||
destdir="${build.classes}"
|
||||
debug="${javac.debug}"
|
||||
|
|
|
@ -25,10 +25,11 @@ import java.io.Writer;
|
|||
|
||||
import jline.ConsoleReader;
|
||||
|
||||
import org.apache.hadoop.hbase.hql.Constants;
|
||||
import org.apache.hadoop.hbase.hql.HQLClient;
|
||||
import org.apache.hadoop.hbase.hql.HQLSecurityManager;
|
||||
import org.apache.hadoop.hbase.hql.HelpCommand;
|
||||
import org.apache.hadoop.hbase.hql.ReturnMsg;
|
||||
import org.apache.hadoop.hbase.hql.HQLSecurityManager;
|
||||
import org.apache.hadoop.hbase.hql.TableFormatter;
|
||||
import org.apache.hadoop.hbase.hql.TableFormatterFactory;
|
||||
import org.apache.hadoop.hbase.hql.formatter.HtmlTableFormatter;
|
||||
|
@ -42,10 +43,9 @@ import org.apache.hadoop.hbase.hql.formatter.HtmlTableFormatter;
|
|||
public class Shell {
|
||||
/** audible keyboard bells */
|
||||
public static final boolean DEFAULT_BELL_ENABLED = true;
|
||||
public static String MASTER_ADDRESS = null;
|
||||
public static String IP = null;
|
||||
public static int PORT = -1;
|
||||
public static String HTML_OPTION = null;
|
||||
public static int RELAUNCH_FLAG = 7;
|
||||
public static int EXIT_FLAG = 9999;
|
||||
|
||||
/** Return the boolean value indicating whether end of command or not */
|
||||
static boolean isEndOfCommand(String line) {
|
||||
|
@ -84,33 +84,33 @@ public class Shell {
|
|||
System.exit(1);
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
HBaseConfiguration conf = new HBaseConfiguration();
|
||||
ConsoleReader reader = new ConsoleReader();
|
||||
System.setSecurityManager(new HQLSecurityManager());
|
||||
reader.setBellEnabled(conf.getBoolean("hbaseshell.jline.bell.enabled",
|
||||
DEFAULT_BELL_ENABLED));
|
||||
Writer out = new OutputStreamWriter(System.out, "UTF-8");
|
||||
TableFormatter tableFormater = new TableFormatterFactory(out, conf).get();
|
||||
if (MASTER_ADDRESS != null) {
|
||||
conf.set("hbase.master", MASTER_ADDRESS.substring(9, MASTER_ADDRESS.length()));
|
||||
}
|
||||
TableFormatter tableFormatter = new TableFormatterFactory(out, conf).get();
|
||||
|
||||
if (HTML_OPTION != null) {
|
||||
tableFormater = new HtmlTableFormatter(out);
|
||||
tableFormatter = new HtmlTableFormatter(out);
|
||||
}
|
||||
|
||||
HelpCommand help = new HelpCommand(out, tableFormater);
|
||||
if (args.length == 0 || !args[0].equals(String.valueOf(Shell.RELAUNCH_FLAG))) {
|
||||
HelpCommand help = new HelpCommand(out, tableFormatter);
|
||||
if (args.length == 0 || !args[0].equals(String.valueOf(Constants.FLAG_RELAUNCH))) {
|
||||
help.printVersion();
|
||||
}
|
||||
|
||||
StringBuilder queryStr = new StringBuilder();
|
||||
String extendedLine;
|
||||
HQLClient hql = new HQLClient(conf, IP, PORT, out, tableFormatter);
|
||||
|
||||
while ((extendedLine = reader.readLine(getPrompt(queryStr))) != null) {
|
||||
if (isEndOfCommand(extendedLine)) {
|
||||
queryStr.append(" " + extendedLine);
|
||||
long start = System.currentTimeMillis();
|
||||
|
||||
HQLClient hql = new HQLClient(conf, MASTER_ADDRESS, out, tableFormater);
|
||||
ReturnMsg rs = hql.executeQuery(queryStr.toString());
|
||||
|
||||
long end = System.currentTimeMillis();
|
||||
|
@ -132,7 +132,9 @@ public class Shell {
|
|||
private static void argumentParsing(String[] args) {
|
||||
for (int i = 0; i < args.length; i++) {
|
||||
if (args[i].toLowerCase().startsWith("--master:")) {
|
||||
MASTER_ADDRESS = args[i];
|
||||
String[] address = args[i].substring(9, args[i].length()).split(":");
|
||||
IP = address[0];
|
||||
PORT = Integer.valueOf(address[1]);
|
||||
} else if (args[i].toLowerCase().startsWith("--html")) {
|
||||
HTML_OPTION = args[i];
|
||||
}
|
||||
|
|
|
@ -0,0 +1,30 @@
|
|||
/**
|
||||
* Copyright 2007 The Apache Software Foundation
|
||||
*
|
||||
* Licensed to the Apache Software Foundation (ASF) under one
|
||||
* or more contributor license agreements. See the NOTICE file
|
||||
* distributed with this work for additional information
|
||||
* regarding copyright ownership. The ASF licenses this file
|
||||
* to you under the Apache License, Version 2.0 (the
|
||||
* "License"); you may not use this file except in compliance
|
||||
* with the License. You may obtain a copy of the License at
|
||||
*
|
||||
* http://www.apache.org/licenses/LICENSE-2.0
|
||||
*
|
||||
* Unless required by applicable law or agreed to in writing, software
|
||||
* distributed under the License is distributed on an "AS IS" BASIS,
|
||||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||
* See the License for the specific language governing permissions and
|
||||
* limitations under the License.
|
||||
*/
|
||||
package org.apache.hadoop.hbase.hql;
|
||||
|
||||
/**
|
||||
* Some constants used in the hql.
|
||||
*/
|
||||
public class Constants {
|
||||
public static final int FLAG_RELAUNCH = 7;
|
||||
public static final int FLAG_EXIT = 9999;
|
||||
|
||||
public static final int ERROR_CODE = -1;
|
||||
}
|
|
@ -33,7 +33,7 @@ public class ExitCommand extends BasicCommand {
|
|||
HBaseConfiguration conf) {
|
||||
// TOD: Is this the best way to exit? Would be a problem if shell is run
|
||||
// inside another program -- St.Ack 09/11/2007
|
||||
System.exit(Shell.EXIT_FLAG);
|
||||
System.exit(Constants.FLAG_EXIT);
|
||||
return null;
|
||||
}
|
||||
|
||||
|
|
|
@ -26,37 +26,55 @@ import org.apache.hadoop.hbase.hql.generated.HQLParser;
|
|||
import org.apache.hadoop.hbase.hql.generated.ParseException;
|
||||
import org.apache.hadoop.hbase.hql.generated.TokenMgrError;
|
||||
|
||||
/**
|
||||
* HQL query language service client interfaces.
|
||||
*/
|
||||
public class HQLClient {
|
||||
public static String MASTER_ADDRESS = null;
|
||||
static HBaseConfiguration conf;
|
||||
static TableFormatter tableFormater;
|
||||
static Writer out;
|
||||
static TableFormatter tableFormatter = null;
|
||||
static Writer out = null;
|
||||
|
||||
public HQLClient(HBaseConfiguration config, String master, Writer output,
|
||||
/**
|
||||
* Constructor
|
||||
*
|
||||
* @param config HBaseConfiguration
|
||||
* @param ip IP Address
|
||||
* @param port port number
|
||||
* @param writer writer
|
||||
* @param formatter table formatter
|
||||
*/
|
||||
public HQLClient(HBaseConfiguration config, String ip, int port, Writer writer,
|
||||
TableFormatter formatter) {
|
||||
conf = config;
|
||||
out = output;
|
||||
tableFormater = formatter;
|
||||
MASTER_ADDRESS = master;
|
||||
if (ip != null && port != -1)
|
||||
conf.set("hbase.master", ip + ":" + port);
|
||||
out = writer;
|
||||
tableFormatter = formatter;
|
||||
}
|
||||
|
||||
/**
|
||||
* Executes query.
|
||||
*
|
||||
* @param query
|
||||
* @return ReturnMsg object
|
||||
*/
|
||||
public ReturnMsg executeQuery(String query) {
|
||||
HQLParser parser = new HQLParser(query, out, tableFormater);
|
||||
ReturnMsg rs = null;
|
||||
HQLParser parser = new HQLParser(query, out, tableFormatter);
|
||||
ReturnMsg msg = null;
|
||||
|
||||
try {
|
||||
Command cmd = parser.terminatedCommand();
|
||||
if (cmd != null) {
|
||||
rs = cmd.execute(conf);
|
||||
msg = cmd.execute(conf);
|
||||
}
|
||||
} catch (ParseException pe) {
|
||||
String[] msg = pe.getMessage().split("[\n]");
|
||||
rs = new ReturnMsg(-9, "Syntax error : Type 'help;' for usage.\nMessage : " + msg[0]);
|
||||
msg = new ReturnMsg(Constants.ERROR_CODE,
|
||||
"Syntax error : Type 'help;' for usage.");
|
||||
} catch (TokenMgrError te) {
|
||||
String[] msg = te.getMessage().split("[\n]");
|
||||
rs = new ReturnMsg(-9, "Lexical error : Type 'help;' for usage.\nMessage : " + msg[0]);
|
||||
msg = new ReturnMsg(Constants.ERROR_CODE,
|
||||
"Lexical error : Type 'help;' for usage.");
|
||||
}
|
||||
|
||||
return rs;
|
||||
return msg;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
|
@ -43,7 +43,7 @@ public class HQLSecurityManager extends SecurityManager {
|
|||
*/
|
||||
@SuppressWarnings("static-access")
|
||||
public void checkExit(int status) {
|
||||
if (status != Shell.EXIT_FLAG) {
|
||||
if (status != Constants.FLAG_EXIT) {
|
||||
// throw new ExitException(status);
|
||||
|
||||
// I didn't figure out How can catch the ExitException in shell main.
|
||||
|
@ -51,11 +51,11 @@ public class HQLSecurityManager extends SecurityManager {
|
|||
Shell shell = new Shell();
|
||||
|
||||
List<String> argList = new ArrayList<String>();
|
||||
argList.add(String.valueOf(Shell.RELAUNCH_FLAG));
|
||||
argList.add(String.valueOf(Constants.FLAG_RELAUNCH));
|
||||
if(Shell.HTML_OPTION != null)
|
||||
argList.add(Shell.HTML_OPTION);
|
||||
if(Shell.MASTER_ADDRESS != null)
|
||||
argList.add(Shell.MASTER_ADDRESS);
|
||||
if(Shell.IP != null && Shell.PORT != -1)
|
||||
argList.add("--master:" + Shell.IP + ":" + Shell.PORT);
|
||||
|
||||
try {
|
||||
shell.main(argList.toArray(new String[] {}));
|
||||
|
|
|
@ -27,6 +27,7 @@ import java.util.HashMap;
|
|||
import java.util.Map;
|
||||
|
||||
import org.apache.hadoop.hbase.HBaseConfiguration;
|
||||
import org.apache.hadoop.hbase.util.VersionInfo;
|
||||
|
||||
public class HelpCommand extends BasicCommand {
|
||||
private String argument;
|
||||
|
@ -34,10 +35,7 @@ public class HelpCommand extends BasicCommand {
|
|||
"Description", "Example" };
|
||||
|
||||
/** application name */
|
||||
public static final String APP_NAME = "Hbase Shell";
|
||||
|
||||
/** version of the code */
|
||||
public static final String APP_VERSION = "0.0.2";
|
||||
public static final String APP_NAME = "HQL";
|
||||
|
||||
/** help contents map */
|
||||
public final Map<String, String[]> help = new HashMap<String, String[]>();
|
||||
|
@ -135,8 +133,8 @@ public class HelpCommand extends BasicCommand {
|
|||
* @throws IOException
|
||||
*/
|
||||
public void printVersion() throws IOException {
|
||||
println(APP_NAME + ", " + APP_VERSION + " version.\n"
|
||||
+ "Copyright (c) 2007 by udanax, "
|
||||
println(APP_NAME + ", " + VersionInfo.getVersion() + " version.\n"
|
||||
+ "Copyright (c) 2008 by udanax, "
|
||||
+ "licensed to Apache Software Foundation.\n"
|
||||
+ "Type 'help;' for usage.\n");
|
||||
}
|
||||
|
|
Loading…
Reference in New Issue