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;
@ -64,22 +65,21 @@ public class Shell {
"";
}
/**
* 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) {

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,7 +53,7 @@ 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);
@ -63,16 +65,15 @@ public class AlterCommand extends SchemaModificationCommand {
columns = columnSpecMap.keySet();
for (String c : columns) {
columnDesc = getColumnDescriptor(c, columnSpecMap.get(c));
println("Adding " + c + " to " + tableName +
"... Please wait.");
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.");
println("Dropping " + column + " from " + tableName
+ "... Please wait.");
column = appendDelimiter(column);
admin.deleteColumn(new Text(tableName), new Text(column));
enableTable(admin, tableName);

View File

@ -25,11 +25,14 @@ 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")
@ -39,6 +42,7 @@ public abstract class BasicCommand implements Command, CommandFactory {
/**
* Constructor
*
* @param o A Writer.
*/
public BasicCommand(final Writer o) {
@ -65,12 +69,12 @@ public abstract class BasicCommand implements Command, CommandFactory {
}
/**
* 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;
}
/**

View File

@ -32,7 +32,8 @@ public class ClearCommand extends BasicCommand {
super(o);
}
public ReturnMsg execute(@SuppressWarnings("unused") HBaseConfiguration conf) {
public ReturnMsg execute(@SuppressWarnings("unused")
HBaseConfiguration conf) {
clear();
return null;
}

View File

@ -25,9 +25,13 @@ public interface Command {
/** family indicator */
public static final String FAMILY_INDICATOR = ":";
public enum CommandType {DDL, UPDATE, SELECT, INSERT, DELETE, SHELL}
public enum CommandType {
DDL, UPDATE, SELECT, INSERT, DELETE, SHELL
}
/** Execute a command
/**
* Execute a command
*
* @param conf Configuration
* @return Result of command execution
*/

View File

@ -37,8 +37,7 @@ 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);
@ -48,7 +47,7 @@ public class CreateCommand extends SchemaModificationCommand {
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);
@ -64,14 +63,14 @@ public class CreateCommand extends SchemaModificationCommand {
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) {
@ -80,6 +79,7 @@ public class CreateCommand extends SchemaModificationCommand {
/**
* Adds a column specification.
*
* @param columnSpec Column specification
*/
public void addColumnSpec(String column, Map<String, Object> columnSpec) {

View File

@ -50,7 +50,7 @@ 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);
@ -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++) {

View File

@ -33,8 +33,7 @@ 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;
@ -51,24 +50,24 @@ public class DescCommand extends BasicCommand {
public ReturnMsg execute(final HBaseConfiguration conf) {
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 {
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());
}

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;
/**
@ -40,6 +42,11 @@ public class DisableCommand extends BasicCommand {
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));

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;
/**
@ -39,18 +41,29 @@ public class DropCommand extends BasicCommand {
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) {
if (!conn.tableExists(new Text(table))) {
println("'" + table + "' table not found.");
} else {
println("Dropping " + table + "... Please wait.");
admin.deleteTable(new Text(table));
count++;
}
}
return new ReturnMsg(1, "Table(s) dropped successfully.");
if (count > 0) {
return new ReturnMsg(1, count + " table(s) dropped successfully.");
} else {
return new ReturnMsg(0, count + " table(s) dropped.");
}
} catch (IOException e) {
return new ReturnMsg(0, extractErrMsg(e));
}

View File

@ -43,7 +43,7 @@ public class EnableCommand 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);

View File

@ -28,10 +28,11 @@ public class ExitCommand extends BasicCommand {
super(o);
}
public ReturnMsg execute(@SuppressWarnings("unused") HBaseConfiguration conf) {
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;
}

View File

@ -36,7 +36,8 @@ public class FsCommand extends BasicCommand {
super(o);
}
public ReturnMsg execute(@SuppressWarnings("unused") HBaseConfiguration conf) {
public ReturnMsg execute(@SuppressWarnings("unused")
HBaseConfiguration conf) {
// This commmand will write the
FsShell shell = new FsShell();
try {

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,11 +147,8 @@ 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[] {
// 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']);" });

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) + ":");

View File

@ -49,7 +49,8 @@ public class JarCommand extends BasicCommand {
}
@SuppressWarnings("deprecation")
public ReturnMsg execute(@SuppressWarnings("unused") HBaseConfiguration conf) {
public ReturnMsg execute(@SuppressWarnings("unused")
HBaseConfiguration conf) {
try {
String[] args = getQuery();
@ -67,7 +68,7 @@ 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);
}
@ -111,7 +112,7 @@ public class JarCommand extends BasicCommand {
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,22 +121,19 @@ 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();
}

View File

@ -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);
}
}

View File

@ -55,12 +55,10 @@ 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;
@ -77,14 +75,14 @@ public class SelectCommand extends BasicCommand {
}
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);
@ -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++;
}
@ -152,12 +149,12 @@ public class SelectCommand extends BasicCommand {
return count;
}
private String toString(final Text columnName, final byte [] cell)
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));
@ -168,8 +165,8 @@ public class SelectCommand extends BasicCommand {
}
/**
* 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;
@ -193,13 +190,12 @@ public class SelectCommand extends BasicCommand {
}
}
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;
}
}
@ -270,9 +266,10 @@ public class SelectCommand extends BasicCommand {
// 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);
@ -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,7 +313,7 @@ public class SelectCommand extends BasicCommand {
}
public void setRowKey(String rowKey) {
if(rowKey == null)
if (rowKey == null)
this.rowKey = null;
else
this.rowKey = new Text(rowKey);
@ -333,13 +330,12 @@ public class SelectCommand extends BasicCommand {
Writer out = new OutputStreamWriter(System.out, "UTF-8");
HBaseConfiguration c = new HBaseConfiguration();
// For debugging
TableFormatterFactory tff =
new TableFormatterFactory(out, c);
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,32 +27,36 @@ 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
* @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,30 +28,30 @@ 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
* 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:
* <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);
}
@ -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);
}

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() {