HADOOP-1672 HBase Shell should use new client classes

Use HTable and HTableAdmin to do what HClient used (Removed all
references to HClient).



git-svn-id: https://svn.apache.org/repos/asf/lucene/hadoop/trunk/src/contrib/hbase@566467 13f79535-47bb-0310-9956-ffa450edef68
This commit is contained in:
Michael Stack 2007-08-16 01:56:29 +00:00
parent be33a241ce
commit 9dedf26b0f
18 changed files with 137 additions and 106 deletions

View File

@ -96,3 +96,5 @@ Trunk (unreleased changes)
59. HADOOP-1711 HTable API should use interfaces instead of concrete classes as
method parameters and return values
60. HADOOP-1644 Compactions should not block updates
60. HADOOP-1672 HBase Shell should use new client classes
(Edward Yoon via Stack).

View File

@ -43,7 +43,6 @@ public class Shell {
/** Main method */
public static void main(String args[]) throws IOException {
Configuration conf = new HBaseConfiguration();
HClient client = new HClient(conf);
ConsoleReader reader = new ConsoleReader();
reader.setBellEnabled(conf.getBoolean("hbaseshell.jline.bell.enabled",
DEFAULT_BELL_ENABLED));
@ -60,7 +59,7 @@ public class Shell {
try {
Command cmd = parser.terminatedCommand();
if (cmd != null) {
rs = cmd.execute(client);
rs = cmd.execute(conf);
}
} catch (ParseException pe) {
String[] msg = pe.getMessage().split("[\n]");

View File

@ -21,11 +21,11 @@ package org.apache.hadoop.hbase.shell;
import java.io.IOException;
import org.apache.hadoop.hbase.HClient;
import org.apache.hadoop.conf.Configuration;
public class ClearCommand extends BasicCommand {
public ReturnMsg execute(HClient client) {
public ReturnMsg execute(Configuration conf) {
clear();
return null;
}
@ -34,7 +34,7 @@ public class ClearCommand extends BasicCommand {
String osName = System.getProperty("os.name");
if (osName.length() > 7 && osName.subSequence(0, 7).equals("Windows")) {
try {
Runtime.getRuntime().exec("cls");
Runtime.getRuntime().exec("cmd /C cls");
} catch (IOException e) {
System.out.println("Can't clear." + e.toString());
}

View File

@ -19,12 +19,14 @@
*/
package org.apache.hadoop.hbase.shell;
import org.apache.hadoop.hbase.HClient;
import org.apache.hadoop.conf.Configuration;
public interface Command {
/** family indicator */
public static final String FAMILY_INDICATOR = ":";
/** Execute a command */
public ReturnMsg execute(HClient client);
public ReturnMsg execute(Configuration conf);
}

View File

@ -23,5 +23,7 @@ package org.apache.hadoop.hbase.shell;
* Parser uses command factories to create command.
*/
public interface CommandFactory {
Command getCommand();
}

View File

@ -23,6 +23,7 @@ package org.apache.hadoop.hbase.shell;
* Manufactures console table, but stupid.
*/
public class ConsoleTable {
public static void printHead(String name) {
System.out.println("+------+----------------------+");
System.out.print("| No. | ");
@ -187,4 +188,5 @@ public class ConsoleTable {
"----------------------+----------------------+");
System.out.println();
}
}

View File

@ -22,36 +22,41 @@ package org.apache.hadoop.hbase.shell;
import java.io.IOException;
import java.util.List;
import org.apache.hadoop.hbase.HClient;
import org.apache.hadoop.conf.Configuration;
import org.apache.hadoop.hbase.HBaseAdmin;
import org.apache.hadoop.hbase.HColumnDescriptor;
import org.apache.hadoop.hbase.HConnection;
import org.apache.hadoop.hbase.HConnectionManager;
import org.apache.hadoop.hbase.HTableDescriptor;
import org.apache.hadoop.io.Text;
public class CreateCommand extends BasicCommand {
String table;
List<String> columnfamilies;
private Text table;
private List<String> columnfamilies;
@SuppressWarnings("unused")
private int limit;
int limit;
public ReturnMsg execute(HClient client) {
public ReturnMsg execute(Configuration conf) {
if (this.table == null || this.columnfamilies == null)
return new ReturnMsg(0, "Syntax error : Please check 'Create' syntax.");
try {
HTableDescriptor desc = new HTableDescriptor(this.table);
HConnection conn = HConnectionManager.getConnection(conf);
HBaseAdmin admin = new HBaseAdmin(conf);
if (conn.tableExists(this.table)) {
return new ReturnMsg(0, "Table was already exsits.");
}
HTableDescriptor desc = new HTableDescriptor(this.table.toString());
for (int i = 0; i < this.columnfamilies.size(); i++) {
String columnFamily = columnfamilies.get(i);
if (columnFamily.lastIndexOf(':') == (columnFamily.length() - 1)) {
columnFamily = columnFamily.substring(0, columnFamily.length() - 1);
}
desc.addFamily(new HColumnDescriptor(columnFamily + FAMILY_INDICATOR));
}
client.createTable(desc);
admin.createTable(desc);
return new ReturnMsg(1, "Table created successfully.");
} catch (IOException e) {
return new ReturnMsg(0, "error msg : " + e.toString());
@ -59,7 +64,7 @@ public class CreateCommand extends BasicCommand {
}
public void setTable(String table) {
this.table = table;
this.table = new Text(table);
}
public void setColumnfamilies(List<String> columnfamilies) {
@ -69,4 +74,5 @@ public class CreateCommand extends BasicCommand {
public void setLimit(int limit) {
this.limit = limit;
}
}

View File

@ -24,36 +24,35 @@ import java.util.List;
import java.util.Map;
import java.util.Set;
import org.apache.hadoop.hbase.HClient;
import org.apache.hadoop.conf.Configuration;
import org.apache.hadoop.hbase.HTable;
import org.apache.hadoop.io.Text;
public class DeleteCommand extends BasicCommand {
String table;
Map<String, List<String>> condition;
private Text table;
private Map<String, List<String>> condition;
public ReturnMsg execute(HClient client) {
public ReturnMsg execute(Configuration conf) {
if (this.table == null || condition == null)
return new ReturnMsg(0, "Syntax error : Please check 'Delete' syntax.");
try {
client.openTable(new Text(this.table));
long lockId = client.startUpdate(getRow());
HTable table = new HTable(conf, this.table);
long lockId = table.startUpdate(getRow());
if (getColumn() != null) {
client.delete(lockId, getColumn());
table.delete(lockId, getColumn());
} else {
Set<Text> keySet = client.getRow(getRow()).keySet();
Set<Text> keySet = table.getRow(getRow()).keySet();
Text[] columnKey = keySet.toArray(new Text[keySet.size()]);
for (int i = 0; i < columnKey.length; i++) {
client.delete(lockId, columnKey[i]);
table.delete(lockId, columnKey[i]);
}
}
client.commit(lockId);
table.commit(lockId);
return new ReturnMsg(1, "1 deleted successfully. ");
} catch (IOException e) {
@ -62,7 +61,7 @@ public class DeleteCommand extends BasicCommand {
}
public void setTable(String table) {
this.table = table;
this.table = new Text(table);
}
public void setCondition(Map<String, List<String>> cond) {
@ -80,4 +79,5 @@ public class DeleteCommand extends BasicCommand {
return null;
}
}
}

View File

@ -21,31 +21,36 @@ package org.apache.hadoop.hbase.shell;
import java.io.IOException;
import org.apache.hadoop.hbase.HClient;
import org.apache.hadoop.conf.Configuration;
import org.apache.hadoop.hbase.HConnection;
import org.apache.hadoop.hbase.HConnectionManager;
import org.apache.hadoop.hbase.HTableDescriptor;
import org.apache.hadoop.io.Text;
public class DescCommand extends BasicCommand {
String argument;
public ReturnMsg execute(HClient client) {
if (this.argument == null)
private Text table;
public ReturnMsg execute(Configuration conf) {
if (this.table == null)
return new ReturnMsg(0, "Syntax error : Please check 'Describe' syntax.");
try {
HTableDescriptor[] tables = client.listTables();
HConnection conn = HConnectionManager.getConnection(conf);
if (!conn.tableExists(this.table)) {
return new ReturnMsg(0, "Table not found.");
}
HTableDescriptor[] tables = conn.listTables();
Text[] columns = null;
for (int i = 0; i < tables.length; i++) {
if (tables[i].getName().toString().equals(this.argument)) {
if (tables[i].getName().equals(this.table)) {
columns = tables[i].families().keySet().toArray(new Text[] {});
}
}
if (columns == null) {
return new ReturnMsg(0, "Table not found.");
}
ConsoleTable.printHead("ColumnFamily Name");
for (int ii = 0; ii < columns.length; ii++) {
String familyName = columns[ii].toString().replace(FAMILY_INDICATOR, "");
@ -59,7 +64,8 @@ public class DescCommand extends BasicCommand {
}
}
public void setArgument(String argument) {
this.argument = argument;
public void setArgument(String table) {
this.table = new Text(table);
}
}

View File

@ -21,18 +21,21 @@ package org.apache.hadoop.hbase.shell;
import java.io.IOException;
import org.apache.hadoop.hbase.HClient;
import org.apache.hadoop.conf.Configuration;
import org.apache.hadoop.hbase.HBaseAdmin;
import org.apache.hadoop.io.Text;
public class DropCommand extends BasicCommand {
String argument;
public ReturnMsg execute(HClient client) {
if (this.argument == null)
private Text table;
public ReturnMsg execute(Configuration conf) {
if (this.table == null)
return new ReturnMsg(0, "Syntax error : Please check 'Drop' syntax.");
try {
client.deleteTable(new Text(this.argument));
HBaseAdmin admin = new HBaseAdmin(conf);
admin.deleteTable(this.table);
return new ReturnMsg(1, "Table droped successfully.");
} catch (IOException e) {
@ -40,7 +43,8 @@ public class DropCommand extends BasicCommand {
}
}
public void setArgument(String argument) {
this.argument = argument;
public void setArgument(String table) {
this.table = new Text(table);
}
}

View File

@ -19,11 +19,11 @@
*/
package org.apache.hadoop.hbase.shell;
import org.apache.hadoop.hbase.HClient;
import org.apache.hadoop.conf.Configuration;
public class ExitCommand extends BasicCommand {
public ReturnMsg execute(HClient client) {
public ReturnMsg execute(Configuration conf) {
System.exit(1);
return null;
}

View File

@ -38,7 +38,7 @@ import org.apache.hadoop.hbase.shell.*;
* Parsing command line.
*/
public class Parser {
private static String QueryString;
private String QueryString;
public Parser(String query) {
this((Reader)(new StringReader(query)));

View File

@ -19,12 +19,13 @@
*/
package org.apache.hadoop.hbase.shell;
import org.apache.hadoop.hbase.HClient;
import org.apache.hadoop.conf.Configuration;
public class HelpCommand extends BasicCommand {
String argument;
public ReturnMsg execute(HClient client) {
private String argument;
public ReturnMsg execute(Configuration conf) {
HelpManager.printHelp(this.argument);
return null;
}
@ -32,4 +33,5 @@ public class HelpCommand extends BasicCommand {
public void setArgument(String argument) {
this.argument = argument;
}
}

View File

@ -23,19 +23,18 @@ import java.io.IOException;
import java.util.List;
import java.util.Map;
import org.apache.hadoop.hbase.HClient;
import org.apache.hadoop.conf.Configuration;
import org.apache.hadoop.hbase.HTable;
import org.apache.hadoop.io.Text;
public class InsertCommand extends BasicCommand {
String table;
List<String> columnfamilies;
private Text table;
private List<String> columnfamilies;
private List<String> values;
private Map<String, List<String>> condition;
List<String> values;
Map<String, List<String>> condition;
public ReturnMsg execute(HClient client) {
public ReturnMsg execute(Configuration conf) {
if (this.table == null || this.values == null || this.condition == null)
return new ReturnMsg(0, "Syntax error : Please check 'Insert' syntax.");
@ -44,14 +43,14 @@ public class InsertCommand extends BasicCommand {
"Mismatch between values list and columnfamilies list");
try {
client.openTable(new Text(this.table));
long lockId = client.startUpdate(new Text(getRow()));
HTable table = new HTable(conf, this.table);
long lockId = table.startUpdate(getRow());
for (int i = 0; i < this.values.size(); i++) {
client.put(lockId, getColumn(i), getValue(i));
}
table.put(lockId, getColumn(i), getValue(i));
client.commit(lockId);
}
table.commit(lockId);
return new ReturnMsg(1, "1 row inserted successfully.");
} catch (IOException e) {
@ -61,7 +60,7 @@ public class InsertCommand extends BasicCommand {
}
public void setTable(String table) {
this.table = table;
this.table = new Text(table);
}
public void setColumnfamilies(List<String> columnfamilies) {

View File

@ -20,8 +20,8 @@
package org.apache.hadoop.hbase.shell;
public class ReturnMsg {
private String msg;
private String msg;
private int type;
public ReturnMsg(int i, String string) {
@ -41,4 +41,5 @@ public class ReturnMsg {
public int getType() {
return this.type;
}
}

View File

@ -24,47 +24,49 @@ import java.util.List;
import java.util.Map;
import java.util.TreeMap;
import org.apache.hadoop.hbase.HClient;
import org.apache.hadoop.conf.Configuration;
import org.apache.hadoop.hbase.HBaseAdmin;
import org.apache.hadoop.hbase.HConstants;
import org.apache.hadoop.hbase.HRegionInfo;
import org.apache.hadoop.hbase.HScannerInterface;
import org.apache.hadoop.hbase.HStoreKey;
import org.apache.hadoop.hbase.HTable;
import org.apache.hadoop.hbase.HTableDescriptor;
import org.apache.hadoop.io.DataInputBuffer;
import org.apache.hadoop.io.Text;
public class SelectCommand extends BasicCommand {
String table;
int limit;
private Text table;
private int limit;
private Map<String, List<String>> condition;
Map<String, List<String>> condition;
public ReturnMsg execute(HClient client) {
public ReturnMsg execute(Configuration conf) {
if (this.condition != null && this.condition.containsKey("error"))
return new ReturnMsg(0, "Syntax error : Please check 'Select' syntax.");
try {
client.openTable(new Text(this.table));
HTable table = new HTable(conf, this.table);
HBaseAdmin admin = new HBaseAdmin(conf);
switch (getCondition()) {
case 0:
HTableDescriptor[] tables = client.listTables();
HTableDescriptor[] tables = admin.listTables();
Text[] columns = null;
if (this.table.equals(HConstants.ROOT_TABLE_NAME.toString())
|| this.table.equals(HConstants.META_TABLE_NAME.toString())) {
if (this.table.equals(HConstants.ROOT_TABLE_NAME)
|| this.table.equals(HConstants.META_TABLE_NAME)) {
columns = HConstants.COLUMN_FAMILY_ARRAY;
} else {
for (int i = 0; i < tables.length; i++) {
if (tables[i].getName().toString().equals(this.table)) {
if (tables[i].getName().equals(this.table)) {
columns = tables[i].families().keySet().toArray(new Text[] {});
}
}
}
HScannerInterface scan = client.obtainScanner(columns, new Text(""));
HScannerInterface scan = table.obtainScanner(columns, new Text(""));
HStoreKey key = new HStoreKey();
TreeMap<Text, byte[]> results = new TreeMap<Text, byte[]>();
@ -100,7 +102,7 @@ public class SelectCommand extends BasicCommand {
count = 0;
ConsoleTable.selectHead();
for (Map.Entry<Text, byte[]> entry : client.getRow(new Text(getRow())).entrySet()) {
for (Map.Entry<Text, byte[]> entry : table.getRow(new Text(getRow())).entrySet()) {
byte[] value = entry.getValue();
String cellData = new String(value);
@ -125,7 +127,7 @@ public class SelectCommand extends BasicCommand {
Text[] column = new Text[] { new Text(getColumn()) };
HScannerInterface scanner = client.obtainScanner(column, new Text(""));
HScannerInterface scanner = table.obtainScanner(column, new Text(""));
HStoreKey k = new HStoreKey();
TreeMap<Text, byte[]> r = new TreeMap<Text, byte[]>();
@ -150,7 +152,7 @@ public class SelectCommand extends BasicCommand {
case 3:
byte[] rs1 = client.get(new Text(getRow()), new Text(getColumn()));
byte[] rs1 = table.get(new Text(getRow()), new Text(getColumn()));
ConsoleTable.selectHead();
ConsoleTable.printLine(0, getRow(), getColumn(),
@ -161,7 +163,7 @@ public class SelectCommand extends BasicCommand {
case 4:
byte[][] rs2 = client.get(new Text(getRow()), new Text(getColumn()), this.limit);
byte[][] rs2 = table.get(new Text(getRow()), new Text(getColumn()), this.limit);
ConsoleTable.selectHead();
for (int i = 0; i < rs2.length; i++) {
@ -174,7 +176,7 @@ public class SelectCommand extends BasicCommand {
case 5:
byte[][] rs3 = client.get(new Text(getRow()), new Text(getColumn()), getTime(), this.limit);
byte[][] rs3 = table.get(new Text(getRow()), new Text(getColumn()), getTime(), this.limit);
ConsoleTable.selectHead();
for (int i = 0; i < rs3.length; i++) {
@ -194,7 +196,7 @@ public class SelectCommand extends BasicCommand {
}
public void setTable(String table) {
this.table = table;
this.table = new Text(table);
}
public void setLimit(int limit) {

View File

@ -21,21 +21,24 @@ package org.apache.hadoop.hbase.shell;
import java.io.IOException;
import org.apache.hadoop.hbase.HClient;
import org.apache.hadoop.conf.Configuration;
import org.apache.hadoop.hbase.HBaseAdmin;
import org.apache.hadoop.hbase.HTableDescriptor;
public class ShowCommand extends BasicCommand {
String argument;
public ReturnMsg execute(HClient client) {
if (this.argument == null)
private String command;
public ReturnMsg execute(Configuration conf) {
if (this.command == null)
return new ReturnMsg(0, "Syntax error : Please check 'Show' syntax.");
try {
int tableLength = 0;
HBaseAdmin admin = new HBaseAdmin(conf);
if ("tables".equals(this.argument)) {
HTableDescriptor[] tables = client.listTables();
int tableLength = 0;
if ("tables".equals(this.command)) {
HTableDescriptor[] tables = admin.listTables();
tableLength = tables.length;
if (tableLength == 0) {
return new ReturnMsg(0, "Table not found.");
@ -57,6 +60,7 @@ public class ShowCommand extends BasicCommand {
}
public void setArgument(String argument) {
this.argument = argument;
this.command = argument;
}
}

View File

@ -33,7 +33,7 @@ import org.apache.hadoop.hbase.shell.*;
* Parsing command line.
*/
public class Parser implements ParserConstants {
private static String QueryString;
private String QueryString;
public Parser(String query) {
this((Reader)(new StringReader(query)));