HADOOP-2297 System.exit() Handling in hbase shell jar command

git-svn-id: https://svn.apache.org/repos/asf/lucene/hadoop/trunk/src/contrib/hbase@599713 13f79535-47bb-0310-9956-ffa450edef68
This commit is contained in:
Michael Stack 2007-11-30 07:33:09 +00:00
parent 5a465ae05b
commit 05d4458b47
25 changed files with 312 additions and 280 deletions

View File

@ -66,6 +66,7 @@ Trunk (unreleased changes)
(Edward Yoon via Stack)
HADOOP-2198 HTable should have method to return table metadata
HADOOP-2296 hbase shell: phantom columns show up from select command
HADOOP-2297 System.exit() Handling in hbase shell jar command
Release 0.15.1

View File

@ -27,6 +27,7 @@ import jline.ConsoleReader;
import org.apache.hadoop.hbase.shell.Command;
import org.apache.hadoop.hbase.shell.HelpCommand;
import org.apache.hadoop.hbase.shell.ShellSecurityManager;
import org.apache.hadoop.hbase.shell.ReturnMsg;
import org.apache.hadoop.hbase.shell.TableFormatterFactory;
import org.apache.hadoop.hbase.shell.generated.ParseException;
@ -63,23 +64,22 @@ public class Shell {
" sec)":
"";
}
/**
* Main method
* @param args not used
* @throws IOException
*/
public static void main(@SuppressWarnings("unused") String args[])
throws IOException {
public static void main(String args[]) throws IOException {
HBaseConfiguration conf = new HBaseConfiguration();
ConsoleReader reader = new ConsoleReader();
System.setSecurityManager(new ShellSecurityManager());
reader.setBellEnabled(conf.getBoolean("hbaseshell.jline.bell.enabled",
DEFAULT_BELL_ENABLED));
Writer out = new OutputStreamWriter(System.out, "UTF-8");
TableFormatterFactory tff = new TableFormatterFactory(out, conf);
HelpCommand help = new HelpCommand(out, tff.get());
help.printVersion();
if(args.length == 0) help.printVersion();
StringBuilder queryStr = new StringBuilder();
String extendedLine;
while ((extendedLine = reader.readLine(getPrompt(queryStr))) != null) {
@ -91,16 +91,16 @@ public class Shell {
try {
Command cmd = parser.terminatedCommand();
if (cmd != null) {
rs = cmd.execute(conf);
rs = cmd.execute(conf);
}
} catch (ParseException pe) {
} catch (ParseException pe) {
String[] msg = pe.getMessage().split("[\n]");
System.out.println("Syntax error : Type 'help;' for usage.\nMessage : " + msg[0]);
} catch (TokenMgrError te) {
String[] msg = te.getMessage().split("[\n]");
System.out.println("Lexical error : Type 'help;' for usage.\nMessage : " + msg[0]);
}
}
long end = System.currentTimeMillis();
if (rs != null && rs.getType() > -1)
System.out.println(rs.getMsg()

View File

@ -36,10 +36,12 @@ import org.apache.hadoop.io.Text;
* Alters tables.
*/
public class AlterCommand extends SchemaModificationCommand {
public enum OperationType {ADD, DROP, CHANGE, NOOP}
public enum OperationType {
ADD, DROP, CHANGE, NOOP
}
private OperationType operationType = OperationType.NOOP;
private Map<String, Map<String, Object>> columnSpecMap =
new HashMap<String, Map<String, Object>>();
private Map<String, Map<String, Object>> columnSpecMap = new HashMap<String, Map<String, Object>>();
private String tableName;
private String column; // column to be dropped
@ -51,37 +53,36 @@ public class AlterCommand extends SchemaModificationCommand {
try {
HConnection conn = HConnectionManager.getConnection(conf);
if (!conn.tableExists(new Text(this.tableName))) {
return new ReturnMsg(0, "'" + this.tableName + "' Table not found");
return new ReturnMsg(0, "'" + this.tableName + "'" + TABLE_NOT_FOUND);
}
HBaseAdmin admin = new HBaseAdmin(conf);
Set<String> columns = null;
HColumnDescriptor columnDesc = null;
switch (operationType) {
case ADD:
disableTable(admin, tableName);
columns = columnSpecMap.keySet();
for (String c : columns) {
columnDesc = getColumnDescriptor(c, columnSpecMap.get(c));
println("Adding " + c + " to " + tableName +
"... Please wait.");
admin.addColumn(new Text(tableName), columnDesc);
}
enableTable(admin, tableName);
break;
case DROP:
disableTable(admin, tableName);
println("Dropping " + column + " from " + tableName +
"... Please wait.");
column = appendDelimiter(column);
admin.deleteColumn(new Text(tableName), new Text(column));
enableTable(admin, tableName);
break;
case CHANGE:
// Not yet supported
return new ReturnMsg(0, "" + operationType + " is not yet supported.");
case NOOP:
return new ReturnMsg(0, "Invalid operation type.");
case ADD:
disableTable(admin, tableName);
columns = columnSpecMap.keySet();
for (String c : columns) {
columnDesc = getColumnDescriptor(c, columnSpecMap.get(c));
println("Adding " + c + " to " + tableName + "... Please wait.");
admin.addColumn(new Text(tableName), columnDesc);
}
enableTable(admin, tableName);
break;
case DROP:
disableTable(admin, tableName);
println("Dropping " + column + " from " + tableName
+ "... Please wait.");
column = appendDelimiter(column);
admin.deleteColumn(new Text(tableName), new Text(column));
enableTable(admin, tableName);
break;
case CHANGE:
// Not yet supported
return new ReturnMsg(0, "" + operationType + " is not yet supported.");
case NOOP:
return new ReturnMsg(0, "Invalid operation type.");
}
return new ReturnMsg(0, "Table altered successfully.");
} catch (Exception e) {
@ -135,9 +136,9 @@ public class AlterCommand extends SchemaModificationCommand {
public void setOperationType(OperationType operationType) {
this.operationType = operationType;
}
@Override
public CommandType getCommandType() {
return CommandType.DDL;
}
}
}

View File

@ -25,20 +25,24 @@ import java.io.Writer;
/**
* Takes the lowest-common-denominator {@link Writer} doing its own printlns,
* etc.
* @see <a href="http://wiki.apache.org/lucene-hadoop/Hbase/HbaseShell">HBaseShell</a>
*
* @see <a
* href="http://wiki.apache.org/lucene-hadoop/Hbase/HbaseShell">HBaseShell</a>
*/
public abstract class BasicCommand implements Command, CommandFactory {
private final Writer out;
public final String LINE_SEPARATOR = System.getProperty("line.separator");
public final String TABLE_NOT_FOUND = " is non-existant table.";
// Shutdown constructor.
@SuppressWarnings("unused")
private BasicCommand() {
this(null);
}
/**
* Constructor
*
* @param o A Writer.
*/
public BasicCommand(final Writer o) {
@ -48,29 +52,29 @@ public abstract class BasicCommand implements Command, CommandFactory {
public BasicCommand getBasicCommand() {
return this;
}
/** basic commands are their own factories. */
public Command getCommand() {
return this;
}
protected String extractErrMsg(String msg) {
int index = msg.indexOf(":");
int eofIndex = msg.indexOf("\n");
return msg.substring(index + 1, eofIndex);
}
protected String extractErrMsg(Exception e) {
return extractErrMsg(e.getMessage());
}
/**
* Appends, if it does not exist, a delimiter (colon)
* at the end of the column name.
* Appends, if it does not exist, a delimiter (colon) at the end of the column
* name.
*/
protected String appendDelimiter(String column) {
return (!column.endsWith(FAMILY_INDICATOR) && column.indexOf(FAMILY_INDICATOR) == -1)?
column + FAMILY_INDICATOR: column;
return (!column.endsWith(FAMILY_INDICATOR) && column
.indexOf(FAMILY_INDICATOR) == -1) ? column + FAMILY_INDICATOR : column;
}
/**
@ -79,18 +83,18 @@ public abstract class BasicCommand implements Command, CommandFactory {
public Writer getOut() {
return this.out;
}
public void print(final String msg) throws IOException {
this.out.write(msg);
}
public void println(final String msg) throws IOException {
print(msg);
print(LINE_SEPARATOR);
this.out.flush();
}
public CommandType getCommandType() {
return CommandType.SELECT;
}
}
}

View File

@ -25,14 +25,15 @@ import java.io.Writer;
import org.apache.hadoop.hbase.HBaseConfiguration;
/**
* Clears the console screen.
* Clears the console screen.
*/
public class ClearCommand extends BasicCommand {
public ClearCommand(Writer o) {
super(o);
}
public ReturnMsg execute(@SuppressWarnings("unused") HBaseConfiguration conf) {
public ReturnMsg execute(@SuppressWarnings("unused")
HBaseConfiguration conf) {
clear();
return null;
}
@ -57,9 +58,9 @@ public class ClearCommand extends BasicCommand {
}
}
}
@Override
public CommandType getCommandType() {
return CommandType.SHELL;
}
}
}

View File

@ -24,10 +24,14 @@ import org.apache.hadoop.hbase.HBaseConfiguration;
public interface Command {
/** family indicator */
public static final String FAMILY_INDICATOR = ":";
public enum CommandType {DDL, UPDATE, SELECT, INSERT, DELETE, SHELL}
/** Execute a command
public enum CommandType {
DDL, UPDATE, SELECT, INSERT, DELETE, SHELL
}
/**
* Execute a command
*
* @param conf Configuration
* @return Result of command execution
*/
@ -35,7 +39,7 @@ public interface Command {
/**
* @return Type of this command whether DDL, SELECT, INSERT, UPDATE, DELETE,
* or SHELL.
* or SHELL.
*/
public CommandType getCommandType();
}
}

View File

@ -37,20 +37,19 @@ import org.apache.hadoop.io.Text;
*/
public class CreateCommand extends SchemaModificationCommand {
private Text tableName;
private Map<String, Map<String, Object>> columnSpecMap =
new HashMap<String, Map<String, Object>>();
private Map<String, Map<String, Object>> columnSpecMap = new HashMap<String, Map<String, Object>>();
public CreateCommand(Writer o) {
super(o);
}
public ReturnMsg execute(HBaseConfiguration conf) {
try {
HConnection conn = HConnectionManager.getConnection(conf);
if (conn.tableExists(this.tableName)) {
return new ReturnMsg(0, "'" + this.tableName + "' Table already exist");
return new ReturnMsg(0, "'" + this.tableName + "' table already exist.");
}
HBaseAdmin admin = new HBaseAdmin(conf);
HTableDescriptor tableDesc = new HTableDescriptor(tableName.toString());
HColumnDescriptor columnDesc = null;
@ -59,19 +58,19 @@ public class CreateCommand extends SchemaModificationCommand {
columnDesc = getColumnDescriptor(column, columnSpecMap.get(column));
tableDesc.addFamily(columnDesc);
}
println("Creating table... Please wait.");
admin.createTable(tableDesc);
return new ReturnMsg(0, "Table created successfully.");
}
catch (Exception e) {
} catch (Exception e) {
return new ReturnMsg(0, extractErrMsg(e));
}
}
/**
* Sets the table to be created.
*
* @param tableName Table to be created
*/
public void setTable(String tableName) {
@ -79,13 +78,14 @@ public class CreateCommand extends SchemaModificationCommand {
}
/**
* Adds a column specification.
* Adds a column specification.
*
* @param columnSpec Column specification
*/
public void addColumnSpec(String column, Map<String, Object> columnSpec) {
columnSpecMap.put(column, columnSpec);
}
@Override
public CommandType getCommandType() {
return CommandType.DDL;

View File

@ -50,9 +50,9 @@ public class DeleteCommand extends BasicCommand {
try {
HConnection conn = HConnectionManager.getConnection(conf);
if (!conn.tableExists(new Text(this.tableName))) {
return new ReturnMsg(0, "'" + this.tableName + "' Table not found");
return new ReturnMsg(0, "'" + this.tableName + "'" + TABLE_NOT_FOUND);
}
HBaseAdmin admin = new HBaseAdmin(conf);
HTable hTable = new HTable(conf, new Text(tableName));
long lockID = hTable.startUpdate(new Text(rowKey));
@ -77,6 +77,7 @@ public class DeleteCommand extends BasicCommand {
/**
* Sets the column list.
*
* @param columnList
*/
public void setColumnList(List<String> columnList) {
@ -92,7 +93,8 @@ public class DeleteCommand extends BasicCommand {
Text[] columns = null;
try {
if (this.columnList.contains("*")) {
columns = hTable.getRow(new Text(this.rowKey)).keySet().toArray(new Text[] {});
columns = hTable.getRow(new Text(this.rowKey)).keySet().toArray(
new Text[] {});
} else {
List<Text> tmpList = new ArrayList<Text>();
for (int i = 0; i < this.columnList.size(); i++) {
@ -111,9 +113,9 @@ public class DeleteCommand extends BasicCommand {
}
return columns;
}
@Override
public CommandType getCommandType() {
return CommandType.DELETE;
}
}
}

View File

@ -33,42 +33,41 @@ import org.apache.hadoop.io.Text;
* Prints information about tables.
*/
public class DescCommand extends BasicCommand {
private static final String [] HEADER =
new String [] {"Column Family Descriptor"};
private static final String[] HEADER = new String[] { "Column Family Descriptor" };
private Text tableName;
private final TableFormatter formatter;
// Not instantiable
@SuppressWarnings("unused")
private DescCommand() {
this(null, null);
}
public DescCommand(final Writer o, final TableFormatter f) {
super(o);
this.formatter = f;
}
public ReturnMsg execute(final HBaseConfiguration conf) {
if (this.tableName == null)
return new ReturnMsg(0, "Syntax error : Please check 'Describe' syntax");
if (this.tableName == null)
return new ReturnMsg(0, "Syntax error : Please check 'Describe' syntax.");
try {
HConnection conn = HConnectionManager.getConnection(conf);
if (!conn.tableExists(this.tableName)) {
return new ReturnMsg(0, "Table not found");
return new ReturnMsg(0, "Table not found.");
}
HTableDescriptor [] tables = conn.listTables();
HColumnDescriptor [] columns = null;
HTableDescriptor[] tables = conn.listTables();
HColumnDescriptor[] columns = null;
for (int i = 0; i < tables.length; i++) {
if (tables[i].getName().equals(this.tableName)) {
columns = tables[i].getFamilies().values().
toArray(new HColumnDescriptor [] {});
columns = tables[i].getFamilies().values().toArray(
new HColumnDescriptor[] {});
break;
}
}
formatter.header(HEADER);
// Do a toString on the HColumnDescriptors
String [] columnStrs = new String[columns.length];
String[] columnStrs = new String[columns.length];
for (int i = 0; i < columns.length; i++) {
String tmp = columns[i].toString();
// Strip the curly-brackets if present.
@ -76,10 +75,10 @@ public class DescCommand extends BasicCommand {
tmp = tmp.substring(1, tmp.length() - 1);
}
columnStrs[i] = tmp;
formatter.row(new String [] {columnStrs[i]});
formatter.row(new String[] { columnStrs[i] });
}
formatter.footer();
return new ReturnMsg(1, columns.length + " columnfamily(s) in set");
return new ReturnMsg(1, columns.length + " columnfamily(s) in set.");
} catch (IOException e) {
return new ReturnMsg(0, "error msg : " + e.toString());
}
@ -88,4 +87,4 @@ public class DescCommand extends BasicCommand {
public void setArgument(String table) {
this.tableName = new Text(table);
}
}
}

View File

@ -24,6 +24,8 @@ import java.io.Writer;
import org.apache.hadoop.hbase.HBaseAdmin;
import org.apache.hadoop.hbase.HBaseConfiguration;
import org.apache.hadoop.hbase.HConnection;
import org.apache.hadoop.hbase.HConnectionManager;
import org.apache.hadoop.io.Text;
/**
@ -31,18 +33,23 @@ import org.apache.hadoop.io.Text;
*/
public class DisableCommand extends BasicCommand {
private String tableName;
public DisableCommand(Writer o) {
super(o);
}
public ReturnMsg execute(HBaseConfiguration conf) {
assert tableName != null;
try {
HConnection conn = HConnectionManager.getConnection(conf);
if (!conn.tableExists(new Text(this.tableName))) {
return new ReturnMsg(0, "'" + this.tableName + "'" + TABLE_NOT_FOUND);
}
HBaseAdmin admin = new HBaseAdmin(conf);
admin.disableTable(new Text(tableName));
return new ReturnMsg(1, "Table disabled successfully.");
} catch (IOException e) {
String[] msg = e.getMessage().split("[\n]");
@ -53,9 +60,9 @@ public class DisableCommand extends BasicCommand {
public void setTable(String table) {
this.tableName = table;
}
@Override
public CommandType getCommandType() {
return CommandType.DDL;
}
}
}

View File

@ -25,6 +25,8 @@ import java.util.List;
import org.apache.hadoop.hbase.HBaseAdmin;
import org.apache.hadoop.hbase.HBaseConfiguration;
import org.apache.hadoop.hbase.HConnection;
import org.apache.hadoop.hbase.HConnectionManager;
import org.apache.hadoop.io.Text;
/**
@ -32,25 +34,36 @@ import org.apache.hadoop.io.Text;
*/
public class DropCommand extends BasicCommand {
private List<String> tableList;
public DropCommand(Writer o) {
super(o);
}
public ReturnMsg execute(HBaseConfiguration conf) {
if (tableList == null) {
throw new IllegalArgumentException("List of tables is null");
throw new IllegalArgumentException("List of tables is null.");
}
try {
HBaseAdmin admin = new HBaseAdmin(conf);
HConnection conn = HConnectionManager.getConnection(conf);
int count = 0;
for (String table : tableList) {
println("Dropping " + table + "... Please wait.");
admin.deleteTable(new Text(table));
if (!conn.tableExists(new Text(table))) {
println("'" + table + "' table not found.");
} else {
println("Dropping " + table + "... Please wait.");
admin.deleteTable(new Text(table));
count++;
}
}
if (count > 0) {
return new ReturnMsg(1, count + " table(s) dropped successfully.");
} else {
return new ReturnMsg(0, count + " table(s) dropped.");
}
return new ReturnMsg(1, "Table(s) dropped successfully.");
} catch (IOException e) {
return new ReturnMsg(0, extractErrMsg(e));
}
@ -59,9 +72,9 @@ public class DropCommand extends BasicCommand {
public void setTableList(List<String> tableList) {
this.tableList = tableList;
}
@Override
public CommandType getCommandType() {
return CommandType.DDL;
}
}
}

View File

@ -33,19 +33,19 @@ import org.apache.hadoop.io.Text;
*/
public class EnableCommand extends BasicCommand {
private String tableName;
public EnableCommand(Writer o) {
super(o);
}
public ReturnMsg execute(HBaseConfiguration conf) {
assert tableName != null;
try {
HConnection conn = HConnectionManager.getConnection(conf);
if (!conn.tableExists(new Text(this.tableName))) {
return new ReturnMsg(0, "'" + this.tableName + "' Table not found");
return new ReturnMsg(0, "'" + this.tableName + "'" + TABLE_NOT_FOUND);
}
HBaseAdmin admin = new HBaseAdmin(conf);
admin.enableTable(new Text(tableName));
return new ReturnMsg(1, "Table enabled successfully.");
@ -58,9 +58,9 @@ public class EnableCommand extends BasicCommand {
public void setTable(String table) {
this.tableName = table;
}
@Override
public CommandType getCommandType() {
return CommandType.DDL;
}
}
}

View File

@ -28,13 +28,14 @@ public class ExitCommand extends BasicCommand {
super(o);
}
public ReturnMsg execute(@SuppressWarnings("unused") HBaseConfiguration conf) {
// TOD: Is this the best way to exit? Would be a problem if shell is run
public ReturnMsg execute(@SuppressWarnings("unused")
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(1);
System.exit(9999);
return null;
}
@Override
public CommandType getCommandType() {
return CommandType.SHELL;

View File

@ -31,13 +31,14 @@ import org.apache.hadoop.util.ToolRunner;
*/
public class FsCommand extends BasicCommand {
private List<String> query;
public FsCommand(Writer o) {
super(o);
}
public ReturnMsg execute(@SuppressWarnings("unused") HBaseConfiguration conf) {
// This commmand will write the
public ReturnMsg execute(@SuppressWarnings("unused")
HBaseConfiguration conf) {
// This commmand will write the
FsShell shell = new FsShell();
try {
ToolRunner.run(shell, getQuery());
@ -55,7 +56,7 @@ public class FsCommand extends BasicCommand {
private String[] getQuery() {
return query.toArray(new String[] {});
}
@Override
public CommandType getCommandType() {
return CommandType.SHELL;

View File

@ -30,8 +30,8 @@ import org.apache.hadoop.hbase.HBaseConfiguration;
public class HelpCommand extends BasicCommand {
private String argument;
private static final String[] HEADER = new String[] { "Command",
"Description", "Example" };
private static final String[] HEADER = new String[] { "Command", "Description",
"Example" };
/** application name */
public static final String APP_NAME = "Hbase Shell";
@ -74,10 +74,10 @@ public class HelpCommand extends BasicCommand {
load.put("FS", new String[] {
"Hadoop FsShell; entering a lone 'FS;' " + "will emit usage",
"FS -copyFromLocal /home/user/backup.dat fs/user/backup;" });
"FS [-option] arguments..;" });
load.put("JAR", new String[] { "Hadoop RunJar util",
"JAR ./build/hadoop-examples.jar pi 10 10;" });
"JAR jarFile [mainClass] arguments...;" });
load.put("CLEAR", new String[] { "Clear the screen", "CLEAR;" });
load.put("DESCRIBE", new String[] { "Print table information",
@ -125,11 +125,12 @@ public class HelpCommand extends BasicCommand {
// model of
// data.
load.put("TABLE",
new String[] { "Load a table", "A = table('table_name');" });
load
.put("TABLE", new String[] { "Load a table", "A = table('table_name');" });
load.put("SUBSTITUTE", new String[] { "Substitute expression to [A~Z]",
"D = A.projection('cf_name1'[, 'cf_name2']);" });
load.put("SAVE", new String[] { "Save results into specified table (It runs a mapreduce job)",
load.put("SAVE", new String[] {
"Save results into specified table (It runs a mapreduce job)",
"SAVE A INTO table('table_name');" });
// Relational Operations
@ -146,14 +147,11 @@ public class HelpCommand extends BasicCommand {
+ " B = A.Selection(cf_name1 > 100 [AND cf_name2 = 'string_value']);" });
// Aggregation Functions
//TODO : and apply aggregate function independently to each group of rows
load
.put(
"GROUP",
new String[] {
"Group rows by value of an attribute",
"A = Table('table_name');"
+ " B = Group A by ('cf_name1'[, 'cf_name2']);" });
// TODO : and apply aggregate function independently to each group of rows
load.put("GROUP", new String[] {
"Group rows by value of an attribute",
"A = Table('table_name');"
+ " B = Group A by ('cf_name1'[, 'cf_name2']);" });
return load;
}

View File

@ -48,12 +48,12 @@ public class InsertCommand extends BasicCommand {
HConnection conn = HConnectionManager.getConnection(conf);
if (!conn.tableExists(this.tableName)) {
return new ReturnMsg(0, "'" + this.tableName + "' Table not found");
return new ReturnMsg(0, "'" + this.tableName + "'" + TABLE_NOT_FOUND);
}
if (this.columnfamilies.size() != this.values.size())
return new ReturnMsg(0,
"Mismatch between values list and columnfamilies list");
"Mismatch between values list and columnfamilies list.");
try {
HTable table = new HTable(conf, this.tableName);
@ -61,7 +61,7 @@ public class InsertCommand extends BasicCommand {
for (int i = 0; i < this.values.size(); i++) {
Text column = null;
if(getColumn(i).toString().contains(":"))
if (getColumn(i).toString().contains(":"))
column = getColumn(i);
else
column = new Text(getColumn(i) + ":");
@ -103,7 +103,7 @@ public class InsertCommand extends BasicCommand {
public byte[] getValue(int i) {
return this.values.get(i).getBytes();
}
@Override
public CommandType getCommandType() {
return CommandType.INSERT;

View File

@ -43,14 +43,15 @@ import org.apache.hadoop.util.RunJar;
*/
public class JarCommand extends BasicCommand {
private List<String> query;
public JarCommand(Writer o) {
super(o);
}
@SuppressWarnings("deprecation")
public ReturnMsg execute(@SuppressWarnings("unused") HBaseConfiguration conf) {
public ReturnMsg execute(@SuppressWarnings("unused")
HBaseConfiguration conf) {
try {
String[] args = getQuery();
String usage = "JAR jarFile [mainClass] args...;\n";
@ -67,9 +68,9 @@ public class JarCommand extends BasicCommand {
JarFile jarFile;
try {
jarFile = new JarFile(fileName);
} catch(IOException io) {
} catch (IOException io) {
throw new IOException("Error opening job jar: " + fileName + "\n")
.initCause(io);
.initCause(io);
}
Manifest manifest = jarFile.getManifest();
@ -88,7 +89,7 @@ public class JarCommand extends BasicCommand {
File tmpDir = new File(new Configuration().get("hadoop.tmp.dir"));
tmpDir.mkdirs();
if (!tmpDir.isDirectory()) {
if (!tmpDir.isDirectory()) {
return new ReturnMsg(0, "Mkdirs failed to create " + tmpDir + "\n");
}
final File workDir = File.createTempFile("hadoop-unjar", "", tmpDir);
@ -99,19 +100,19 @@ public class JarCommand extends BasicCommand {
}
Runtime.getRuntime().addShutdownHook(new Thread() {
public void run() {
try {
FileUtil.fullyDelete(workDir);
} catch (IOException e) {
e.printStackTrace();
}
public void run() {
try {
FileUtil.fullyDelete(workDir);
} catch (IOException e) {
e.printStackTrace();
}
});
}
});
RunJar.unJar(file, workDir);
ArrayList<URL> classPath = new ArrayList<URL>();
classPath.add(new File(workDir+"/").toURL());
classPath.add(new File(workDir + "/").toURL());
classPath.add(file.toURL());
classPath.add(new File(workDir, "classes/").toURL());
File[] libs = new File(workDir, "lib").listFiles();
@ -120,29 +121,26 @@ public class JarCommand extends BasicCommand {
classPath.add(libs[i].toURL());
}
}
ClassLoader loader =
new URLClassLoader(classPath.toArray(new URL[0]));
ClassLoader loader = new URLClassLoader(classPath.toArray(new URL[0]));
Thread.currentThread().setContextClassLoader(loader);
Class<?> mainClass = Class.forName(mainClassName, true, loader);
Method main = mainClass.getMethod("main", new Class[] {
Array.newInstance(String.class, 0).getClass()
});
String[] newArgs = Arrays.asList(args)
.subList(firstArg, args.length).toArray(new String[0]);
Method main = mainClass.getMethod("main", new Class[] { Array.newInstance(
String.class, 0).getClass() });
String[] newArgs = Arrays.asList(args).subList(firstArg, args.length)
.toArray(new String[0]);
try {
main.invoke(null, new Object[] { newArgs });
} catch (InvocationTargetException e) {
throw e.getTargetException();
}
} catch (Throwable e) {
e.printStackTrace();
}
return null;
}
public void setQuery(List<String> query) {
this.query = query;
}
@ -150,7 +148,7 @@ public class JarCommand extends BasicCommand {
private String[] getQuery() {
return query.toArray(new String[] {});
}
@Override
public CommandType getCommandType() {
return CommandType.SHELL;

View File

@ -46,7 +46,7 @@ public class ReturnMsg {
public int getType() {
return this.type;
}
@Override
public String toString() {
return this.msg;

View File

@ -42,7 +42,7 @@ public abstract class SchemaModificationCommand extends BasicCommand {
private int vectorSize;
private int numHash;
private int numEntries;
public SchemaModificationCommand(Writer o) {
super(o);
}
@ -77,8 +77,8 @@ public abstract class SchemaModificationCommand extends BasicCommand {
} else if (spec.equals("IN_MEMORY")) {
inMemory = (Boolean) columnSpec.get(spec);
} else if (spec.equals("BLOOMFILTER")) {
bloomFilterType = BloomFilterType.valueOf(((String) columnSpec
.get(spec)).toUpperCase());
bloomFilterType = BloomFilterType.valueOf(((String) columnSpec.get(spec))
.toUpperCase());
} else if (spec.equals("VECTOR_SIZE")) {
vectorSize = (Integer) columnSpec.get(spec);
} else if (spec.equals("NUM_HASH")) {
@ -95,8 +95,8 @@ public abstract class SchemaModificationCommand extends BasicCommand {
if (specs.contains("NUM_ENTRIES")) {
bloomFilterDesc = new BloomFilterDescriptor(bloomFilterType, numEntries);
} else {
bloomFilterDesc = new BloomFilterDescriptor(bloomFilterType,
vectorSize, numHash);
bloomFilterDesc = new BloomFilterDescriptor(bloomFilterType, vectorSize,
numHash);
}
}
@ -107,4 +107,4 @@ public abstract class SchemaModificationCommand extends BasicCommand {
return columnDesc;
}
}
}

View File

@ -55,38 +55,36 @@ public class SelectCommand extends BasicCommand {
// Count of versions to return.
private int version;
private boolean whereClause = false;
private static final String [] HEADER_ROW_CELL =
new String [] {"Row", "Cell"};
private static final String [] HEADER_COLUMN_CELL =
new String [] {"Column", "Cell"};
private static final String [] HEADER =
new String [] {"Row", "Column", "Cell"};
private static final String[] HEADER_ROW_CELL = new String[] { "Row", "Cell" };
private static final String[] HEADER_COLUMN_CELL = new String[] { "Column",
"Cell" };
private static final String[] HEADER = new String[] { "Row", "Column", "Cell" };
private static final String STAR = "*";
private final TableFormatter formatter;
// Not instantiable
@SuppressWarnings("unused")
private SelectCommand() {
this(null, null);
}
public SelectCommand(final Writer o, final TableFormatter f) {
super(o);
this.formatter = f;
}
public ReturnMsg execute(final HBaseConfiguration conf) {
if (this.tableName.equals("") || this.rowKey == null ||
this.columns.size() == 0) {
if (this.tableName.equals("") || this.rowKey == null
|| this.columns.size() == 0) {
return new ReturnMsg(0, "Syntax error : Please check 'Select' syntax.");
}
}
try {
HConnection conn = HConnectionManager.getConnection(conf);
if (!conn.tableExists(this.tableName)) {
return new ReturnMsg(0, "'" + this.tableName + "' Table not found");
return new ReturnMsg(0, "'" + this.tableName + "'" + TABLE_NOT_FOUND);
}
HTable table = new HTable(conf, this.tableName);
HBaseAdmin admin = new HBaseAdmin(conf);
int count = 0;
@ -95,7 +93,7 @@ public class SelectCommand extends BasicCommand {
} else {
count = scanPrint(table, admin);
}
return new ReturnMsg(1, Integer.toString(count) + " row(s) in set");
return new ReturnMsg(1, Integer.toString(count) + " row(s) in set.");
} catch (IOException e) {
String[] msg = e.getMessage().split("[,]");
return new ReturnMsg(0, msg[0]);
@ -110,27 +108,26 @@ public class SelectCommand extends BasicCommand {
byte[][] result = null;
ParsedColumns parsedColumns = getColumns(admin, false);
boolean multiple = parsedColumns.isMultiple() || this.version > 1;
formatter.header(multiple? HEADER_COLUMN_CELL: null);
for (Text column: parsedColumns.getColumns()) {
formatter.header(multiple ? HEADER_COLUMN_CELL : null);
for (Text column : parsedColumns.getColumns()) {
if (this.timestamp != 0) {
result = table.get(this.rowKey, column, this.timestamp,
this.version);
result = table.get(this.rowKey, column, this.timestamp, this.version);
} else {
result = table.get(this.rowKey, column, this.version);
}
for (int ii = 0; result != null && ii < result.length; ii++) {
if (multiple) {
formatter.row(new String [] {column.toString(),
toString(column, result[ii])});
formatter.row(new String[] { column.toString(),
toString(column, result[ii]) });
} else {
formatter.row(new String [] {toString(column, result[ii])});
formatter.row(new String[] { toString(column, result[ii]) });
}
count++;
}
}
} else {
formatter.header(isMultiple()? HEADER_COLUMN_CELL: null);
for (Map.Entry<Text, byte[]> e: table.getRow(this.rowKey).entrySet()) {
formatter.header(isMultiple() ? HEADER_COLUMN_CELL : null);
for (Map.Entry<Text, byte[]> e : table.getRow(this.rowKey).entrySet()) {
Text key = e.getKey();
String keyStr = key.toString();
if (!this.columns.contains(STAR) && !this.columns.contains(keyStr)) {
@ -138,9 +135,9 @@ public class SelectCommand extends BasicCommand {
}
String cellData = toString(key, e.getValue());
if (isMultiple()) {
formatter.row(new String [] {key.toString(), cellData});
formatter.row(new String[] { key.toString(), cellData });
} else {
formatter.row(new String [] {cellData});
formatter.row(new String[] { cellData });
}
count++;
}
@ -151,13 +148,13 @@ public class SelectCommand extends BasicCommand {
}
return count;
}
private String toString(final Text columnName, final byte [] cell)
throws IOException {
private String toString(final Text columnName, final byte[] cell)
throws IOException {
String result = null;
if (columnName.equals(HConstants.COL_REGIONINFO) ||
columnName.equals(HConstants.COL_SPLITA) ||
columnName.equals(HConstants.COL_SPLITA)) {
if (columnName.equals(HConstants.COL_REGIONINFO)
|| columnName.equals(HConstants.COL_SPLITA)
|| columnName.equals(HConstants.COL_SPLITA)) {
result = Writables.getHRegionInfoOrNull(cell).toString();
} else if (columnName.equals(HConstants.COL_STARTCODE)) {
result = Long.toString(Writables.bytesToLong(cell));
@ -166,40 +163,39 @@ public class SelectCommand extends BasicCommand {
}
return result;
}
/**
* Data structure with columns to use scanning and whether or not the
* scan could return more than one column.
* Data structure with columns to use scanning and whether or not the scan
* could return more than one column.
*/
class ParsedColumns {
private final List<Text> cols;
private final boolean isMultiple;
ParsedColumns(final List<Text> columns) {
this(columns, true);
}
ParsedColumns(final List<Text> columns, final boolean isMultiple) {
this.cols = columns;
this.isMultiple = isMultiple;
}
public List<Text> getColumns() {
return this.cols;
}
public boolean isMultiple() {
return this.isMultiple;
}
}
private int scanPrint(HTable table,
HBaseAdmin admin) {
private int scanPrint(HTable table, HBaseAdmin admin) {
int count = 0;
HScannerInterface scan = null;
try {
ParsedColumns parsedColumns = getColumns(admin, true);
Text [] cols = parsedColumns.getColumns().toArray(new Text [] {});
Text[] cols = parsedColumns.getColumns().toArray(new Text[] {});
if (this.timestamp == 0) {
scan = table.obtainScanner(cols, this.rowKey);
} else {
@ -208,17 +204,17 @@ public class SelectCommand extends BasicCommand {
HStoreKey key = new HStoreKey();
TreeMap<Text, byte[]> results = new TreeMap<Text, byte[]>();
// If only one column in query, then don't print out the column.
formatter.header((parsedColumns.isMultiple())? HEADER: HEADER_ROW_CELL);
formatter.header((parsedColumns.isMultiple()) ? HEADER : HEADER_ROW_CELL);
while (scan.next(key, results) && checkLimit(count)) {
Text r = key.getRow();
for (Text columnKey : results.keySet()) {
String cellData = toString(columnKey, results.get(columnKey));
if (parsedColumns.isMultiple()) {
formatter.row(new String [] {r.toString(), columnKey.toString(),
cellData});
formatter.row(new String[] { r.toString(), columnKey.toString(),
cellData });
} else {
// Don't print out the column since only one specified in query.
formatter.row(new String [] {r.toString(), cellData});
formatter.row(new String[] { r.toString(), cellData });
}
count++;
if (this.limit > 0 && count >= this.limit) {
@ -238,24 +234,24 @@ public class SelectCommand extends BasicCommand {
/**
* Make sense of the supplied list of columns.
*
* @param admin Admin to use.
* @return Interpretation of supplied list of columns.
*/
public ParsedColumns getColumns(final HBaseAdmin admin,
final boolean scanning) {
public ParsedColumns getColumns(final HBaseAdmin admin, final boolean scanning) {
ParsedColumns result = null;
try {
if (this.columns.contains("*")) {
if (this.tableName.equals(HConstants.ROOT_TABLE_NAME)
|| this.tableName.equals(HConstants.META_TABLE_NAME)) {
result =
new ParsedColumns(Arrays.asList(HConstants.COLUMN_FAMILY_ARRAY));
result = new ParsedColumns(Arrays
.asList(HConstants.COLUMN_FAMILY_ARRAY));
} else {
HTableDescriptor[] tables = admin.listTables();
for (int i = 0; i < tables.length; i++) {
if (tables[i].getName().equals(this.tableName)) {
result = new ParsedColumns(new ArrayList<Text>(tables[i].
families().keySet()));
result = new ParsedColumns(new ArrayList<Text>(tables[i].families()
.keySet()));
break;
}
}
@ -264,15 +260,16 @@ public class SelectCommand extends BasicCommand {
List<Text> tmpList = new ArrayList<Text>();
for (int i = 0; i < this.columns.size(); i++) {
Text column = null;
// Add '$' to column name if we are scanning. Scanners support
// regex column names. Adding '$', the column becomes a
// Add '$' to column name if we are scanning. Scanners support
// regex column names. Adding '$', the column becomes a
// regex that does an explicit match on the supplied column name.
// Otherwise, if the specified column is a column family, then
// default behavior is to fetch all columns that have a matching
// column family.
column = (this.columns.get(i).contains(":"))?
new Text(this.columns.get(i) + (scanning? "$": "")):
new Text(this.columns.get(i) + ":" + (scanning? "$": ""));
column = (this.columns.get(i).contains(":")) ? new Text(this.columns
.get(i)
+ (scanning ? "$" : "")) : new Text(this.columns.get(i) + ":"
+ (scanning ? "$" : ""));
tmpList.add(column);
}
result = new ParsedColumns(tmpList, tmpList.size() > 1);
@ -282,7 +279,7 @@ public class SelectCommand extends BasicCommand {
}
return result;
}
/*
* @return True if query contains multiple columns.
*/
@ -291,7 +288,7 @@ public class SelectCommand extends BasicCommand {
}
private boolean checkLimit(int count) {
return (this.limit == 0)? true: (this.limit > count) ? true : false;
return (this.limit == 0) ? true : (this.limit > count) ? true : false;
}
public void setTable(String table) {
@ -316,8 +313,8 @@ public class SelectCommand extends BasicCommand {
}
public void setRowKey(String rowKey) {
if(rowKey == null)
this.rowKey = null;
if (rowKey == null)
this.rowKey = null;
else
this.rowKey = new Text(rowKey);
}
@ -328,18 +325,17 @@ public class SelectCommand extends BasicCommand {
public void setVersion(int version) {
this.version = version;
}
public static void main(String[] args) throws Exception {
Writer out = new OutputStreamWriter(System.out, "UTF-8");
HBaseConfiguration c = new HBaseConfiguration();
// For debugging
TableFormatterFactory tff =
new TableFormatterFactory(out, c);
Parser parser = new Parser("select * from 'x' where row='x';", out, tff.get());
TableFormatterFactory tff = new TableFormatterFactory(out, c);
Parser parser = new Parser("select * from 'x' where row='x';", out, tff.get());
Command cmd = parser.terminatedCommand();
ReturnMsg rm = cmd.execute(c);
out.write(rm == null? "": rm.toString());
out.write(rm == null ? "" : rm.toString());
out.flush();
}
}

View File

@ -45,8 +45,7 @@ public class ShowCommand extends BasicCommand {
this(o, f, null);
}
public ShowCommand(final Writer o, final TableFormatter f,
final String argument) {
public ShowCommand(final Writer o, final TableFormatter f, final String argument) {
super(o);
this.formatter = f;
this.command = argument;
@ -54,7 +53,7 @@ public class ShowCommand extends BasicCommand {
public ReturnMsg execute(final HBaseConfiguration conf) {
if (this.command == null) {
return new ReturnMsg(0, "Syntax error : Please check 'Show' syntax");
return new ReturnMsg(0, "Syntax error : Please check 'Show' syntax.");
}
try {
HBaseAdmin admin = new HBaseAdmin(conf);
@ -63,7 +62,7 @@ public class ShowCommand extends BasicCommand {
HTableDescriptor[] tables = admin.listTables();
tableLength = tables.length;
if (tableLength == 0) {
return new ReturnMsg(0, "No tables found");
return new ReturnMsg(0, "No tables found.");
}
formatter.header(HEADER);
for (int i = 0; i < tableLength; i++) {
@ -71,7 +70,7 @@ public class ShowCommand extends BasicCommand {
formatter.row(new String[] { tableName, tables[i].toString() });
}
formatter.footer();
return new ReturnMsg(1, tableLength + " table(s) in set");
return new ReturnMsg(1, tableLength + " table(s) in set.");
} else {
Map<String, VariableRef> refer = VariablesPool.get(command);
if (refer == null) {

View File

@ -67,7 +67,8 @@ public class SubstituteCommand extends BasicCommand {
public void resetVariableRelation(String r1, String r2) {
setChainKey(r1);
String tableName = VariablesPool.get(r1).get(null).getArgument();
VariableRef formula = new VariableRef(Constants.JOIN_SECOND_RELATION, tableName);
VariableRef formula = new VariableRef(Constants.JOIN_SECOND_RELATION,
tableName);
VariablesPool.put(r1, r2, formula);
}

View File

@ -27,33 +27,37 @@ import org.apache.hadoop.hbase.shell.formatter.AsciiTableFormatter;
/**
* Interface implemented by table formatters outputting select results.
* Implementations must have a constructor that takes a Writer.
*
* @see AsciiTableFormatter
*/
public interface TableFormatter {
/**
* Output header.
*
* @param titles Titles to emit.
* @throws IOException
*/
public void header(final String [] titles) throws IOException;
public void header(final String[] titles) throws IOException;
/**
* Output footer.
*
* @throws IOException
*/
public void footer() throws IOException;
/**
* Output a row.
*
* @param cells
* @throws IOException
*/
public void row(final String [] cells) throws IOException;
public void row(final String[] cells) throws IOException;
/**
* @return Output stream being used (This is in interface to enforce fact
* that formatters use Writers -- that they operate on character streams
* rather than on byte streams).
* @return Output stream being used (This is in interface to enforce fact that
* formatters use Writers -- that they operate on character streams
* rather than on byte streams).
*/
public Writer getOut();
}
}

View File

@ -28,34 +28,34 @@ import org.apache.hadoop.conf.Configuration;
import org.apache.hadoop.hbase.shell.formatter.AsciiTableFormatter;
/**
* Table formatter.
* Specify formatter by setting "hbaseshell.formatter" property in
* <code>hbase-site.xml</code> or by setting system property
* <code>hbaseshell.formatter</code>. System property setting prevails over all
* other configurations. Outputs UTF-8 encoded Strings even if
* original data is binary. On static initialization, changes System.out to be
* a UTF-8 output stream.
* .
* <p>TODO: Mysql has --skip-column-names and --silent which inserts a tab as
* separator. Also has --html and --xml.
* <p>To use the html formatter, currently set HBASE_OPTS as in:
* Table formatter. Specify formatter by setting "hbaseshell.formatter" property
* in <code>hbase-site.xml</code> or by setting system property
* <code>hbaseshell.formatter</code>. System property setting prevails over
* all other configurations. Outputs UTF-8 encoded Strings even if original data
* is binary. On static initialization, changes System.out to be a UTF-8 output
* stream. .
* <p>
* TODO: Mysql has --skip-column-names and --silent which inserts a tab as
* separator. Also has --html and --xml.
* <p>
* To use the html formatter, currently set HBASE_OPTS as in:
* <code>$ HBASE_OPTS="-Dhbaseshell.formatter=org.apache.hadoop.hbase.shell.formatter.HtmlTableFormatter" ./bin/hbase shell</code>
* </p>
*/
public class TableFormatterFactory {
private static final Log LOG =
LogFactory.getLog(TableFormatterFactory.class.getName());
private static final Log LOG = LogFactory.getLog(TableFormatterFactory.class
.getName());
private static final String FORMATTER_KEY = "hbaseshell.formatter";
private final TableFormatter formatter;
/**
* Not instantiable
*/
@SuppressWarnings({ "unchecked", "unused" })
@SuppressWarnings( { "unchecked", "unused" })
private TableFormatterFactory() {
this(null, null);
}
@SuppressWarnings("unchecked")
public TableFormatterFactory(final Writer out, final Configuration c) {
String className = System.getProperty(FORMATTER_KEY);
@ -64,10 +64,10 @@ public class TableFormatterFactory {
}
LOG.debug("Table formatter class: " + className);
try {
Class<TableFormatter> clazz =
(Class<TableFormatter>) Class.forName(className);
Class<TableFormatter> clazz = (Class<TableFormatter>) Class
.forName(className);
Constructor<?> constructor = clazz.getConstructor(Writer.class);
this.formatter = (TableFormatter)constructor.newInstance(out);
this.formatter = (TableFormatter) constructor.newInstance(out);
} catch (Exception e) {
throw new RuntimeException("Failed instantiation of " + className, e);
}
@ -80,4 +80,4 @@ public class TableFormatterFactory {
public TableFormatter get() {
return this.formatter;
}
}
}

View File

@ -34,6 +34,7 @@ public class VariableRef {
/**
* Return argument of an operation
*
* @return argument
*/
public String getArgument() {
@ -42,6 +43,7 @@ public class VariableRef {
/**
* Return operation
*
* @return operation
*/
public String getOperation() {