1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30 package org.openimaj.hadoop.tools.globalfeature;
31
32 import java.io.IOException;
33 import java.net.URI;
34 import java.util.List;
35
36 import org.apache.hadoop.conf.Configuration;
37 import org.apache.hadoop.fs.FileSystem;
38 import org.apache.hadoop.fs.LocalFileSystem;
39 import org.apache.hadoop.fs.Path;
40 import org.kohsuke.args4j.CmdLineException;
41 import org.kohsuke.args4j.CmdLineParser;
42 import org.kohsuke.args4j.Option;
43 import org.kohsuke.args4j.ProxyOptionHandler;
44 import org.openimaj.hadoop.sequencefile.SequenceFileUtility;
45 import org.openimaj.tools.globalfeature.GlobalFeatureExtractor;
46 import org.openimaj.tools.globalfeature.GlobalFeatureType;
47
48
49
50
51
52
53
54 public class HadoopGlobalFeaturesOptions {
55 private String[] args;
56
57 @Option(name="--feature-type", aliases="-f", handler=ProxyOptionHandler.class, usage="Feature type", required=true)
58 protected GlobalFeatureType feature;
59 protected GlobalFeatureExtractor featureOp;
60
61 @Option(name = "-input", aliases="-i", required=true, usage="Set the input path(s) or uri(s)")
62 protected List<String> input;
63
64 @Option(name = "-output", aliases="-o", required=true, usage="Set the output location")
65 protected String output;
66
67 @Option(name = "--binary", aliases="-b", required=false, usage="Set output mode to binary")
68 protected boolean binary = false;
69
70 @Option(name="--remove", aliases="-rm", required=false, usage="Remove the existing output location if it exists.", metaVar="BOOLEAN")
71 private boolean replace = false;
72
73 private boolean beforeMaps;
74
75
76
77
78
79 public HadoopGlobalFeaturesOptions(String[] args)
80 {
81 this(args, false);
82 }
83
84
85
86
87
88
89 public HadoopGlobalFeaturesOptions(String[] args, boolean beforeMaps) {
90 this.args = args;
91 this.beforeMaps = beforeMaps;
92 prepare();
93 }
94
95 private void prepare() {
96 CmdLineParser parser = new CmdLineParser(this);
97
98 try {
99 parser.parseArgument(args);
100 } catch (CmdLineException e) {
101 System.err.println(e.getMessage());
102 System.err.println("Usage: java -jar HadoopGlobalFeaturesTool.jar [options...]");
103 parser.printUsage(System.err);
104
105 if (feature == null) {
106 for (GlobalFeatureType m : GlobalFeatureType.values()) {
107 System.err.println();
108 System.err.println(m + " options: ");
109 new CmdLineParser(m.getOptions()).printUsage(System.err);
110 }
111 }
112 System.exit(1);
113 }
114 }
115
116 protected void validate() {
117 if(replace && beforeMaps) {
118 try {
119 URI outuri = SequenceFileUtility.convertToURI(output);
120 FileSystem fs = getFileSystem(outuri);
121 fs.delete(new Path(outuri.toString()), true);
122 } catch (IOException e) {
123
124 }
125 }
126 }
127
128 static FileSystem getFileSystem(URI uri) throws IOException {
129 Configuration config = new Configuration();
130 FileSystem fs = FileSystem.get(uri, config);
131 if (fs instanceof LocalFileSystem) fs = ((LocalFileSystem)fs).getRaw();
132 return fs;
133 }
134 }