hadoop-hive-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From na...@apache.org
Subject svn commit: r957745 - in /hadoop/hive/trunk: ./ cli/src/java/org/apache/hadoop/hive/cli/ data/scripts/ ql/src/test/org/apache/hadoop/hive/ql/ ql/src/test/queries/clientpositive/ ql/src/test/results/clientpositive/
Date Thu, 24 Jun 2010 22:50:52 GMT
Author: namit
Date: Thu Jun 24 22:50:51 2010
New Revision: 957745

URL: http://svn.apache.org/viewvc?rev=957745&view=rev
Log:
HIVE-1405. Add ability to run an initialization script by 'hive -i <fileName>' (John
Sichi via namit)


Added:
    hadoop/hive/trunk/data/scripts/test_init_file.sql
    hadoop/hive/trunk/ql/src/test/queries/clientpositive/init_file.q
    hadoop/hive/trunk/ql/src/test/results/clientpositive/init_file.q.out
Modified:
    hadoop/hive/trunk/CHANGES.txt
    hadoop/hive/trunk/cli/src/java/org/apache/hadoop/hive/cli/CliDriver.java
    hadoop/hive/trunk/cli/src/java/org/apache/hadoop/hive/cli/CliSessionState.java
    hadoop/hive/trunk/cli/src/java/org/apache/hadoop/hive/cli/OptionsProcessor.java
    hadoop/hive/trunk/ql/src/test/org/apache/hadoop/hive/ql/QTestUtil.java

Modified: hadoop/hive/trunk/CHANGES.txt
URL: http://svn.apache.org/viewvc/hadoop/hive/trunk/CHANGES.txt?rev=957745&r1=957744&r2=957745&view=diff
==============================================================================
--- hadoop/hive/trunk/CHANGES.txt (original)
+++ hadoop/hive/trunk/CHANGES.txt Thu Jun 24 22:50:51 2010
@@ -8,6 +8,8 @@ Trunk -  Unreleased
 
     HIVE-1304. Add function row_sequence in contrib (John Sichi via namit)
 
+    HIVE-1405. Add ability to run an initialization script by 'hive -i <fileName>'
(John Sichi via namit)
+
   IMPROVEMENTS
 
     HIVE-1394. Do not update transient_lastDdlTime if the partition is modified by a housekeeping

Modified: hadoop/hive/trunk/cli/src/java/org/apache/hadoop/hive/cli/CliDriver.java
URL: http://svn.apache.org/viewvc/hadoop/hive/trunk/cli/src/java/org/apache/hadoop/hive/cli/CliDriver.java?rev=957745&r1=957744&r2=957745&view=diff
==============================================================================
--- hadoop/hive/trunk/cli/src/java/org/apache/hadoop/hive/cli/CliDriver.java (original)
+++ hadoop/hive/trunk/cli/src/java/org/apache/hadoop/hive/cli/CliDriver.java Thu Jun 24 22:50:51
2010
@@ -216,6 +216,30 @@ public class CliDriver {
     return (processLine(qsb.toString()));
   }
 
+  public int processFile(String fileName) throws IOException {
+    FileReader fileReader = null;
+    try {
+      fileReader = new FileReader(fileName);
+      return processReader(new BufferedReader(fileReader));
+    } finally {
+      if (fileReader != null) {
+        fileReader.close();
+      }
+    }
+  }
+
+  public void processInitFiles(CliSessionState ss) throws IOException {
+    boolean saveSilent = ss.getIsSilent();
+    ss.setIsSilent(true);
+    for (String initFile : ss.initFiles) {
+      int rc = processFile(initFile);
+      if (rc != 0) {
+        System.exit(rc);
+      }
+    }
+    ss.setIsSilent(saveSilent);
+  }
+  
   public static void main(String[] args) throws Exception {
 
     OptionsProcessor oproc = new OptionsProcessor();
@@ -263,13 +287,16 @@ public class CliDriver {
 
     CliDriver cli = new CliDriver();
 
+    // Execute -i init files (always in silent mode)
+    cli.processInitFiles(ss);
+    
     if (ss.execString != null) {
       System.exit(cli.processLine(ss.execString));
     }
 
     try {
       if (ss.fileName != null) {
-        System.exit(cli.processReader(new BufferedReader(new FileReader(ss.fileName))));
+        System.exit(cli.processFile(ss.fileName));
       }
     } catch (FileNotFoundException e) {
       System.err.println("Could not open input file for reading. (" + e.getMessage() + ")");

Modified: hadoop/hive/trunk/cli/src/java/org/apache/hadoop/hive/cli/CliSessionState.java
URL: http://svn.apache.org/viewvc/hadoop/hive/trunk/cli/src/java/org/apache/hadoop/hive/cli/CliSessionState.java?rev=957745&r1=957744&r2=957745&view=diff
==============================================================================
--- hadoop/hive/trunk/cli/src/java/org/apache/hadoop/hive/cli/CliSessionState.java (original)
+++ hadoop/hive/trunk/cli/src/java/org/apache/hadoop/hive/cli/CliSessionState.java Thu Jun
24 22:50:51 2010
@@ -18,6 +18,8 @@
 
 package org.apache.hadoop.hive.cli;
 
+import java.util.ArrayList;
+import java.util.List;
 import java.util.Properties;
 
 import org.apache.hadoop.hive.conf.HiveConf;
@@ -43,6 +45,11 @@ public class CliSessionState extends Ses
    */
   public Properties cmdProperties = new Properties();
 
+  /**
+   * -i option if any that the session has been invoked with.
+   */
+  public List<String> initFiles = new ArrayList<String>();
+
   public CliSessionState() {
     super();
   }

Modified: hadoop/hive/trunk/cli/src/java/org/apache/hadoop/hive/cli/OptionsProcessor.java
URL: http://svn.apache.org/viewvc/hadoop/hive/trunk/cli/src/java/org/apache/hadoop/hive/cli/OptionsProcessor.java?rev=957745&r1=957744&r2=957745&view=diff
==============================================================================
--- hadoop/hive/trunk/cli/src/java/org/apache/hadoop/hive/cli/OptionsProcessor.java (original)
+++ hadoop/hive/trunk/cli/src/java/org/apache/hadoop/hive/cli/OptionsProcessor.java Thu Jun
24 22:50:51 2010
@@ -46,21 +46,24 @@ public class OptionsProcessor {
   protected static final Log l4j = LogFactory.getLog(OptionsProcessor.class.getName());
 
   private final Parser parser = new Parser();
-  private final Option confOptions, isSilentOption, execOption, fileOption, isHelpOption;
+  private final Option confOptions, initFilesOption, isSilentOption,
+    execOption, fileOption, isHelpOption;
 
   /**
    * Shamelessly cloned from Hadoop streaming take in multiple -hiveconf x=y parameters.
    */
   class MultiPropertyOption extends PropertyOption {
     private String optionString;
+    private boolean keyValue;
 
     MultiPropertyOption() {
       super();
     }
 
-    MultiPropertyOption(final String optionString, final String description, final int id)
{
+    MultiPropertyOption(final String optionString, final String description, final int id,
boolean keyValue) {
       super(optionString, description, id);
       this.optionString = optionString;
+      this.keyValue = keyValue;
     }
 
     @Override
@@ -85,7 +88,7 @@ public class OptionsProcessor {
         next = (String) arguments.next();
         if (!next.startsWith("-")) {
 
-          if (next.indexOf("=") == -1) {
+          if (keyValue && (next.indexOf("=") == -1)) {
             throw new OptionException(this, ResourceConstants.UNEXPECTED_TOKEN, "argument:
'"
                 + next + "' is not of the form x=y");
           }
@@ -152,11 +155,16 @@ public class OptionsProcessor {
 
     // -hiveconf var=val
     confOptions = new MultiPropertyOption("-hiveconf",
-        "(n=v) Optional. Add or override Hive/Hadoop properties.", 'D');
+      "(n=v) Optional. Add or override Hive/Hadoop properties.", 'D', true);
+
+    initFilesOption = new MultiPropertyOption(
+      "-i", "File to run before other commands", 'I', false);
 
     new PropertyOption();
-    Group allOptions = new GroupBuilder().withOption(confOptions).withOption(isSilentOption)
-        .withOption(isHelpOption).withOption(execOption).withOption(fileOption).create();
+    Group allOptions =
+      new GroupBuilder().withOption(confOptions).withOption(initFilesOption)
+      .withOption(isSilentOption).withOption(isHelpOption)
+      .withOption(execOption).withOption(fileOption).create();
     parser.setGroup(allOptions);
   }
 
@@ -188,6 +196,11 @@ public class OptionsProcessor {
     ss.execString = (String) cmdLine.getValue(execOption);
     // -f
     ss.fileName = (String) cmdLine.getValue(fileOption);
+    // -i
+    List<String> initFiles = (List<String>) cmdLine.getValue(initFilesOption);
+    if (null != initFiles) {
+      ss.initFiles = initFiles;
+    }
     // -h
     if (cmdLine.hasOption(isHelpOption)) {
       printUsage(null);
@@ -214,15 +227,17 @@ public class OptionsProcessor {
       System.err.println("Invalid arguments: " + error);
     }
     System.err.println("");
-    System.err
-        .println("Usage: hive [--config confdir] [-hiveconf x=y]* [<-f filename>|<-e
query-string>] [-S]");
+    System.err.println(
+      "Usage: hive [--config confdir] [-hiveconf x=y]* [-i <init-filename>]*"
+      + " [-f <filename>|-e <query-string>] [-S]");
     System.err.println("");
+    System.err.println("  -i <filename>             init Sql file");
     System.err.println("  -e 'quoted query string'  Sql from command line");
     System.err.println("  -f <filename>             Sql from files");
     System.err.println("  -S                        Silent mode in interactive shell");
     System.err.println("");
     System.err.println("-e and -f cannot be specified together. In the absence of these");
-    System.err.println("options, interactive shell is started");
+    System.err.println(" options, interactive shell is started.");
     System.err.println("");
 
   }

Added: hadoop/hive/trunk/data/scripts/test_init_file.sql
URL: http://svn.apache.org/viewvc/hadoop/hive/trunk/data/scripts/test_init_file.sql?rev=957745&view=auto
==============================================================================
--- hadoop/hive/trunk/data/scripts/test_init_file.sql (added)
+++ hadoop/hive/trunk/data/scripts/test_init_file.sql Thu Jun 24 22:50:51 2010
@@ -0,0 +1 @@
+create table tbl_created_by_init(i int);

Modified: hadoop/hive/trunk/ql/src/test/org/apache/hadoop/hive/ql/QTestUtil.java
URL: http://svn.apache.org/viewvc/hadoop/hive/trunk/ql/src/test/org/apache/hadoop/hive/ql/QTestUtil.java?rev=957745&r1=957744&r2=957745&view=diff
==============================================================================
--- hadoop/hive/trunk/ql/src/test/org/apache/hadoop/hive/ql/QTestUtil.java (original)
+++ hadoop/hive/trunk/ql/src/test/org/apache/hadoop/hive/ql/QTestUtil.java Thu Jun 24 22:50:51
2010
@@ -530,6 +530,10 @@ public class QTestUtil {
     }
     SessionState.start(ss);
     cliDriver = new CliDriver();
+    if (tname.equals("init_file.q")) {
+      ss.initFiles.add("../data/scripts/test_init_file.sql");
+    }
+    cliDriver.processInitFiles(ss);
   }
 
   public int executeOne(String tname) {

Added: hadoop/hive/trunk/ql/src/test/queries/clientpositive/init_file.q
URL: http://svn.apache.org/viewvc/hadoop/hive/trunk/ql/src/test/queries/clientpositive/init_file.q?rev=957745&view=auto
==============================================================================
--- hadoop/hive/trunk/ql/src/test/queries/clientpositive/init_file.q (added)
+++ hadoop/hive/trunk/ql/src/test/queries/clientpositive/init_file.q Thu Jun 24 22:50:51 2010
@@ -0,0 +1,5 @@
+-- tbl_created_by_init is supposed to have been created for us
+-- automatically by test_init_file.sql
+
+select * from tbl_created_by_init;
+drop table tbl_created_by_init;

Added: hadoop/hive/trunk/ql/src/test/results/clientpositive/init_file.q.out
URL: http://svn.apache.org/viewvc/hadoop/hive/trunk/ql/src/test/results/clientpositive/init_file.q.out?rev=957745&view=auto
==============================================================================
--- hadoop/hive/trunk/ql/src/test/results/clientpositive/init_file.q.out (added)
+++ hadoop/hive/trunk/ql/src/test/results/clientpositive/init_file.q.out Thu Jun 24 22:50:51
2010
@@ -0,0 +1,24 @@
+PREHOOK: query: create table tbl_created_by_init(i int)
+PREHOOK: type: CREATETABLE
+POSTHOOK: query: create table tbl_created_by_init(i int)
+POSTHOOK: type: CREATETABLE
+POSTHOOK: Output: default@tbl_created_by_init
+PREHOOK: query: -- tbl_created_by_init is supposed to have been created for us
+-- automatically by test_init_file.sql
+
+select * from tbl_created_by_init
+PREHOOK: type: QUERY
+PREHOOK: Input: default@tbl_created_by_init
+PREHOOK: Output: file:/var/folders/7P/7PeC14kXFIWq0PIYyexGbmKuXUk/-Tmp-/jsichi/hive_2010-06-18_15-26-08_648_7822849778594453961/10000
+POSTHOOK: query: -- tbl_created_by_init is supposed to have been created for us
+-- automatically by test_init_file.sql
+
+select * from tbl_created_by_init
+POSTHOOK: type: QUERY
+POSTHOOK: Input: default@tbl_created_by_init
+POSTHOOK: Output: file:/var/folders/7P/7PeC14kXFIWq0PIYyexGbmKuXUk/-Tmp-/jsichi/hive_2010-06-18_15-26-08_648_7822849778594453961/10000
+PREHOOK: query: drop table tbl_created_by_init
+PREHOOK: type: DROPTABLE
+POSTHOOK: query: drop table tbl_created_by_init
+POSTHOOK: type: DROPTABLE
+POSTHOOK: Output: default@tbl_created_by_init



Mime
View raw message