You are viewing a plain text version of this content. The canonical link for it is here.
Posted to common-commits@hadoop.apache.org by st...@apache.org on 2007/10/06 05:09:52 UTC

svn commit: r582442 [2/2] - in /lucene/hadoop/trunk/src/contrib/hbase: ./ bin/ conf/ src/java/org/apache/hadoop/hbase/ src/java/org/apache/hadoop/hbase/shell/ src/java/org/apache/hadoop/hbase/shell/generated/ src/test/ src/test/org/apache/hadoop/hbase/...

Modified: lucene/hadoop/trunk/src/contrib/hbase/src/java/org/apache/hadoop/hbase/shell/TableFormatterFactory.java
URL: http://svn.apache.org/viewvc/lucene/hadoop/trunk/src/contrib/hbase/src/java/org/apache/hadoop/hbase/shell/TableFormatterFactory.java?rev=582442&r1=582441&r2=582442&view=diff
==============================================================================
--- lucene/hadoop/trunk/src/contrib/hbase/src/java/org/apache/hadoop/hbase/shell/TableFormatterFactory.java (original)
+++ lucene/hadoop/trunk/src/contrib/hbase/src/java/org/apache/hadoop/hbase/shell/TableFormatterFactory.java Fri Oct  5 20:09:50 2007
@@ -19,153 +19,65 @@
  */
 package org.apache.hadoop.hbase.shell;
 
-import java.io.PrintStream;
-import java.io.UnsupportedEncodingException;
+import java.io.Writer;
+import java.lang.reflect.Constructor;
+
+import org.apache.commons.logging.Log;
+import org.apache.commons.logging.LogFactory;
+import org.apache.hadoop.conf.Configuration;
+import org.apache.hadoop.hbase.shell.formatter.AsciiTableFormatter;
 
 /**
  * Table formatter.
- * TODO: Make a factory that chooses the formatter to use based off
- * configuration.  Read a property from hbase-site or from System properties.
- * For now, default is the internal AsciiTableFormatter.
- * TODO: Mysql has --skip-column-names and --silent which inserts a tab as
+ * Specify formatter by setting "hbaseshell.formatter" property in
+ * <code>hbase-site.xml</code> or by setting system property
+ * <code>hbaseshell.formatter</code>. System property setting prevails over all
+ * other configurations.  Outputs UTF-8 encoded Strings even if
+ * original data is binary.  On static initialization, changes System.out to be
+ * a UTF-8 output stream.
+ * .
+ * <p>TODO: Mysql has --skip-column-names and --silent which inserts a tab as
  * separator.  Also has --html and --xml.
+ * <p>To use the html formatter, currently set HBASE_OPTS as in:
+ * <code>$ HBASE_OPTS="-Dhbaseshell.formatter=org.apache.hadoop.hbase.shell.formatter.HtmlTableFormatter" ./bin/hbase shell</code>
+ * </p>
  */
 public class TableFormatterFactory {
-  private static final TableFormatterFactory factory =
-    new TableFormatterFactory();
+  private static final Log LOG =
+    LogFactory.getLog(TableFormatterFactory.class.getName());
+  private static final String FORMATTER_KEY = "hbaseshell.formatter";
   private final TableFormatter formatter;
   
-  private TableFormatterFactory() {
-    this.formatter = new AsciiTableFormatter();
-  }
-  
   /**
-   * @return Configured table formatter.
+   * Not instantiable
    */
-  public static TableFormatter get() {
-    return factory.formatter;
+  @SuppressWarnings({ "unchecked", "unused" })
+  private TableFormatterFactory() {
+    this(null, null);
   }
   
-  /*
-   * Formmatter that outputs data in UTF-8 inside an ASCII table on STDOUT.
-   * If only a single cell result, then no formatting is done.  Presumption is
-   * that client manages serial access outputting tables.
-   */
-  private class AsciiTableFormatter implements TableFormatter {
-    private PrintStream out;
-    private static final String COLUMN_DELIMITER = "| ";
-    private static final String COLUMN_CLOSER = "|";
-    private static final int DEFAULT_COLUMN_WIDTH = 26;
-    // Width is a line of content + delimiter
-    private int columnWidth = DEFAULT_COLUMN_WIDTH;
-    // Amount of width to use for a line of content.
-    private int columnContentWidth =
-      DEFAULT_COLUMN_WIDTH - COLUMN_DELIMITER.length();
-    // COLUMN_LINE is put at head and foot of a column and per column, is drawn
-    // as row delimiter
-    private String columnHorizLine;
-    private final String COLUMN_HORIZ_LINE_CLOSER = "+";
-    // Used padding content to fill column
-    private final String PADDING_CHAR = " ";
-    // True if we are to output no formatting.
-    private boolean noFormatting = false;
-    
-    /*
-     * Constructor. 
-     */
-    protected AsciiTableFormatter() {
-      try {
-        this.out = new PrintStream(System.out, true, "UTF-8");
-      } catch (UnsupportedEncodingException e) {
-        throw new RuntimeException("Failed setting output to UTF-8", e);
-      }
-    }
-    
-    /**
-     * @param titles List of titles.  Pass null if no formatting (i.e.
-     * no header, no footer, etc.
-     */
-    public void header(String[] titles) {
-      if (titles == null) {
-        // print nothing.
-        this.noFormatting = true;
-        return;
-      }
-      // Calculate width of columns.
-      this.columnWidth = titles.length == 1? 3 * DEFAULT_COLUMN_WIDTH:
-        titles.length == 2? 39: DEFAULT_COLUMN_WIDTH;
-      this.columnContentWidth = this.columnWidth - COLUMN_DELIMITER.length();
-      // Create the horizontal line to draw across the top of each column.
-      this.columnHorizLine = calculateColumnHorizLine(this.columnWidth);
-      // Print out a column topper per column.
-      printRowDelimiter(titles.length);
-      row(titles);
-    }
-
-    public void row(String [] cells) {
-      if (this.noFormatting) {
-        this.out.print(cells[0]);
-        return;
-      }
-      // Ok.  Output cells a line at a time w/ delimiters between cells.
-      int [] indexes = new int[cells.length];
-      for (int i = 0; i < indexes.length; i++) {
-        indexes[i] = 0;
-      }
-      int allFinished = 0;
-      while (allFinished < indexes.length) {
-        StringBuffer sb = new StringBuffer();
-        for (int i = 0; i < cells.length; i++) {
-          sb.append(COLUMN_DELIMITER);
-          int offset = indexes[i];
-          if (offset + this.columnContentWidth >= cells[i].length()) {
-            String substr = cells[i].substring(offset);
-            if (substr.length() > 0) {
-              // This column is finished
-              allFinished++;
-              sb.append(substr);
-            }
-            for (int j = 0; j < this.columnContentWidth - substr.length(); j++) {
-              sb.append(PADDING_CHAR);
-            }
-            indexes[i] = cells[i].length();
-          } else {
-            String substr = cells[i].substring(indexes[i],
-              indexes[i] + this.columnContentWidth);
-            indexes[i] += this.columnContentWidth;
-            sb.append(substr);
-          }
-        }
-        sb.append(COLUMN_CLOSER);
-        this.out.println(sb.toString());
-      }
-      printRowDelimiter(cells.length);
+  @SuppressWarnings("unchecked")
+  public TableFormatterFactory(final Writer out, final Configuration c) {
+    String className = System.getProperty(FORMATTER_KEY);
+    if (className == null) {
+      className = c.get(FORMATTER_KEY, AsciiTableFormatter.class.getName());
+    }
+    LOG.debug("Table formatter class: " + className);
+    try {
+      Class<TableFormatter> clazz =
+        (Class<TableFormatter>) Class.forName(className);
+      Constructor<?> constructor = clazz.getConstructor(Writer.class);
+      this.formatter =  (TableFormatter)constructor.newInstance(out);
+    } catch (Exception e) {
+      throw new RuntimeException("Failed instantiation of " + className, e);
     }
+  }
 
-    public void footer() {
-      if (this.noFormatting) {
-        // If no formatting, output a newline to delimit cell and the
-        // result summary output at end of every command.
-        this.out.println();
-      }
-      // We're done. Clear flag.
-      this.noFormatting = false;
-    }
-    
-    private void printRowDelimiter(final int columnCount) {
-      for (int i = 0; i < columnCount; i++) {
-        this.out.print(this.columnHorizLine);
-      }
-      this.out.println(COLUMN_HORIZ_LINE_CLOSER);
-    }
-    
-    private String calculateColumnHorizLine(final int width) {
-      StringBuffer sb = new StringBuffer();
-      sb.append("+");
-      for (int i = 1; i < width; i++) {
-        sb.append("-");
-      }
-      return sb.toString();
-    }
+  /**
+   * @return The table formatter instance
+   */
+  @SuppressWarnings("unchecked")
+  public TableFormatter get() {
+    return this.formatter;
   }
 }

Modified: lucene/hadoop/trunk/src/contrib/hbase/src/java/org/apache/hadoop/hbase/shell/generated/Parser.java
URL: http://svn.apache.org/viewvc/lucene/hadoop/trunk/src/contrib/hbase/src/java/org/apache/hadoop/hbase/shell/generated/Parser.java?rev=582442&r1=582441&r2=582442&view=diff
==============================================================================
--- lucene/hadoop/trunk/src/contrib/hbase/src/java/org/apache/hadoop/hbase/shell/generated/Parser.java (original)
+++ lucene/hadoop/trunk/src/contrib/hbase/src/java/org/apache/hadoop/hbase/shell/generated/Parser.java Fri Oct  5 20:09:50 2007
@@ -27,6 +27,7 @@
 import java.util.HashMap;
 import java.io.StringReader;
 import java.io.Reader;
+import java.io.Writer;
 
 import org.apache.hadoop.hbase.shell.*;
 
@@ -35,10 +36,14 @@
  */
 public class Parser implements ParserConstants {
   private String QueryString;
+  private TableFormatter formatter;
+  private Writer out;
 
-  public Parser(String query) {
+  public Parser(final String query, final Writer o, final TableFormatter f) {
     this((Reader)(new StringReader(query)));
     this.QueryString = query;
+    this.formatter = f;
+    this.out = o;
   }
 
   public String getQueryStr() {
@@ -164,7 +169,7 @@
   }
 
   final public ExitCommand exitCommand() throws ParseException {
-  ExitCommand exit = new ExitCommand();
+  ExitCommand exit = new ExitCommand(this.out);
     jj_consume_token(EXIT);
              {if (true) return exit;}
     throw new Error("Missing return statement in function");
@@ -172,7 +177,7 @@
 
   final public FsCommand fsCommand() throws ParseException {
   Token t = null;
-  FsCommand fs = new FsCommand();
+  FsCommand fs = new FsCommand(this.out);
   List<String> query = new ArrayList<String>();
     jj_consume_token(FS);
     label_1:
@@ -195,7 +200,7 @@
 
   final public JarCommand jarCommand() throws ParseException {
   Token t = null;
-  JarCommand jar = new JarCommand();
+  JarCommand jar = new JarCommand(this.out);
   List<String> query = new ArrayList<String>();
     jj_consume_token(JAR);
     label_2:
@@ -230,7 +235,7 @@
 
   final public HelpCommand helpCommand() throws ParseException {
   Token t = null;
-  HelpCommand help = new HelpCommand();
+  HelpCommand help = new HelpCommand(this.out, this.formatter);
   String argument = "";
     jj_consume_token(HELP);
     switch ((jj_ntk==-1)?jj_ntk():jj_ntk) {
@@ -304,7 +309,7 @@
   }
 
   final public ShowCommand showCommand() throws ParseException {
-  ShowCommand show = new ShowCommand();
+  ShowCommand show = new ShowCommand(this.out, this.formatter);
   String argument = null;
     jj_consume_token(SHOW);
     switch ((jj_ntk==-1)?jj_ntk():jj_ntk) {
@@ -323,7 +328,7 @@
   }
 
   final public DescCommand descCommand() throws ParseException {
-  DescCommand desc = new DescCommand();
+  DescCommand desc = new DescCommand(this.out, this.formatter);
   String argument = null;
     switch ((jj_ntk==-1)?jj_ntk():jj_ntk) {
     case DESCRIBE:
@@ -450,7 +455,7 @@
   }
 
   final public CreateCommand createCommand() throws ParseException {
-  CreateCommand createCommand = new CreateCommand();
+  CreateCommand createCommand = new CreateCommand(this.out);
   String table = null;
   Map<String, Object> columnSpec = null;
   String column = null;
@@ -483,7 +488,7 @@
   }
 
   final public AlterCommand alterCommand() throws ParseException {
-  AlterCommand alterCommand = new AlterCommand();
+  AlterCommand alterCommand = new AlterCommand(this.out);
   String table = null;
   String column = null;
   Map<String, Object> columnSpec = null;
@@ -547,7 +552,7 @@
   }
 
   final public DropCommand dropCommand() throws ParseException {
-  DropCommand drop = new DropCommand();
+  DropCommand drop = new DropCommand(this.out);
   List<String> tableList = null;
     jj_consume_token(DROP);
     jj_consume_token(TABLE);
@@ -558,7 +563,7 @@
   }
 
   final public InsertCommand insertCommand() throws ParseException {
-  InsertCommand in = new InsertCommand();
+  InsertCommand in = new InsertCommand(this.out);
   List<String> columnfamilies = null;
   List<String> values = null;
   String table = null;
@@ -593,7 +598,7 @@
   }
 
   final public DeleteCommand deleteCommand() throws ParseException {
-  DeleteCommand deleteCommand = new DeleteCommand();
+  DeleteCommand deleteCommand = new DeleteCommand(this.out);
   List<String> columnList = null;
   Token t = null;
   String table = null;
@@ -624,7 +629,7 @@
   }
 
   final public SelectCommand selectCommand() throws ParseException {
-  SelectCommand select = new SelectCommand();
+  SelectCommand select = new SelectCommand(this.out, this.formatter);
   List<String> columns = null;
   String rowKey = "";
   String timestamp = null;
@@ -704,7 +709,7 @@
   }
 
   final public EnableCommand enableCommand() throws ParseException {
-  EnableCommand enableCommand = new EnableCommand();
+  EnableCommand enableCommand = new EnableCommand(this.out);
   String table = null;
     jj_consume_token(ENABLE);
     table = Identifier();
@@ -714,7 +719,7 @@
   }
 
   final public DisableCommand disableCommand() throws ParseException {
-  DisableCommand disableCommand = new DisableCommand();
+  DisableCommand disableCommand = new DisableCommand(this.out);
   String table = null;
     jj_consume_token(DISABLE);
     table = Identifier();
@@ -724,7 +729,7 @@
   }
 
   final public ClearCommand clearCommand() throws ParseException {
-  ClearCommand clear = new ClearCommand();
+  ClearCommand clear = new ClearCommand(this.out);
     jj_consume_token(CLEAR);
      {if (true) return clear;}
     throw new Error("Missing return statement in function");
@@ -970,6 +975,16 @@
     finally { jj_save(0, xla); }
   }
 
+  final private boolean jj_3R_12() {
+    Token xsp;
+    xsp = jj_scanpos;
+    if (jj_scan_token(60)) {
+    jj_scanpos = xsp;
+    if (jj_scan_token(61)) return true;
+    }
+    return false;
+  }
+
   final private boolean jj_3R_11() {
     if (jj_scan_token(ID)) return true;
     return false;
@@ -988,16 +1003,6 @@
   final private boolean jj_3_1() {
     if (jj_scan_token(ADD)) return true;
     if (jj_3R_10()) return true;
-    return false;
-  }
-
-  final private boolean jj_3R_12() {
-    Token xsp;
-    xsp = jj_scanpos;
-    if (jj_scan_token(60)) {
-    jj_scanpos = xsp;
-    if (jj_scan_token(61)) return true;
-    }
     return false;
   }
 

Modified: lucene/hadoop/trunk/src/contrib/hbase/src/java/org/apache/hadoop/hbase/shell/generated/ParserTokenManager.java
URL: http://svn.apache.org/viewvc/lucene/hadoop/trunk/src/contrib/hbase/src/java/org/apache/hadoop/hbase/shell/generated/ParserTokenManager.java?rev=582442&r1=582441&r2=582442&view=diff
==============================================================================
--- lucene/hadoop/trunk/src/contrib/hbase/src/java/org/apache/hadoop/hbase/shell/generated/ParserTokenManager.java (original)
+++ lucene/hadoop/trunk/src/contrib/hbase/src/java/org/apache/hadoop/hbase/shell/generated/ParserTokenManager.java Fri Oct  5 20:09:50 2007
@@ -26,6 +26,7 @@
 import java.util.HashMap;
 import java.io.StringReader;
 import java.io.Reader;
+import java.io.Writer;
 import org.apache.hadoop.hbase.shell.*;
 
 public class ParserTokenManager implements ParserConstants

Modified: lucene/hadoop/trunk/src/contrib/hbase/src/test/hbase-site.xml
URL: http://svn.apache.org/viewvc/lucene/hadoop/trunk/src/contrib/hbase/src/test/hbase-site.xml?rev=582442&r1=582441&r2=582442&view=diff
==============================================================================
--- lucene/hadoop/trunk/src/contrib/hbase/src/test/hbase-site.xml (original)
+++ lucene/hadoop/trunk/src/contrib/hbase/src/test/hbase-site.xml Fri Oct  5 20:09:50 2007
@@ -68,6 +68,20 @@
     </description>
   </property>
   <property>
+    <name>hbase.master.info.port</name>
+    <value>-1</value>
+    <description>The port for the hbase master web UI
+    Set to -1 if you do not want the info server to run.
+    </description>
+  </property>
+  <property>
+    <name>hbase.regionserver.info.port</name>
+    <value>-1</value>
+    <description>The port for the hbase regionserver web UI
+    Set to -1 if you do not want the info server to run.
+    </description>
+  </property>
+  <property>
     <name>hbase.master.lease.thread.wakefrequency</name>
     <value>3000</value>
     <description>The interval between checks for expired region server leases.

Modified: lucene/hadoop/trunk/src/contrib/hbase/src/test/org/apache/hadoop/hbase/HBaseClusterTestCase.java
URL: http://svn.apache.org/viewvc/lucene/hadoop/trunk/src/contrib/hbase/src/test/org/apache/hadoop/hbase/HBaseClusterTestCase.java?rev=582442&r1=582441&r2=582442&view=diff
==============================================================================
--- lucene/hadoop/trunk/src/contrib/hbase/src/test/org/apache/hadoop/hbase/HBaseClusterTestCase.java (original)
+++ lucene/hadoop/trunk/src/contrib/hbase/src/test/org/apache/hadoop/hbase/HBaseClusterTestCase.java Fri Oct  5 20:09:50 2007
@@ -71,7 +71,7 @@
 
   /** {@inheritDoc} */
   @Override
-  public void setUp() throws Exception {
+  protected void setUp() throws Exception {
     super.setUp();
     this.cluster =
       new MiniHBaseCluster(this.conf, this.regionServers, this.miniHdfs);
@@ -79,7 +79,7 @@
 
   /** {@inheritDoc} */
   @Override
-  public void tearDown() throws Exception {
+  protected void tearDown() throws Exception {
     super.tearDown();
     if (this.cluster != null) {
       this.cluster.shutdown();

Modified: lucene/hadoop/trunk/src/contrib/hbase/src/test/org/apache/hadoop/hbase/HBaseTestCase.java
URL: http://svn.apache.org/viewvc/lucene/hadoop/trunk/src/contrib/hbase/src/test/org/apache/hadoop/hbase/HBaseTestCase.java?rev=582442&r1=582441&r2=582442&view=diff
==============================================================================
--- lucene/hadoop/trunk/src/contrib/hbase/src/test/org/apache/hadoop/hbase/HBaseTestCase.java (original)
+++ lucene/hadoop/trunk/src/contrib/hbase/src/test/org/apache/hadoop/hbase/HBaseTestCase.java Fri Oct  5 20:09:50 2007
@@ -66,7 +66,7 @@
   
   /** {@inheritDoc} */
   @Override
-  public void setUp() throws Exception {
+  protected void setUp() throws Exception {
     super.setUp();
     this.testDir = getUnitTestdir(getName());
     this.localFs = FileSystem.getLocal(this.conf);
@@ -77,7 +77,7 @@
   
   /** {@inheritDoc} */
   @Override
-  public void tearDown() throws Exception {
+  protected void tearDown() throws Exception {
     if (this.localFs != null && this.testDir != null &&
         this.localFs.exists(testDir)) {
       this.localFs.delete(testDir);

Modified: lucene/hadoop/trunk/src/contrib/hbase/src/test/org/apache/hadoop/hbase/MiniHBaseCluster.java
URL: http://svn.apache.org/viewvc/lucene/hadoop/trunk/src/contrib/hbase/src/test/org/apache/hadoop/hbase/MiniHBaseCluster.java?rev=582442&r1=582441&r2=582442&view=diff
==============================================================================
--- lucene/hadoop/trunk/src/contrib/hbase/src/test/org/apache/hadoop/hbase/MiniHBaseCluster.java (original)
+++ lucene/hadoop/trunk/src/contrib/hbase/src/test/org/apache/hadoop/hbase/MiniHBaseCluster.java Fri Oct  5 20:09:50 2007
@@ -84,7 +84,6 @@
 
   /**
    * Starts a MiniHBaseCluster on top of an existing HDFSCluster
-   *
    *<pre>
    ****************************************************************************
    *            *  *  *  *  *  N O T E  *  *  *  *  *
@@ -181,7 +180,6 @@
       this.regionServer = r;
     }
 
-    /** {@inheritDoc} */
     @Override
     public void run() {
       LOG.info("Starting " + getName());
@@ -461,4 +459,8 @@
       r.flushcache(false);
     }
   }
-}
\ No newline at end of file
+
+  public ArrayList<RegionServerThread> getRegionThreads() {
+    return this.regionThreads;
+  }
+}

Modified: lucene/hadoop/trunk/src/contrib/hbase/src/test/org/apache/hadoop/hbase/TestToString.java
URL: http://svn.apache.org/viewvc/lucene/hadoop/trunk/src/contrib/hbase/src/test/org/apache/hadoop/hbase/TestToString.java?rev=582442&r1=582441&r2=582442&view=diff
==============================================================================
--- lucene/hadoop/trunk/src/contrib/hbase/src/test/org/apache/hadoop/hbase/TestToString.java (original)
+++ lucene/hadoop/trunk/src/contrib/hbase/src/test/org/apache/hadoop/hbase/TestToString.java Fri Oct  5 20:09:50 2007
@@ -34,7 +34,7 @@
     final String hostport = "127.0.0.1:9999";
     HServerAddress address = new HServerAddress(hostport);
     assertEquals("HServerAddress toString", address.toString(), hostport);
-    HServerInfo info = new HServerInfo(address, -1);
+    HServerInfo info = new HServerInfo(address, -1, 60030);
     assertEquals("HServerInfo", "address: " + hostport + ", startcode: -1" +
         ", load: (requests: 0 regions: 0)", info.toString());
   }

Modified: lucene/hadoop/trunk/src/contrib/hbase/src/test/org/apache/hadoop/hbase/shell/TestHBaseShell.java
URL: http://svn.apache.org/viewvc/lucene/hadoop/trunk/src/contrib/hbase/src/test/org/apache/hadoop/hbase/shell/TestHBaseShell.java?rev=582442&r1=582441&r2=582442&view=diff
==============================================================================
--- lucene/hadoop/trunk/src/contrib/hbase/src/test/org/apache/hadoop/hbase/shell/TestHBaseShell.java (original)
+++ lucene/hadoop/trunk/src/contrib/hbase/src/test/org/apache/hadoop/hbase/shell/TestHBaseShell.java Fri Oct  5 20:09:50 2007
@@ -21,8 +21,10 @@
 
 import java.io.ByteArrayOutputStream;
 import java.io.IOException;
+import java.io.OutputStreamWriter;
 import java.io.PrintStream;
 import java.io.UnsupportedEncodingException;
+import java.io.Writer;
 
 import org.apache.commons.logging.Log;
 import org.apache.commons.logging.LogFactory;
@@ -157,7 +159,9 @@
   private ReturnMsg runCommand(final String cmdStr)
   throws ParseException, UnsupportedEncodingException {
     LOG.info("Running command: " + cmdStr);
-    Parser parser = new Parser(cmdStr);
+    Writer out = new OutputStreamWriter(System.out, "UTF-8");
+    TableFormatterFactory tff = new TableFormatterFactory(out, this.conf);
+    Parser parser = new Parser(cmdStr, out, tff.get());
     Command cmd = parser.terminatedCommand();
     ReturnMsg rm = cmd.execute(this.conf);
     dumpStdout();