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

View File

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

View File

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

View File

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

View File

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

View File

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

View File

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

View File

@ -33,42 +33,41 @@ import org.apache.hadoop.io.Text;
* Prints information about tables. * Prints information about tables.
*/ */
public class DescCommand extends BasicCommand { public class DescCommand extends BasicCommand {
private static final String [] HEADER = private static final String[] HEADER = new String[] { "Column Family Descriptor" };
new String [] {"Column Family Descriptor"};
private Text tableName; private Text tableName;
private final TableFormatter formatter; private final TableFormatter formatter;
// Not instantiable // Not instantiable
@SuppressWarnings("unused") @SuppressWarnings("unused")
private DescCommand() { private DescCommand() {
this(null, null); this(null, null);
} }
public DescCommand(final Writer o, final TableFormatter f) { public DescCommand(final Writer o, final TableFormatter f) {
super(o); super(o);
this.formatter = f; this.formatter = f;
} }
public ReturnMsg execute(final HBaseConfiguration conf) { public ReturnMsg execute(final HBaseConfiguration conf) {
if (this.tableName == null) if (this.tableName == null)
return new ReturnMsg(0, "Syntax error : Please check 'Describe' syntax"); return new ReturnMsg(0, "Syntax error : Please check 'Describe' syntax.");
try { try {
HConnection conn = HConnectionManager.getConnection(conf); HConnection conn = HConnectionManager.getConnection(conf);
if (!conn.tableExists(this.tableName)) { if (!conn.tableExists(this.tableName)) {
return new ReturnMsg(0, "Table not found"); return new ReturnMsg(0, "Table not found.");
} }
HTableDescriptor [] tables = conn.listTables(); HTableDescriptor[] tables = conn.listTables();
HColumnDescriptor [] columns = null; HColumnDescriptor[] columns = null;
for (int i = 0; i < tables.length; i++) { for (int i = 0; i < tables.length; i++) {
if (tables[i].getName().equals(this.tableName)) { if (tables[i].getName().equals(this.tableName)) {
columns = tables[i].getFamilies().values(). columns = tables[i].getFamilies().values().toArray(
toArray(new HColumnDescriptor [] {}); new HColumnDescriptor[] {});
break; break;
} }
} }
formatter.header(HEADER); formatter.header(HEADER);
// Do a toString on the HColumnDescriptors // 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++) { for (int i = 0; i < columns.length; i++) {
String tmp = columns[i].toString(); String tmp = columns[i].toString();
// Strip the curly-brackets if present. // Strip the curly-brackets if present.
@ -76,10 +75,10 @@ public class DescCommand extends BasicCommand {
tmp = tmp.substring(1, tmp.length() - 1); tmp = tmp.substring(1, tmp.length() - 1);
} }
columnStrs[i] = tmp; columnStrs[i] = tmp;
formatter.row(new String [] {columnStrs[i]}); formatter.row(new String[] { columnStrs[i] });
} }
formatter.footer(); 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) { } catch (IOException e) {
return new ReturnMsg(0, "error msg : " + e.toString()); return new ReturnMsg(0, "error msg : " + e.toString());
} }
@ -88,4 +87,4 @@ public class DescCommand extends BasicCommand {
public void setArgument(String table) { public void setArgument(String table) {
this.tableName = new Text(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.HBaseAdmin;
import org.apache.hadoop.hbase.HBaseConfiguration; import org.apache.hadoop.hbase.HBaseConfiguration;
import org.apache.hadoop.hbase.HConnection;
import org.apache.hadoop.hbase.HConnectionManager;
import org.apache.hadoop.io.Text; import org.apache.hadoop.io.Text;
/** /**
@ -31,18 +33,23 @@ import org.apache.hadoop.io.Text;
*/ */
public class DisableCommand extends BasicCommand { public class DisableCommand extends BasicCommand {
private String tableName; private String tableName;
public DisableCommand(Writer o) { public DisableCommand(Writer o) {
super(o); super(o);
} }
public ReturnMsg execute(HBaseConfiguration conf) { public ReturnMsg execute(HBaseConfiguration conf) {
assert tableName != null; assert tableName != null;
try { 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); HBaseAdmin admin = new HBaseAdmin(conf);
admin.disableTable(new Text(tableName)); admin.disableTable(new Text(tableName));
return new ReturnMsg(1, "Table disabled successfully."); return new ReturnMsg(1, "Table disabled successfully.");
} catch (IOException e) { } catch (IOException e) {
String[] msg = e.getMessage().split("[\n]"); String[] msg = e.getMessage().split("[\n]");
@ -53,9 +60,9 @@ public class DisableCommand extends BasicCommand {
public void setTable(String table) { public void setTable(String table) {
this.tableName = table; this.tableName = table;
} }
@Override @Override
public CommandType getCommandType() { public CommandType getCommandType() {
return CommandType.DDL; return CommandType.DDL;
} }
} }

View File

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

View File

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

View File

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

View File

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

View File

@ -30,8 +30,8 @@ import org.apache.hadoop.hbase.HBaseConfiguration;
public class HelpCommand extends BasicCommand { public class HelpCommand extends BasicCommand {
private String argument; private String argument;
private static final String[] HEADER = new String[] { "Command", private static final String[] HEADER = new String[] { "Command", "Description",
"Description", "Example" }; "Example" };
/** application name */ /** application name */
public static final String APP_NAME = "Hbase Shell"; public static final String APP_NAME = "Hbase Shell";
@ -74,10 +74,10 @@ public class HelpCommand extends BasicCommand {
load.put("FS", new String[] { load.put("FS", new String[] {
"Hadoop FsShell; entering a lone 'FS;' " + "will emit usage", "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", 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("CLEAR", new String[] { "Clear the screen", "CLEAR;" });
load.put("DESCRIBE", new String[] { "Print table information", load.put("DESCRIBE", new String[] { "Print table information",
@ -125,11 +125,12 @@ public class HelpCommand extends BasicCommand {
// model of // model of
// data. // data.
load.put("TABLE", load
new String[] { "Load a table", "A = table('table_name');" }); .put("TABLE", new String[] { "Load a table", "A = table('table_name');" });
load.put("SUBSTITUTE", new String[] { "Substitute expression to [A~Z]", load.put("SUBSTITUTE", new String[] { "Substitute expression to [A~Z]",
"D = A.projection('cf_name1'[, 'cf_name2']);" }); "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');" }); "SAVE A INTO table('table_name');" });
// Relational Operations // Relational Operations
@ -146,14 +147,11 @@ public class HelpCommand extends BasicCommand {
+ " B = A.Selection(cf_name1 > 100 [AND cf_name2 = 'string_value']);" }); + " B = A.Selection(cf_name1 > 100 [AND cf_name2 = 'string_value']);" });
// Aggregation Functions // Aggregation Functions
//TODO : and apply aggregate function independently to each group of rows // TODO : and apply aggregate function independently to each group of rows
load load.put("GROUP", new String[] {
.put( "Group rows by value of an attribute",
"GROUP", "A = Table('table_name');"
new String[] { + " B = Group A by ('cf_name1'[, 'cf_name2']);" });
"Group rows by value of an attribute",
"A = Table('table_name');"
+ " B = Group A by ('cf_name1'[, 'cf_name2']);" });
return load; return load;
} }

View File

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

View File

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

View File

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

View File

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

View File

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

View File

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

View File

@ -67,7 +67,8 @@ public class SubstituteCommand extends BasicCommand {
public void resetVariableRelation(String r1, String r2) { public void resetVariableRelation(String r1, String r2) {
setChainKey(r1); setChainKey(r1);
String tableName = VariablesPool.get(r1).get(null).getArgument(); 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); 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. * Interface implemented by table formatters outputting select results.
* Implementations must have a constructor that takes a Writer. * Implementations must have a constructor that takes a Writer.
*
* @see AsciiTableFormatter * @see AsciiTableFormatter
*/ */
public interface TableFormatter { public interface TableFormatter {
/** /**
* Output header. * Output header.
*
* @param titles Titles to emit. * @param titles Titles to emit.
* @throws IOException * @throws IOException
*/ */
public void header(final String [] titles) throws IOException; public void header(final String[] titles) throws IOException;
/** /**
* Output footer. * Output footer.
*
* @throws IOException * @throws IOException
*/ */
public void footer() throws IOException; public void footer() throws IOException;
/** /**
* Output a row. * Output a row.
*
* @param cells * @param cells
* @throws IOException * @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 * @return Output stream being used (This is in interface to enforce fact that
* that formatters use Writers -- that they operate on character streams * formatters use Writers -- that they operate on character streams
* rather than on byte streams). * rather than on byte streams).
*/ */
public Writer getOut(); public Writer getOut();
} }

View File

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

View File

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