Skip to content

Commit ff90245

Browse files
Add command line parser
1 parent d936879 commit ff90245

File tree

6 files changed

+91
-12
lines changed

6 files changed

+91
-12
lines changed

build.gradle

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@ apply plugin: 'java'
22
apply plugin: 'application'
33

44
group 'org.radarcns.restructurehdfs'
5-
version '0.3.2'
5+
version '0.3.3-SNAPSHOT'
66
mainClassName = 'org.radarcns.RestructureAvroRecords'
77

88
run {
@@ -15,6 +15,7 @@ targetCompatibility = '1.8'
1515
ext.avroVersion = '1.8.2'
1616
ext.jacksonVersion = '2.8.9'
1717
ext.hadoopVersion = '2.7.3'
18+
ext.jCommanderVersion = '1.72'
1819

1920
repositories {
2021
jcenter()
@@ -27,6 +28,8 @@ dependencies {
2728
compile group: 'com.fasterxml.jackson.core' , name: 'jackson-databind', version: jacksonVersion
2829
compile group: 'com.fasterxml.jackson.dataformat' , name: 'jackson-dataformat-csv', version: jacksonVersion
2930

31+
compile group: 'com.beust', name: 'jcommander', version: jCommanderVersion
32+
3033
runtime group: 'org.apache.hadoop', name: 'hadoop-hdfs', version: hadoopVersion
3134

3235
testCompile group: 'junit', name: 'junit', version: '4.12'

src/main/java/org/radarcns/RestructureAvroRecords.java

Lines changed: 16 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@
1616

1717
package org.radarcns;
1818

19+
import com.beust.jcommander.JCommander;
1920
import com.fasterxml.jackson.databind.JsonMappingException;
2021
import org.apache.avro.Schema.Field;
2122
import org.apache.avro.file.DataFileReader;
@@ -32,6 +33,7 @@
3233
import org.radarcns.util.JsonAvroConverter;
3334
import org.radarcns.util.ProgressBar;
3435
import org.radarcns.util.RecordConverterFactory;
36+
import org.radarcns.util.commandline.CommandLineArgs;
3537
import org.slf4j.Logger;
3638
import org.slf4j.LoggerFactory;
3739

@@ -70,27 +72,30 @@ public class RestructureAvroRecords {
7072

7173
private long processedFileCount;
7274
private long processedRecordsCount;
73-
private static final boolean USE_GZIP = "gzip".equalsIgnoreCase(System.getProperty("org.radarcns.compression"));
75+
private static boolean USE_GZIP;
76+
private static boolean DO_DEDUPLICATE;
7477

75-
// Default set to false because causes loss of records from Biovotion data. https://github.com/RADAR-base/Restructure-HDFS-topic/issues/16
76-
private static final boolean DO_DEDUPLICATE = "true".equalsIgnoreCase(System.getProperty("org.radarcns.deduplicate", "false"));
78+
private static final CommandLineArgs commandLineArgs = new CommandLineArgs();
79+
private static final JCommander parser = JCommander.newBuilder().addObject(commandLineArgs).build();
7780

7881
public static void main(String [] args) throws Exception {
79-
if (args.length != 3) {
80-
System.out.println("Usage: hadoop jar restructurehdfs-all-0.2.jar <webhdfs_url> <hdfs_root_directory> <output_folder>");
81-
System.exit(1);
82-
}
82+
83+
parser.setProgramName("hadoop jar restructurehdfs-all-0.3.2.jar");
84+
parser.parse(args);
85+
USE_GZIP = "gzip".equalsIgnoreCase(commandLineArgs.compression);
86+
DO_DEDUPLICATE = commandLineArgs.deduplicate;
8387

8488
logger.info(new SimpleDateFormat("yyyy-MM-dd HH:mm:ss").format(new Date()));
8589
logger.info("Starting...");
86-
logger.info("In: " + args[0] + args[1]);
87-
logger.info("Out: " + args[2]);
90+
logger.info("In: " + commandLineArgs.hdfsUri + commandLineArgs.hdfsRootDirectory);
91+
logger.info("Out: " + commandLineArgs.outputDirectory);
92+
logger.info("Deduplicate set to {}", DO_DEDUPLICATE);
8893

8994
long time1 = System.currentTimeMillis();
9095

91-
RestructureAvroRecords restr = new RestructureAvroRecords(args[0], args[2]);
96+
RestructureAvroRecords restr = new RestructureAvroRecords(commandLineArgs.hdfsUri, commandLineArgs.outputDirectory);
9297
try {
93-
restr.start(args[1]);
98+
restr.start(commandLineArgs.hdfsRootDirectory);
9499
} catch (IOException ex) {
95100
logger.error("Processing failed", ex);
96101
}
Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
package org.radarcns.util.commandline;
2+
3+
import com.beust.jcommander.IParameterValidator;
4+
import com.beust.jcommander.ParameterException;
5+
6+
public class BooleanValidator implements IParameterValidator{
7+
8+
@Override
9+
public void validate(String name, String value) throws ParameterException {
10+
if(!value.equalsIgnoreCase("true") && !value.equalsIgnoreCase("false")) {
11+
ParameterException exc = new ParameterException("Parameter " + name + " can only be true or false (found " + value +")");
12+
exc.usage();
13+
throw exc;
14+
}
15+
}
16+
}
Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
package org.radarcns.util.commandline;
2+
3+
import com.beust.jcommander.Parameter;
4+
5+
public class CommandLineArgs {
6+
@Parameter(names = { "-c", "--compression" }, description = "Compression to use when converting the files. Default is none. Gzip is available.")
7+
public String compression = "none";
8+
9+
// Default set to false because causes loss of records from Biovotion data. https://github.com/RADAR-base/Restructure-HDFS-topic/issues/16
10+
@Parameter(names = { "-d", "--deduplicate" }, description = "Boolean to define if to use deduplication or not.", validateWith = BooleanValidator.class)
11+
public Boolean deduplicate = false;
12+
13+
@Parameter(names = { "-u", "--hdfs-uri" }, description = "The HDFS uri to connect to. Eg - 'hdfs://<HOST>:<RPC_PORT>/<PATH>'.", required = true, validateWith = { HdfsUriValidator.class, PathValidator.class })
14+
public String hdfsUri;
15+
16+
@Parameter(names = { "-i", "--hdfs-root-directory" }, description = "The input HDFS root directory from which files are to be read. Eg - '/topicAndroidNew'", required = true, validateWith = PathValidator.class)
17+
public String hdfsRootDirectory;
18+
19+
@Parameter(names = { "-o", "--output-directory"}, description = "The output folder where the files are to be extracted.", required = true, validateWith = PathValidator.class)
20+
public String outputDirectory;
21+
22+
@Parameter(names = { "-h", "--help"}, help = true)
23+
public boolean help;
24+
}
Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
package org.radarcns.util.commandline;
2+
3+
4+
import com.beust.jcommander.ParameterException;
5+
import com.beust.jcommander.IParameterValidator;
6+
7+
public class HdfsUriValidator implements IParameterValidator{
8+
@Override
9+
public void validate(String name, String value) throws ParameterException {
10+
if (! value.matches("((hdfs)|(webhdfs)):(/?/?)[^\\s]+")) {
11+
ParameterException exc = new ParameterException("Parameter " + name + " should be a valid HDFS or WebHDFS URI. Eg - hdfs://<HOST>:<RPC_PORT>/<PATH>. (found " + value +")");
12+
exc.usage();
13+
throw exc;
14+
}
15+
}
16+
}
Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
package org.radarcns.util.commandline;
2+
3+
import com.beust.jcommander.ParameterException;
4+
import com.beust.jcommander.IParameterValidator;
5+
6+
public class PathValidator implements IParameterValidator{
7+
@Override
8+
public void validate(String name, String value) throws ParameterException {
9+
if (value == null || value.isEmpty()) {
10+
ParameterException exc = new ParameterException("Parameter " + name + " should be supplied. It cannot be empty or null. (found " + value +")");
11+
exc.usage();
12+
throw exc;
13+
}
14+
}
15+
}

0 commit comments

Comments
 (0)