hadoop-common-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From vinayakum...@apache.org
Subject [1/2] hadoop git commit: HADOOP-12214. Parse 'HadoopArchive' commandline using cli Options. (Contributed by Vinayakumar B)
Date Tue, 21 Jul 2015 07:44:02 GMT
Repository: hadoop
Updated Branches:
  refs/heads/branch-2 428383134 -> 8ef382135
  refs/heads/trunk df1e8ce44 -> 87f29c6b8


HADOOP-12214. Parse 'HadoopArchive' commandline using cli Options. (Contributed by Vinayakumar
B)


Project: http://git-wip-us.apache.org/repos/asf/hadoop/repo
Commit: http://git-wip-us.apache.org/repos/asf/hadoop/commit/87f29c6b
Tree: http://git-wip-us.apache.org/repos/asf/hadoop/tree/87f29c6b
Diff: http://git-wip-us.apache.org/repos/asf/hadoop/diff/87f29c6b

Branch: refs/heads/trunk
Commit: 87f29c6b8acc07cc011713a79554d51945e265ac
Parents: df1e8ce
Author: Vinayakumar B <vinayakumarb@apache.org>
Authored: Tue Jul 21 13:12:46 2015 +0530
Committer: Vinayakumar B <vinayakumarb@apache.org>
Committed: Tue Jul 21 13:12:46 2015 +0530

----------------------------------------------------------------------
 hadoop-common-project/hadoop-common/CHANGES.txt |  3 +
 .../org/apache/hadoop/tools/HadoopArchives.java | 87 +++++++++++++-------
 .../apache/hadoop/tools/TestHadoopArchives.java |  4 +-
 3 files changed, 64 insertions(+), 30 deletions(-)
----------------------------------------------------------------------


http://git-wip-us.apache.org/repos/asf/hadoop/blob/87f29c6b/hadoop-common-project/hadoop-common/CHANGES.txt
----------------------------------------------------------------------
diff --git a/hadoop-common-project/hadoop-common/CHANGES.txt b/hadoop-common-project/hadoop-common/CHANGES.txt
index ef8e238..24709e0 100644
--- a/hadoop-common-project/hadoop-common/CHANGES.txt
+++ b/hadoop-common-project/hadoop-common/CHANGES.txt
@@ -708,6 +708,9 @@ Release 2.8.0 - UNRELEASED
     HADOOP-12081. Fix UserGroupInformation.java to support 64-bit zLinux.
     (aajisaka)
 
+    HADOOP-12214. Parse 'HadoopArchive' commandline using cli Options.
+    (vinayakumarb)
+
   OPTIMIZATIONS
 
     HADOOP-11785. Reduce the number of listStatus operation in distcp

http://git-wip-us.apache.org/repos/asf/hadoop/blob/87f29c6b/hadoop-tools/hadoop-archives/src/main/java/org/apache/hadoop/tools/HadoopArchives.java
----------------------------------------------------------------------
diff --git a/hadoop-tools/hadoop-archives/src/main/java/org/apache/hadoop/tools/HadoopArchives.java
b/hadoop-tools/hadoop-archives/src/main/java/org/apache/hadoop/tools/HadoopArchives.java
index f00bb6d..330830b 100644
--- a/hadoop-tools/hadoop-archives/src/main/java/org/apache/hadoop/tools/HadoopArchives.java
+++ b/hadoop-tools/hadoop-archives/src/main/java/org/apache/hadoop/tools/HadoopArchives.java
@@ -33,6 +33,11 @@ import java.util.Random;
 import java.util.Set;
 import java.util.TreeMap;
 
+import org.apache.commons.cli.CommandLine;
+import org.apache.commons.cli.GnuParser;
+import org.apache.commons.cli.HelpFormatter;
+import org.apache.commons.cli.Options;
+import org.apache.commons.cli.Parser;
 import org.apache.commons.logging.Log;
 import org.apache.commons.logging.LogFactory;
 import org.apache.hadoop.conf.Configuration;
@@ -81,6 +86,10 @@ public class HadoopArchives implements Tool {
   private static final Log LOG = LogFactory.getLog(HadoopArchives.class);
   
   private static final String NAME = "har"; 
+  private static final String ARCHIVE_NAME = "archiveName";
+  private static final String REPLICATION = "r";
+  private static final String PARENT_PATH = "p";
+  private static final String HELP = "help";
   static final String SRC_LIST_LABEL = NAME + ".src.list";
   static final String DST_DIR_LABEL = NAME + ".dest.path";
   static final String TMP_DIR_LABEL = NAME + ".tmp.dir";
@@ -101,9 +110,9 @@ public class HadoopArchives implements Tool {
   /** the desired replication degree; default is 10 **/
   short repl = 10;
 
-  private static final String usage = "Usage: archive"
-  + " -archiveName <NAME>.har -p <parent path> [-r <replication factor>]"
+
-      "<src>* <dest>" +
+  private static final String usage = "archive"
+  + " <-archiveName <NAME>.har> <-p <parent path>> [-r <replication
factor>]" +
+      " <src>* <dest>" +
   "\n";
   
  
@@ -794,7 +803,17 @@ public class HadoopArchives implements Tool {
     }
     
   }
-  
+
+  private void printUsage(Options opts, boolean printDetailed) {
+    HelpFormatter helpFormatter = new HelpFormatter();
+    if (printDetailed) {
+      helpFormatter.printHelp(usage.length() + 10, usage, null, opts, null,
+          false);
+    } else {
+      System.out.println(usage);
+    }
+  }
+
   /** the main driver for creating the archives
    *  it takes at least three command line parameters. The parent path, 
    *  The src and the dest. It does an lsr on the source paths.
@@ -804,43 +823,51 @@ public class HadoopArchives implements Tool {
 
   public int run(String[] args) throws Exception {
     try {
-      Path parentPath = null;
-      List<Path> srcPaths = new ArrayList<Path>();
-      Path destPath = null;
-      String archiveName = null;
-      if (args.length < 5) {
-        System.out.println(usage);
-        throw new IOException("Invalid usage.");
-      }
-      if (!"-archiveName".equals(args[0])) {
-        System.out.println(usage);
+      // Parse CLI options
+      Options options = new Options();
+      options.addOption(ARCHIVE_NAME, true,
+          "Name of the Archive. This is mandatory option");
+      options.addOption(PARENT_PATH, true,
+          "Parent path of sources. This is mandatory option");
+      options.addOption(REPLICATION, true, "Replication factor archive files");
+      options.addOption(HELP, false, "Show the usage");
+      Parser parser = new GnuParser();
+      CommandLine commandLine = parser.parse(options, args, true);
+
+      if (commandLine.hasOption(HELP)) {
+        printUsage(options, true);
+        return 0;
+      }
+      if (!commandLine.hasOption(ARCHIVE_NAME)) {
+        printUsage(options, false);
         throw new IOException("Archive Name not specified.");
       }
-      archiveName = args[1];
+      String archiveName = commandLine.getOptionValue(ARCHIVE_NAME);
       if (!checkValidName(archiveName)) {
-        System.out.println(usage);
+        printUsage(options, false);
         throw new IOException("Invalid name for archives. " + archiveName);
       }
-      int i = 2;
       //check to see if relative parent has been provided or not
       //this is a required parameter. 
-      if (! "-p".equals(args[i])) {
-        System.out.println(usage);
+      if (!commandLine.hasOption(PARENT_PATH)) {
+        printUsage(options, false);
         throw new IOException("Parent path not specified.");
       }
-      parentPath = new Path(args[i+1]);
+      Path parentPath = new Path(commandLine.getOptionValue(PARENT_PATH));
       if (!parentPath.isAbsolute()) {
-        parentPath= parentPath.getFileSystem(getConf()).makeQualified(parentPath);
+        parentPath = parentPath.getFileSystem(getConf()).makeQualified(
+            parentPath);
       }
 
-      i+=2;
-
-      if ("-r".equals(args[i])) {
-        repl = Short.parseShort(args[i+1]);
-        i+=2;
+      if (commandLine.hasOption(REPLICATION)) {
+        repl = Short.parseShort(commandLine.getOptionValue(REPLICATION));
       }
+      // Remaining args
+      args = commandLine.getArgs();
+      List<Path> srcPaths = new ArrayList<Path>();
+      Path destPath = null;
       //read the rest of the paths
-      for (; i < args.length; i++) {
+      for (int i = 0; i < args.length; i++) {
         if (i == (args.length - 1)) {
           destPath = new Path(args[i]);
           if (!destPath.isAbsolute()) {
@@ -850,13 +877,17 @@ public class HadoopArchives implements Tool {
         else {
           Path argPath = new Path(args[i]);
           if (argPath.isAbsolute()) {
-            System.out.println(usage);
+            printUsage(options, false);
             throw new IOException("Source path " + argPath +
                 " is not relative to "+ parentPath);
           }
           srcPaths.add(new Path(parentPath, argPath));
         }
       }
+      if (destPath == null) {
+        printUsage(options, false);
+        throw new IOException("Destination path not specified.");
+      }
       if (srcPaths.size() == 0) {
         // assuming if the user does not specify path for sources
         // the whole parent directory needs to be archived. 

http://git-wip-us.apache.org/repos/asf/hadoop/blob/87f29c6b/hadoop-tools/hadoop-archives/src/test/java/org/apache/hadoop/tools/TestHadoopArchives.java
----------------------------------------------------------------------
diff --git a/hadoop-tools/hadoop-archives/src/test/java/org/apache/hadoop/tools/TestHadoopArchives.java
b/hadoop-tools/hadoop-archives/src/test/java/org/apache/hadoop/tools/TestHadoopArchives.java
index 101cb06..d8222dc 100644
--- a/hadoop-tools/hadoop-archives/src/test/java/org/apache/hadoop/tools/TestHadoopArchives.java
+++ b/hadoop-tools/hadoop-archives/src/test/java/org/apache/hadoop/tools/TestHadoopArchives.java
@@ -753,8 +753,8 @@ public class TestHadoopArchives {
 
     final String harName = "foo.har";
     final String fullHarPathStr = prefix + harName;
-    final String[] args = { "-archiveName", harName, "-p", inputPathStr,
-        "-r 3", "*", archivePath.toString() };
+    final String[] args = { "-archiveName", harName, "-p", inputPathStr, "-r",
+        "3", "*", archivePath.toString() };
     System.setProperty(HadoopArchives.TEST_HADOOP_ARCHIVES_JAR_PATH,
         HADOOP_ARCHIVES_JAR);
     final HadoopArchives har = new HadoopArchives(conf);


Mime
View raw message