Return-Path: X-Original-To: apmail-hadoop-common-commits-archive@www.apache.org Delivered-To: apmail-hadoop-common-commits-archive@www.apache.org Received: from mail.apache.org (hermes.apache.org [140.211.11.3]) by minotaur.apache.org (Postfix) with SMTP id 5B7CE18162 for ; Tue, 21 Jul 2015 07:44:03 +0000 (UTC) Received: (qmail 49259 invoked by uid 500); 21 Jul 2015 07:44:02 -0000 Delivered-To: apmail-hadoop-common-commits-archive@hadoop.apache.org Received: (qmail 49144 invoked by uid 500); 21 Jul 2015 07:44:02 -0000 Mailing-List: contact common-commits-help@hadoop.apache.org; run by ezmlm Precedence: bulk List-Help: List-Unsubscribe: List-Post: List-Id: Reply-To: common-dev@hadoop.apache.org Delivered-To: mailing list common-commits@hadoop.apache.org Received: (qmail 49125 invoked by uid 99); 21 Jul 2015 07:44:02 -0000 Received: from git1-us-west.apache.org (HELO git1-us-west.apache.org) (140.211.11.23) by apache.org (qpsmtpd/0.29) with ESMTP; Tue, 21 Jul 2015 07:44:02 +0000 Received: by git1-us-west.apache.org (ASF Mail Server at git1-us-west.apache.org, from userid 33) id 875D5E0286; Tue, 21 Jul 2015 07:44:02 +0000 (UTC) Content-Type: text/plain; charset="us-ascii" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit From: vinayakumarb@apache.org To: common-commits@hadoop.apache.org Date: Tue, 21 Jul 2015 07:44:03 -0000 Message-Id: <6b63daf1898d40a18317681c59bb07ae@git.apache.org> In-Reply-To: References: X-Mailer: ASF-Git Admin Mailer Subject: [2/2] hadoop git commit: HADOOP-12214. Parse 'HadoopArchive' commandline using cli Options. (Contributed by Vinayakumar B) HADOOP-12214. Parse 'HadoopArchive' commandline using cli Options. (Contributed by Vinayakumar B) (cherry picked from commit 87f29c6b8acc07cc011713a79554d51945e265ac) Project: http://git-wip-us.apache.org/repos/asf/hadoop/repo Commit: http://git-wip-us.apache.org/repos/asf/hadoop/commit/8ef38213 Tree: http://git-wip-us.apache.org/repos/asf/hadoop/tree/8ef38213 Diff: http://git-wip-us.apache.org/repos/asf/hadoop/diff/8ef38213 Branch: refs/heads/branch-2 Commit: 8ef382135684c4c1e05619f6fb6d1d2cf7093192 Parents: 4283831 Author: Vinayakumar B Authored: Tue Jul 21 13:12:46 2015 +0530 Committer: Vinayakumar B Committed: Tue Jul 21 13:13:31 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/8ef38213/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 c044b8c..99a83e4 100644 --- a/hadoop-common-project/hadoop-common/CHANGES.txt +++ b/hadoop-common-project/hadoop-common/CHANGES.txt @@ -205,6 +205,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/8ef38213/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 .har -p [-r ]" + - "* " + + private static final String usage = "archive" + + " <-archiveName .har> <-p > [-r ]" + + " * " + "\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 srcPaths = new ArrayList(); - 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 srcPaths = new ArrayList(); + 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/8ef38213/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);