1 | package org.hathitrust;
|
---|
2 |
|
---|
3 | import java.io.Serializable;
|
---|
4 | import org.apache.commons.cli.*;
|
---|
5 |
|
---|
6 | import org.apache.spark.api.java.*;
|
---|
7 | import org.apache.spark.SparkConf;
|
---|
8 |
|
---|
9 | public class PrepareForIngest implements Serializable
|
---|
10 | {
|
---|
11 | private static final long serialVersionUID = 1L;
|
---|
12 |
|
---|
13 | public static final int NUM_PARTITIONS = 6; // default would appear to be 2
|
---|
14 |
|
---|
15 | protected String _input_dir;
|
---|
16 | protected String _json_list_filename;
|
---|
17 | protected String _output_dir;
|
---|
18 |
|
---|
19 | protected int _verbosity;
|
---|
20 |
|
---|
21 | public PrepareForIngest(String input_dir, String json_list_filename, String output_dir, int verbosity)
|
---|
22 | {
|
---|
23 | _input_dir = input_dir;
|
---|
24 | _json_list_filename = (json_list_filename != null) ? json_list_filename : input_dir;
|
---|
25 | _output_dir = output_dir;
|
---|
26 | _verbosity = verbosity;
|
---|
27 | }
|
---|
28 |
|
---|
29 | public void exec()
|
---|
30 | {
|
---|
31 | String spark_app_name = "HathiTrust Extract Features: Prepare for Solr Ingest";
|
---|
32 | spark_app_name += " [" + _json_list_filename + "]";
|
---|
33 |
|
---|
34 | SparkConf conf = new SparkConf().setAppName(spark_app_name);
|
---|
35 | JavaSparkContext jsc = new JavaSparkContext(conf);
|
---|
36 |
|
---|
37 | if (_verbosity >= 2) {
|
---|
38 | System.out.println("Default Minimum Partions: " + jsc.defaultMinPartitions());
|
---|
39 | System.out.println("Default Parallelism: " + jsc.defaultParallelism());
|
---|
40 | }
|
---|
41 |
|
---|
42 | JavaRDD<String> json_list_data = jsc.textFile(_json_list_filename,NUM_PARTITIONS).cache();
|
---|
43 |
|
---|
44 | PagedJSON paged_json = new PagedJSON(_input_dir,_output_dir,_verbosity);
|
---|
45 | JavaRDD<String> json_ids = json_list_data.flatMap(paged_json).cache();
|
---|
46 |
|
---|
47 | json_ids.saveAsTextFile("foo");
|
---|
48 |
|
---|
49 | long num_ids = json_ids.count();
|
---|
50 | System.out.println("");
|
---|
51 | System.out.println("############");
|
---|
52 | System.out.println("# Number of page ids: " + num_ids);
|
---|
53 | System.out.println("############");
|
---|
54 | System.out.println("");
|
---|
55 |
|
---|
56 | jsc.close();
|
---|
57 | }
|
---|
58 |
|
---|
59 | public static void main(String[] args) {
|
---|
60 |
|
---|
61 |
|
---|
62 | Options options = new Options();
|
---|
63 |
|
---|
64 | //.withType(Integer.class)
|
---|
65 | /*
|
---|
66 | options.addOption(OptionBuilder.withLongOpt("verbosity")
|
---|
67 | .withDescription("Set to control the level of debugging output [0=none, 1=some, 2=lots]")
|
---|
68 | .hasArg()
|
---|
69 | .withArgName("v")
|
---|
70 | .isRequired(false)
|
---|
71 | .create());
|
---|
72 | */
|
---|
73 | //Option num_cores_opt = new Option("n", "num-cores", true, "Number of cores to use");
|
---|
74 | //num_cores_opt.setRequired(false);
|
---|
75 | //options.addOption(num_cores_opt);
|
---|
76 |
|
---|
77 | Option verbosity_opt = new Option("v", "verbosity", true,
|
---|
78 | "Set to control the level of debugging output [0=none, 1=some, 2=lots]");
|
---|
79 | verbosity_opt.setRequired(false);
|
---|
80 | options.addOption(verbosity_opt);
|
---|
81 |
|
---|
82 | //CommandLineParser parser = new DefaultParser(); // 1.3 and above
|
---|
83 |
|
---|
84 | // need to work with CLI v1.2 as this is the JAR that is bundled with Hadoop/Spark
|
---|
85 | CommandLineParser parser = new GnuParser();
|
---|
86 | HelpFormatter formatter = new HelpFormatter();
|
---|
87 | CommandLine cmd;
|
---|
88 |
|
---|
89 | try {
|
---|
90 | cmd = parser.parse(options, args);
|
---|
91 | }
|
---|
92 | catch (ParseException e) {
|
---|
93 | System.err.println(e.getMessage());
|
---|
94 | formatter.printHelp("RUN.bash [options] json-file-list.txt input-dir output-dir", options);
|
---|
95 | System.exit(1);
|
---|
96 | return;
|
---|
97 | }
|
---|
98 |
|
---|
99 | //value = ((Integer)cmdLine.getParsedOptionValue("num-cores")).intValue();
|
---|
100 | //value = ((Integer)cmdLine.getOptionValue("num-cores","2")).intValue();
|
---|
101 |
|
---|
102 | //cmd.hasOption("json-filelist")
|
---|
103 |
|
---|
104 | String verbosity_str = cmd.getOptionValue("verbosity","0");
|
---|
105 | int verbosity = Integer.parseInt(verbosity_str);
|
---|
106 |
|
---|
107 | String[] filtered_args = cmd.getArgs();
|
---|
108 |
|
---|
109 | if (filtered_args.length != 3) {
|
---|
110 | formatter.printHelp("RUN.bash [options] json-filelist.txt input-dir output-dir", options);
|
---|
111 | System.exit(1);
|
---|
112 | }
|
---|
113 | String json_list_filename = filtered_args[0];
|
---|
114 | String input_dir = filtered_args[1];
|
---|
115 | String output_dir = filtered_args[2];
|
---|
116 |
|
---|
117 | PrepareForIngest prep_for_ingest = new PrepareForIngest(input_dir,json_list_filename,output_dir,verbosity);
|
---|
118 | prep_for_ingest.exec();
|
---|
119 |
|
---|
120 | }
|
---|
121 | }
|
---|