From 91de46cac15c5784aac2aac51bc6b6f54b125e04 Mon Sep 17 00:00:00 2001 From: Michael Stack Date: Thu, 13 Dec 2007 15:43:04 +0000 Subject: [PATCH] HADOOP-2351 If select command returns no result, it doesn't need to show the header information git-svn-id: https://svn.apache.org/repos/asf/lucene/hadoop/trunk/src/contrib/hbase@603940 13f79535-47bb-0310-9956-ffa450edef68 --- CHANGES.txt | 4 ++++ src/java/org/apache/hadoop/hbase/Shell.java | 12 ++++++++-- .../hadoop/hbase/shell/SelectCommand.java | 22 ++++++++++++++++--- .../hbase/shell/ShellSecurityManager.java | 13 +++++++++-- 4 files changed, 44 insertions(+), 7 deletions(-) diff --git a/CHANGES.txt b/CHANGES.txt index 33301bbe9fb..b8a9eb6903f 100644 --- a/CHANGES.txt +++ b/CHANGES.txt @@ -117,6 +117,10 @@ Trunk (unreleased changes) (Edward Yoon via Stack) HADOOP-2407 Keeping MapFile.Reader open is expensive: Part 2 HADOOP-2047 Add an '--master=X' and '--html' command-line parameters to shell + (Edward Yoon via Stack) + HADOOP-2351 If select command returns no result, it doesn't need to show the + header information (Edward Yoon via Stack) + Release 0.15.1 Branch 0.15 diff --git a/src/java/org/apache/hadoop/hbase/Shell.java b/src/java/org/apache/hadoop/hbase/Shell.java index 547a988cc8b..3a265c7d5d1 100644 --- a/src/java/org/apache/hadoop/hbase/Shell.java +++ b/src/java/org/apache/hadoop/hbase/Shell.java @@ -47,6 +47,7 @@ public class Shell { public static final boolean DEFAULT_BELL_ENABLED = true; public static String MASTER_ADDRESS = null; public static String HTML_OPTION = null; + public static int RELAUNCH_FLAG = 7; /** Return the boolean value indicating whether end of command or not */ static boolean isEndOfCommand(String line) { @@ -78,6 +79,14 @@ public class Shell { */ public static void main(String args[]) throws IOException { argumentParsing(args); + if (args.length != 0) { + if (args[0].equals("--help") || args[0].equals("-h")) { + System.out + .println("Usage: ./bin/hbase shell [--master:master_address:port] [--html]\n"); + System.exit(1); + } + } + HBaseConfiguration conf = new HBaseConfiguration(); ConsoleReader reader = new ConsoleReader(); System.setSecurityManager(new ShellSecurityManager()); @@ -90,11 +99,10 @@ public class Shell { } if (HTML_OPTION != null) { tableFormater = new HtmlTableFormatter(out); - System.out.println("--html"); } HelpCommand help = new HelpCommand(out, tableFormater); - if (args.length == 0 || !args[0].equals("7")) { + if (args.length == 0 || !args[0].equals(String.valueOf(Shell.RELAUNCH_FLAG))) { help.printVersion(); } StringBuilder queryStr = new StringBuilder(); diff --git a/src/java/org/apache/hadoop/hbase/shell/SelectCommand.java b/src/java/org/apache/hadoop/hbase/shell/SelectCommand.java index bdd21ac87cf..41ab58331c3 100644 --- a/src/java/org/apache/hadoop/hbase/shell/SelectCommand.java +++ b/src/java/org/apache/hadoop/hbase/shell/SelectCommand.java @@ -37,6 +37,7 @@ 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.hbase.Shell; import org.apache.hadoop.hbase.shell.generated.Parser; import org.apache.hadoop.hbase.util.Writables; import org.apache.hadoop.io.Text; @@ -113,8 +114,10 @@ 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()) { + if(count == 0) { + formatter.header(multiple ? HEADER_COLUMN_CELL : null); + } if (this.timestamp != 0) { result = table.get(this.rowKey, column, this.timestamp, this.version); } else { @@ -131,8 +134,10 @@ public class SelectCommand extends BasicCommand { } } } else { - formatter.header(isMultiple() ? HEADER_COLUMN_CELL : null); for (Map.Entry e : table.getRow(this.rowKey).entrySet()) { + if(count == 0) { + formatter.header(isMultiple() ? HEADER_COLUMN_CELL : null); + } Text key = e.getKey(); String keyStr = key.toString(); if (!this.columns.contains(STAR) && !this.columns.contains(keyStr)) { @@ -147,6 +152,10 @@ public class SelectCommand extends BasicCommand { count++; } } + + if(count == 0 && Shell.HTML_OPTION != null) { + formatter.header(isMultiple() ? HEADER_COLUMN_CELL : null); + } formatter.footer(); } catch (IOException e) { e.printStackTrace(); @@ -209,8 +218,10 @@ public class SelectCommand extends BasicCommand { HStoreKey key = new HStoreKey(); TreeMap results = new TreeMap(); // If only one column in query, then don't print out the column. - formatter.header((parsedColumns.isMultiple()) ? HEADER : HEADER_ROW_CELL); while (scan.next(key, results) && checkLimit(count)) { + if(count == 0) { + formatter.header((parsedColumns.isMultiple()) ? HEADER : HEADER_ROW_CELL); + } Text r = key.getRow(); for (Text columnKey : results.keySet()) { String cellData = toString(columnKey, results.get(columnKey)); @@ -229,6 +240,11 @@ public class SelectCommand extends BasicCommand { // Clear results else subsequent results polluted w/ previous finds. results.clear(); } + + if(count == 0 && Shell.HTML_OPTION != null) { + formatter.header((parsedColumns.isMultiple()) ? HEADER : HEADER_ROW_CELL); + } + formatter.footer(); scan.close(); } catch (IOException e) { diff --git a/src/java/org/apache/hadoop/hbase/shell/ShellSecurityManager.java b/src/java/org/apache/hadoop/hbase/shell/ShellSecurityManager.java index ab5428abe9e..c2b56f15548 100644 --- a/src/java/org/apache/hadoop/hbase/shell/ShellSecurityManager.java +++ b/src/java/org/apache/hadoop/hbase/shell/ShellSecurityManager.java @@ -21,6 +21,8 @@ package org.apache.hadoop.hbase.shell; import java.io.IOException; import java.security.Permission; +import java.util.ArrayList; +import java.util.List; import org.apache.hadoop.hbase.Shell; @@ -47,9 +49,16 @@ public class ShellSecurityManager extends SecurityManager { // I didn't figure out How can catch the ExitException in shell main. // So, I just Re-launching the shell. Shell shell = new Shell(); - String[] args = new String[] { String.valueOf(7) }; + + List argList = new ArrayList(); + argList.add(String.valueOf(Shell.RELAUNCH_FLAG)); + if(Shell.HTML_OPTION != null) + argList.add(Shell.HTML_OPTION); + if(Shell.MASTER_ADDRESS != null) + argList.add(Shell.MASTER_ADDRESS); + try { - shell.main(args); + shell.main(argList.toArray(new String[] {})); } catch (IOException e) { e.printStackTrace(); }