You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@hive.apache.org by cw...@apache.org on 2011/08/30 23:43:41 UTC

svn commit: r1163413 - in /hive/trunk: cli/src/java/org/apache/hadoop/hive/cli/CliDriver.java cli/src/java/org/apache/hadoop/hive/cli/CliSessionState.java common/src/java/org/apache/hadoop/hive/conf/HiveConf.java conf/hive-default.xml

Author: cws
Date: Tue Aug 30 21:43:41 2011
New Revision: 1163413

URL: http://svn.apache.org/viewvc?rev=1163413&view=rev
Log:
HIVE-2233. Show current database in hive prompt (Jakob Homan via cws)

Modified:
    hive/trunk/cli/src/java/org/apache/hadoop/hive/cli/CliDriver.java
    hive/trunk/cli/src/java/org/apache/hadoop/hive/cli/CliSessionState.java
    hive/trunk/common/src/java/org/apache/hadoop/hive/conf/HiveConf.java
    hive/trunk/conf/hive-default.xml

Modified: hive/trunk/cli/src/java/org/apache/hadoop/hive/cli/CliDriver.java
URL: http://svn.apache.org/viewvc/hive/trunk/cli/src/java/org/apache/hadoop/hive/cli/CliDriver.java?rev=1163413&r1=1163412&r2=1163413&view=diff
==============================================================================
--- hive/trunk/cli/src/java/org/apache/hadoop/hive/cli/CliDriver.java (original)
+++ hive/trunk/cli/src/java/org/apache/hadoop/hive/cli/CliDriver.java Tue Aug 30 21:43:41 2011
@@ -650,7 +650,10 @@ public class CliDriver {
     int ret = 0;
 
     String prefix = "";
-    String curPrompt = prompt;
+    String curDB = getFormattedDb(conf, ss);
+    String curPrompt = prompt + curDB;
+    String dbSpaces = spacesForString(curDB);
+
     while ((line = reader.readLine(curPrompt + "> ")) != null) {
       if (!prefix.equals("")) {
         prefix += '\n';
@@ -659,10 +662,12 @@ public class CliDriver {
         line = prefix + line;
         ret = cli.processLine(line, true);
         prefix = "";
-        curPrompt = prompt;
+        curDB = getFormattedDb(conf, ss);
+        curPrompt = prompt + curDB;
+        dbSpaces = dbSpaces.length() == curDB.length() ? dbSpaces : spacesForString(curDB);
       } else {
         prefix = prefix + line;
-        curPrompt = prompt2;
+        curPrompt = prompt2 + dbSpaces;
         continue;
       }
     }
@@ -672,4 +677,37 @@ public class CliDriver {
     System.exit(ret);
   }
 
+  /**
+   * Retrieve the current database name string to display, based on the
+   * configuration value.
+   * @param conf storing whether or not to show current db
+   * @param ss CliSessionState to query for db name
+   * @return String to show user for current db value
+   */
+  private static String getFormattedDb(HiveConf conf, CliSessionState ss) {
+    if (!HiveConf.getBoolVar(conf, HiveConf.ConfVars.CLIPRINTCURRENTDB)) {
+      return "";
+    }
+    String currDb = ss.getCurrentDbName();
+
+    if (currDb == null) {
+      return "";
+    }
+
+    return " (" + currDb + ")";
+  }
+
+  /**
+   * Generate a string of whitespace the same length as the parameter
+   *
+   * @param s String for which to generate equivalent whitespace
+   * @return  Whitespace
+   */
+  private static String spacesForString(String s) {
+    if (s == null || s.length() == 0) {
+      return "";
+    }
+    return String.format("%1$-" + s.length() +"s", "");
+  }
+
 }

Modified: hive/trunk/cli/src/java/org/apache/hadoop/hive/cli/CliSessionState.java
URL: http://svn.apache.org/viewvc/hive/trunk/cli/src/java/org/apache/hadoop/hive/cli/CliSessionState.java?rev=1163413&r1=1163412&r2=1163413&view=diff
==============================================================================
--- hive/trunk/cli/src/java/org/apache/hadoop/hive/cli/CliSessionState.java (original)
+++ hive/trunk/cli/src/java/org/apache/hadoop/hive/cli/CliSessionState.java Tue Aug 30 21:43:41 2011
@@ -23,6 +23,8 @@ import java.util.List;
 import java.util.Properties;
 
 import org.apache.hadoop.hive.conf.HiveConf;
+import org.apache.hadoop.hive.ql.metadata.Hive;
+import org.apache.hadoop.hive.ql.metadata.HiveException;
 import org.apache.hadoop.hive.ql.session.SessionState;
 import org.apache.hadoop.hive.service.HiveClient;
 import org.apache.thrift.TException;
@@ -68,6 +70,8 @@ public class CliSessionState extends Ses
   private TTransport transport;
   private HiveClient client;
 
+  private Hive hive; // currently only used (and init'ed) in getCurrentDbName
+
   public CliSessionState() {
     super();
     remoteMode = false;
@@ -119,4 +123,19 @@ public class CliSessionState extends Ses
   public HiveClient getClient() {
     return client;
   }
+
+  /**
+   * Return the name of the current database
+   * @return the name of the current database or, if an error, null
+   */
+  public String getCurrentDbName() {
+    if (hive == null) {
+      try {
+        hive = Hive.get(conf);
+      } catch (HiveException e) {
+        return null;
+      }
+    }
+    return hive.getCurrentDatabase();
+  }
 }

Modified: hive/trunk/common/src/java/org/apache/hadoop/hive/conf/HiveConf.java
URL: http://svn.apache.org/viewvc/hive/trunk/common/src/java/org/apache/hadoop/hive/conf/HiveConf.java?rev=1163413&r1=1163412&r2=1163413&view=diff
==============================================================================
--- hive/trunk/common/src/java/org/apache/hadoop/hive/conf/HiveConf.java (original)
+++ hive/trunk/common/src/java/org/apache/hadoop/hive/conf/HiveConf.java Tue Aug 30 21:43:41 2011
@@ -228,6 +228,7 @@ public class HiveConf extends Configurat
 
     // CLI
     CLIIGNOREERRORS("hive.cli.errors.ignore", false),
+    CLIPRINTCURRENTDB("hive.cli.print.current.db", false),
 
     HIVE_METASTORE_FS_HANDLER_CLS("hive.metastore.fs.handler.class", "org.apache.hadoop.hive.metastore.HiveMetaStoreFsImpl"),
 

Modified: hive/trunk/conf/hive-default.xml
URL: http://svn.apache.org/viewvc/hive/trunk/conf/hive-default.xml?rev=1163413&r1=1163412&r2=1163413&view=diff
==============================================================================
--- hive/trunk/conf/hive-default.xml (original)
+++ hive/trunk/conf/hive-default.xml Tue Aug 30 21:43:41 2011
@@ -58,6 +58,12 @@
 </property>
 
 <property>
+  <name>hive.cli.print.current.db</name>
+  <value>false</value>
+  <description>Whether to include the current database in the hive prompt.</description>
+</property>
+
+<property>
   <name>hive.exec.scratchdir</name>
   <value>/tmp/hive-${user.name}</value>
   <description>Scratch space for Hive jobs</description>