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

This commit is contained in:
Vinayakumar B 2015-07-21 13:12:46 +05:30
parent df1e8ce44a
commit 87f29c6b8a
3 changed files with 63 additions and 29 deletions

View File

@ -708,6 +708,9 @@ Release 2.8.0 - UNRELEASED
HADOOP-12081. Fix UserGroupInformation.java to support 64-bit zLinux. HADOOP-12081. Fix UserGroupInformation.java to support 64-bit zLinux.
(aajisaka) (aajisaka)
HADOOP-12214. Parse 'HadoopArchive' commandline using cli Options.
(vinayakumarb)
OPTIMIZATIONS OPTIMIZATIONS
HADOOP-11785. Reduce the number of listStatus operation in distcp HADOOP-11785. Reduce the number of listStatus operation in distcp

View File

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

View File

@ -753,8 +753,8 @@ private String makeArchiveWithRepl() throws Exception {
final String harName = "foo.har"; final String harName = "foo.har";
final String fullHarPathStr = prefix + harName; final String fullHarPathStr = prefix + harName;
final String[] args = { "-archiveName", harName, "-p", inputPathStr, final String[] args = { "-archiveName", harName, "-p", inputPathStr, "-r",
"-r 3", "*", archivePath.toString() }; "3", "*", archivePath.toString() };
System.setProperty(HadoopArchives.TEST_HADOOP_ARCHIVES_JAR_PATH, System.setProperty(HadoopArchives.TEST_HADOOP_ARCHIVES_JAR_PATH,
HADOOP_ARCHIVES_JAR); HADOOP_ARCHIVES_JAR);
final HadoopArchives har = new HadoopArchives(conf); final HadoopArchives har = new HadoopArchives(conf);