1 | /** jmt12 **/
|
---|
2 | package org.nzdl.gsdl;
|
---|
3 |
|
---|
4 | import java.io.BufferedOutputStream;
|
---|
5 | import java.io.BufferedReader;
|
---|
6 | import java.io.File;
|
---|
7 | import java.io.FileOutputStream;
|
---|
8 | import java.io.FileWriter;
|
---|
9 | import java.io.InputStream;
|
---|
10 | import java.io.InputStreamReader;
|
---|
11 | import java.io.IOException;
|
---|
12 | import java.io.PrintWriter;
|
---|
13 | import java.lang.ProcessBuilder;
|
---|
14 | import java.lang.ProcessBuilder.*;
|
---|
15 | import java.lang.Thread;
|
---|
16 | import java.net.InetAddress;
|
---|
17 | import java.util.Map;
|
---|
18 |
|
---|
19 | import org.apache.hadoop.fs.Path;
|
---|
20 | import org.apache.hadoop.conf.*;
|
---|
21 | import org.apache.hadoop.io.*;
|
---|
22 | import org.apache.hadoop.mapreduce.*;
|
---|
23 | import org.apache.hadoop.mapreduce.Mapper.Context;
|
---|
24 | import org.apache.hadoop.mapreduce.lib.input.FileInputFormat;
|
---|
25 | import org.apache.hadoop.mapreduce.lib.input.FileSplit;
|
---|
26 | import org.apache.hadoop.mapreduce.lib.output.FileOutputFormat;
|
---|
27 | import org.apache.hadoop.mapreduce.lib.output.NullOutputFormat;
|
---|
28 | import org.apache.hadoop.mapreduce.lib.output.TextOutputFormat;
|
---|
29 | import org.apache.hadoop.util.*;
|
---|
30 |
|
---|
31 | /** @class WordCount
|
---|
32 | */
|
---|
33 | public class HadoopGreenstoneIngest
|
---|
34 | {
|
---|
35 |
|
---|
36 | /** @class GSFileRecordReader
|
---|
37 | */
|
---|
38 | public static class GSFileRecordReader
|
---|
39 | extends RecordReader<Text, IntWritable>
|
---|
40 | {
|
---|
41 | /** Uncompressed file name */
|
---|
42 | private Text current_key;
|
---|
43 |
|
---|
44 | private IntWritable current_value = new IntWritable(1);
|
---|
45 |
|
---|
46 | /** Used to indicate progress */
|
---|
47 | private boolean is_finished = false;
|
---|
48 |
|
---|
49 | /**
|
---|
50 | */
|
---|
51 | @Override
|
---|
52 | public void initialize( InputSplit inputSplit, TaskAttemptContext taskAttemptContext )
|
---|
53 | throws IOException, InterruptedException
|
---|
54 | {
|
---|
55 | FileSplit split = (FileSplit) inputSplit;
|
---|
56 | current_key = new Text(split.getPath().toString());
|
---|
57 | }
|
---|
58 | /** initialize() **/
|
---|
59 |
|
---|
60 | /**
|
---|
61 | * We only ever have a single
|
---|
62 | */
|
---|
63 | @Override
|
---|
64 | public boolean nextKeyValue()
|
---|
65 | throws IOException, InterruptedException
|
---|
66 | {
|
---|
67 | if (!is_finished)
|
---|
68 | {
|
---|
69 | is_finished = true;
|
---|
70 | return true;
|
---|
71 | }
|
---|
72 | return false;
|
---|
73 | }
|
---|
74 | /** nextKeyValue() **/
|
---|
75 |
|
---|
76 | /** @function getProgress
|
---|
77 | * Rather than calculating progress, we just keep it simple
|
---|
78 | */
|
---|
79 | @Override
|
---|
80 | public float getProgress()
|
---|
81 | throws IOException, InterruptedException
|
---|
82 | {
|
---|
83 | return is_finished ? 1 : 0;
|
---|
84 | }
|
---|
85 | /** getProgress() **/
|
---|
86 |
|
---|
87 | /**
|
---|
88 | * Returns the current key (name of the zipped file)
|
---|
89 | */
|
---|
90 | @Override
|
---|
91 | public Text getCurrentKey()
|
---|
92 | throws IOException, InterruptedException
|
---|
93 | {
|
---|
94 | return current_key;
|
---|
95 | }
|
---|
96 | /** getCurrentKey() **/
|
---|
97 |
|
---|
98 | /**
|
---|
99 | * Returns the current value (contents of the zipped file)
|
---|
100 | */
|
---|
101 | @Override
|
---|
102 | public IntWritable getCurrentValue()
|
---|
103 | throws IOException, InterruptedException
|
---|
104 | {
|
---|
105 | return current_value;
|
---|
106 | }
|
---|
107 | /** getCurrentValue() **/
|
---|
108 |
|
---|
109 | /**
|
---|
110 | * Close quietly, ignoring any exceptions
|
---|
111 | */
|
---|
112 | @Override
|
---|
113 | public void close()
|
---|
114 | throws IOException
|
---|
115 | {
|
---|
116 | // nothing to do
|
---|
117 | }
|
---|
118 | /** close() **/
|
---|
119 |
|
---|
120 | }
|
---|
121 | /** GSFileRecordReader **/
|
---|
122 |
|
---|
123 | /** @class GSFileInputFormat
|
---|
124 | */
|
---|
125 | public static class GSFileInputFormat
|
---|
126 | extends FileInputFormat<Text, IntWritable>
|
---|
127 | {
|
---|
128 | /**
|
---|
129 | * Don't split the files
|
---|
130 | */
|
---|
131 | @Override
|
---|
132 | protected boolean isSplitable(JobContext context, Path filename)
|
---|
133 | {
|
---|
134 | return false;
|
---|
135 | }
|
---|
136 | /** isSplitable() **/
|
---|
137 |
|
---|
138 | /**
|
---|
139 | */
|
---|
140 | @Override
|
---|
141 | public RecordReader<Text, IntWritable> createRecordReader(InputSplit split, TaskAttemptContext content)
|
---|
142 | throws IOException, InterruptedException
|
---|
143 | {
|
---|
144 | return new GSFileRecordReader();
|
---|
145 | }
|
---|
146 | /** createRecordReader() **/
|
---|
147 |
|
---|
148 | }
|
---|
149 | /** class GSFileInputFormat **/
|
---|
150 |
|
---|
151 | /** @class GSMap
|
---|
152 | */
|
---|
153 | public static class GSMap
|
---|
154 | extends Mapper<Text, IntWritable, Text, IntWritable>
|
---|
155 | {
|
---|
156 | /** @function map
|
---|
157 | * The key is the full path (HDFS) of the file to be processed.
|
---|
158 | */
|
---|
159 | public void map(Text key, IntWritable value, Context context)
|
---|
160 | throws IOException, InterruptedException
|
---|
161 | {
|
---|
162 | String file_path = key.toString();
|
---|
163 | // - configuration for the task
|
---|
164 | Configuration conf = context.getConfiguration();
|
---|
165 | String gsdlhome = conf.get("gsdlhome");
|
---|
166 | String hdfs_prefix = conf.get("hdfsprefix");
|
---|
167 | String hadoop_prefix = conf.get("hadoopprefix");
|
---|
168 | String collection = conf.get("collection");
|
---|
169 | String task_id = conf.get("mapred.task.id");
|
---|
170 | task_id = task_id.substring(8); // remove "attempt_" prefix
|
---|
171 | // - create a temporary directory
|
---|
172 | File greenstone_tmp_dir = new File("/tmp/greenstone");
|
---|
173 | if (!greenstone_tmp_dir.isDirectory())
|
---|
174 | {
|
---|
175 | greenstone_tmp_dir.mkdir();
|
---|
176 | }
|
---|
177 | // - open a unique log file
|
---|
178 | File import_process_log = new File("/tmp/greenstone/import-hadoop-" + task_id + ".log");
|
---|
179 | // - start the log by writing the time and the manifest line
|
---|
180 | FileWriter fw1 = new FileWriter(import_process_log, true);
|
---|
181 | long start_time = System.currentTimeMillis()/1000;
|
---|
182 | fw1.write("[Started:" + start_time + "]\n");
|
---|
183 | fw1.write("[Host:" + InetAddress.getLocalHost().getHostName() + "]\n");
|
---|
184 | fw1.write("[Task:" + task_id + "]\n");
|
---|
185 | fw1.write("[Map:" + file_path + " => " + value + "]\n");
|
---|
186 |
|
---|
187 | // - create a temporary manifest file to process this file. Overwrite any
|
---|
188 | // existing file
|
---|
189 | File manifest_path = new File("/tmp/greenstone/manifest" + task_id + ".xml");
|
---|
190 | FileWriter manifest_writer = new FileWriter(manifest_path);
|
---|
191 | manifest_writer.write("<Manifest version=\"2.0\">\n");
|
---|
192 | manifest_writer.write("\t<Index>\n");
|
---|
193 | manifest_writer.write("\t\t<Filename>" + file_path + "</Filename>\n");
|
---|
194 | manifest_writer.write("\t</Index>\n");
|
---|
195 | manifest_writer.write("</Manifest>\n");
|
---|
196 | manifest_writer.close();
|
---|
197 |
|
---|
198 | // - call Greenstone passing in the path to the manifest
|
---|
199 | ProcessBuilder import_process_builder
|
---|
200 | = new ProcessBuilder("time", "-p", "import.pl", "-manifest", manifest_path.toString(), "-keepold", "-verbosity", "42", "-archivedir", hdfs_prefix + "/user/jmt12/gsdl/collect/" + collection + "/archives", collection);
|
---|
201 | fw1.write("[Command:" + import_process_builder.command() + "]\n");
|
---|
202 | // - alter environment
|
---|
203 | Map<String, String> import_process_env = import_process_builder.environment();
|
---|
204 | // - path
|
---|
205 | String path = import_process_env.get("PATH");
|
---|
206 | path = gsdlhome + "/ext/parallel-building/bin/script:" + path;
|
---|
207 | path = gsdlhome + "/ext/parallel-building/linux/bin:" + path;
|
---|
208 | path = hadoop_prefix + "/bin:" + path;
|
---|
209 | path = gsdlhome + "/ext/tdb-edit/linux/bin:" + path;
|
---|
210 | path = gsdlhome + "/ext/tdb-edit/bin/script:" + path;
|
---|
211 | path = gsdlhome + "/ext/video-and-audio/linux/bin:" + path;
|
---|
212 | path = gsdlhome + "/bin/script:" + path;
|
---|
213 | path = gsdlhome + "/bin/linux:" + path;
|
---|
214 | import_process_env.put("PATH", path);
|
---|
215 | // - ld_library_path
|
---|
216 | import_process_env.put("LD_LIBRARY_PATH", gsdlhome + "/ext/parallel-building/linux/lib:" + gsdlhome + "/ext/hadoop/linux/lib:" + gsdlhome + "/ext/video-and-audio/linux/lib:" + gsdlhome + "/ext/tdb-edit/linux/lib");
|
---|
217 | // - dyld_library_path
|
---|
218 | import_process_env.put("DYLD_LIBRARY_PATH", gsdlhome + "/ext/video-and-audio/linux/lib");
|
---|
219 | // - misc
|
---|
220 | import_process_env.put("GSDLHOME", gsdlhome);
|
---|
221 | import_process_env.put("GSDLOS", "linux");
|
---|
222 | import_process_env.put("GSDLEXTS", "parallel-building:tdb-edit:video-and-audio");
|
---|
223 | // - installed extension paths
|
---|
224 | import_process_env.put("GEXTPARALLELBUILDING_INSTALLED", gsdlhome + "/ext/parallel-building/linux");
|
---|
225 | import_process_env.put("GEXTVIDEO_INSTALLED", gsdlhome + "/ext/video-and-audio/linux");
|
---|
226 | import_process_env.put("GEXTTDBEDIT_INSTALLED", gsdlhome + "/ext/tdb-edit/linux");
|
---|
227 | // - Hadoop specific
|
---|
228 | import_process_env.put("HADOOP_PREFIX", hadoop_prefix);
|
---|
229 | // - change working directory
|
---|
230 | import_process_builder.directory(new File(gsdlhome));
|
---|
231 | // - close our output to the log before opening in the process
|
---|
232 | fw1.close();
|
---|
233 |
|
---|
234 | // - write output to log
|
---|
235 | import_process_builder.redirectErrorStream(true);
|
---|
236 | import_process_builder.redirectOutput(Redirect.appendTo(import_process_log));
|
---|
237 |
|
---|
238 | // - create progress reporter (so Hadoop doesn't time us out)
|
---|
239 | Thread reporter = new HadoopProgressReporter(context, import_process_log);
|
---|
240 | reporter.start();
|
---|
241 |
|
---|
242 | // - run process
|
---|
243 | Process import_process = import_process_builder.start();
|
---|
244 | try
|
---|
245 | {
|
---|
246 | int import_status = import_process.waitFor();
|
---|
247 | if (import_status != 0)
|
---|
248 | {
|
---|
249 | throw new Exception("exit status: " + import_status);
|
---|
250 | }
|
---|
251 | }
|
---|
252 | catch (Exception e)
|
---|
253 | {
|
---|
254 | System.err.println("Error! Import command failed (" + e.toString() + ")");
|
---|
255 | }
|
---|
256 |
|
---|
257 | // - stop the progress reporter as, one way or another, there will be no
|
---|
258 | // more progress
|
---|
259 | reporter.interrupt();
|
---|
260 | reporter = null; // force gc
|
---|
261 |
|
---|
262 | // - write end time to log
|
---|
263 | FileWriter fw2 = new FileWriter(import_process_log, true);
|
---|
264 | long end_time = System.currentTimeMillis()/1000;
|
---|
265 | fw2.write("[Completed:" + end_time + "]\n");
|
---|
266 | fw2.close();
|
---|
267 |
|
---|
268 | // - for now return a dummy output. In the future I may want to parse the
|
---|
269 | // output from Greenstone as output and allow reducing to make me a
|
---|
270 | // pretty timebased log
|
---|
271 | context.write(key, value);
|
---|
272 | }
|
---|
273 | /** map(LongWritable,Text,Context) **/
|
---|
274 |
|
---|
275 | }
|
---|
276 | /** class GSMap **/
|
---|
277 |
|
---|
278 | /** @function main
|
---|
279 | */
|
---|
280 | public static void main(String[] args)
|
---|
281 | throws Exception
|
---|
282 | {
|
---|
283 | if (args.length < 6)
|
---|
284 | {
|
---|
285 | System.out.println("Usage: bin/hadoop jar hadoop-greenstone.jar org.nzdl.gsdl.HadoopGreenstoneIngest <gsdlhome> <hdfsprefix> <hadoop prefix> <collection> <hdfsin> <hdfsout>\n");
|
---|
286 | System.exit(0);
|
---|
287 | }
|
---|
288 |
|
---|
289 | Configuration conf = new Configuration();
|
---|
290 | conf.set("gsdlhome", args[0]);
|
---|
291 | conf.set("hdfsprefix", args[1]); // HDThriftFS or HDFSShell
|
---|
292 | conf.set("hadoopprefix", args[2]);
|
---|
293 | conf.set("collection", args[3]);
|
---|
294 | // Set the number of retries to 1 - hopefully one of the following will work
|
---|
295 | conf.setInt("mapred.map.max.attempts", 1); // Old Hadoop
|
---|
296 | conf.setInt("mapreduce.map.maxattempts", 1); // Hadoop 2.0.3-alpha
|
---|
297 | conf.setInt("mapreduce.map.max.attempts", 1); // Solution on Web
|
---|
298 | // prevent timeouts
|
---|
299 | long milli_seconds = 60*60*1000; // 1 hour
|
---|
300 | conf.setLong("mapred.task.timeout", milli_seconds);
|
---|
301 | Job job = new Job(conf, "hadoopgreenstoneingest");
|
---|
302 | job.setJarByClass(HadoopGreenstoneIngest.class);
|
---|
303 |
|
---|
304 | job.setOutputKeyClass(Text.class);
|
---|
305 | job.setOutputValueClass(IntWritable.class);
|
---|
306 |
|
---|
307 | // Register the map, combiner, and reducer classes
|
---|
308 | job.setMapperClass(GSMap.class);
|
---|
309 | // - in theory, uses the IdentityReducer by default, which simply returns
|
---|
310 | // the input as the output (so no processing)
|
---|
311 | job.setNumReduceTasks(0);
|
---|
312 |
|
---|
313 | // Sets the input and output handlers - may need to adjust input to provide me
|
---|
314 | // a series of filenames (TextInputFormat will instead read in a text file and
|
---|
315 | // return each line...)
|
---|
316 | job.setInputFormatClass(GSFileInputFormat.class);
|
---|
317 | job.setOutputFormatClass(NullOutputFormat.class);
|
---|
318 | //job.setOutputFormatClass(TextOutputFormat.class);
|
---|
319 |
|
---|
320 | // Register the input and output paths
|
---|
321 | // - this input path should be to a file (in HDFS) that lists the paths to
|
---|
322 | // the manifest files
|
---|
323 | FileInputFormat.setInputPaths(job, new Path(args[4]));
|
---|
324 | // - for now the output isn't that important, but in the future I may use
|
---|
325 | // this mechanism to produce a time based log.
|
---|
326 | FileOutputFormat.setOutputPath(job, new Path(args[5]));
|
---|
327 |
|
---|
328 | // Recommended notation despite my hatiness of ?: syntax
|
---|
329 | System.exit(job.waitForCompletion(true)?0:1);
|
---|
330 | }
|
---|
331 | /** main(String[]) **/
|
---|
332 | }
|
---|
333 |
|
---|
334 | class HadoopProgressReporter
|
---|
335 | extends Thread
|
---|
336 | {
|
---|
337 |
|
---|
338 | private Context hadoop_process;
|
---|
339 |
|
---|
340 | private File log_file;
|
---|
341 |
|
---|
342 | HadoopProgressReporter(Context hadoop_process, File log_file)
|
---|
343 | {
|
---|
344 | this.hadoop_process = hadoop_process;
|
---|
345 | this.log_file = log_file;
|
---|
346 | }
|
---|
347 |
|
---|
348 | public void run()
|
---|
349 | {
|
---|
350 | try
|
---|
351 | {
|
---|
352 | while (!this.isInterrupted())
|
---|
353 | {
|
---|
354 | sleep(60000); // Wait a minute
|
---|
355 | //FileWriter fw1 = new FileWriter(this.log_file, true);
|
---|
356 | //long time = System.currentTimeMillis()/1000;
|
---|
357 | //fw1.write("[" + time + "] HadoopProgressReporter.progress()\n");
|
---|
358 | //fw1.close();
|
---|
359 | this.hadoop_process.progress(); // Inform Hadoop we are still processing
|
---|
360 | }
|
---|
361 | }
|
---|
362 | catch (InterruptedException iex)
|
---|
363 | {
|
---|
364 | // We've been interrupted: no more progress
|
---|
365 | }
|
---|
366 | catch (Exception ex)
|
---|
367 | {
|
---|
368 | ex.printStackTrace();
|
---|
369 | }
|
---|
370 | }
|
---|
371 | }
|
---|