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.util.DoubleAccumulator;
|
---|
8 | import org.apache.spark.SparkConf;
|
---|
9 |
|
---|
10 | public class PrepareForIngest implements Serializable
|
---|
11 | {
|
---|
12 | private static final long serialVersionUID = 1L;
|
---|
13 |
|
---|
14 | public static final int NUM_PARTITIONS = 6; // default would appear to be 2
|
---|
15 |
|
---|
16 | protected String _input_dir;
|
---|
17 | protected String _json_list_filename;
|
---|
18 | protected String _solr_url;
|
---|
19 | protected String _output_dir;
|
---|
20 |
|
---|
21 | protected int _verbosity;
|
---|
22 |
|
---|
23 | public PrepareForIngest(String input_dir, String json_list_filename,
|
---|
24 | String solr_url, String output_dir, int verbosity)
|
---|
25 | {
|
---|
26 | _input_dir = input_dir;
|
---|
27 | _json_list_filename = (json_list_filename != null) ? json_list_filename : input_dir;
|
---|
28 |
|
---|
29 | _solr_url = solr_url;
|
---|
30 | _output_dir = output_dir;
|
---|
31 | _verbosity = verbosity;
|
---|
32 | }
|
---|
33 |
|
---|
34 | public void exec()
|
---|
35 | {
|
---|
36 | String spark_app_name = "HathiTrust Extract Features: Prepare for Solr Ingest";
|
---|
37 | spark_app_name += " [" + _json_list_filename + "]";
|
---|
38 |
|
---|
39 | SparkConf conf = new SparkConf().setAppName(spark_app_name);
|
---|
40 | JavaSparkContext jsc = new JavaSparkContext(conf);
|
---|
41 |
|
---|
42 | if (_verbosity >= 2) {
|
---|
43 | System.out.println("Default Minimum Partions: " + jsc.defaultMinPartitions());
|
---|
44 | System.out.println("Default Parallelism: " + jsc.defaultParallelism());
|
---|
45 | }
|
---|
46 |
|
---|
47 | JavaRDD<String> json_list_data = jsc.textFile(_json_list_filename,NUM_PARTITIONS).cache();
|
---|
48 |
|
---|
49 | long num_volumes = json_list_data.count();
|
---|
50 | double per_vol = 100.0/(double)num_volumes;
|
---|
51 |
|
---|
52 | DoubleAccumulator progress_accum = jsc.sc().doubleAccumulator("ProgressPercent");
|
---|
53 |
|
---|
54 | PagedJSON paged_json = new PagedJSON(_input_dir,_solr_url,_output_dir,_verbosity, progress_accum,per_vol);
|
---|
55 | //JavaRDD<String> json_ids = json_list_data.flatMap(paged_json).cache();
|
---|
56 |
|
---|
57 | json_list_data.foreach(paged_json);
|
---|
58 |
|
---|
59 |
|
---|
60 | //long num_ids = json_ids.count();
|
---|
61 | long num_ids = num_volumes;
|
---|
62 |
|
---|
63 | System.out.println("");
|
---|
64 | System.out.println("############");
|
---|
65 | System.out.println("# Number of page ids: " + num_ids);
|
---|
66 | System.out.println("############");
|
---|
67 | System.out.println("");
|
---|
68 |
|
---|
69 | /*
|
---|
70 | if (_output_dir != null) {
|
---|
71 | String rdd_save_file = "rdd-solr-json-page-files";
|
---|
72 | json_ids.saveAsTextFile(rdd_save_file);
|
---|
73 | System.out.println("############");
|
---|
74 | System.out.println("# Saved RDD of Solr JSON page files, top-level, as:");
|
---|
75 | System.out.println("# " + rdd_save_file);
|
---|
76 | System.out.println("############");
|
---|
77 | System.out.println("");
|
---|
78 | }
|
---|
79 | */
|
---|
80 |
|
---|
81 | jsc.close();
|
---|
82 | }
|
---|
83 |
|
---|
84 | public static void print_usage(HelpFormatter formatter, Options options)
|
---|
85 | {
|
---|
86 | formatter.printHelp("RUN.bash [options] input-dir json-filelist.txt", options);
|
---|
87 | }
|
---|
88 | public static void main(String[] args) {
|
---|
89 |
|
---|
90 |
|
---|
91 | Options options = new Options();
|
---|
92 |
|
---|
93 | Option verbosity_opt = new Option("v", "verbosity", true,
|
---|
94 | "Set to control the level of debugging output [0=none, 1=some, 2=lots]");
|
---|
95 | verbosity_opt.setRequired(false);
|
---|
96 | options.addOption(verbosity_opt);
|
---|
97 |
|
---|
98 | Option output_dir_opt = new Option("o", "output-dir", true,
|
---|
99 | "If specified, save BZipped Solr JSON files to this directory");
|
---|
100 | output_dir_opt.setRequired(false);
|
---|
101 | options.addOption(output_dir_opt);
|
---|
102 |
|
---|
103 | Option solr_url_opt = new Option("u", "solr-url", true,
|
---|
104 | "If specified, the URL to post the Solr JSON data to");
|
---|
105 | solr_url_opt.setRequired(false);
|
---|
106 | options.addOption(solr_url_opt);
|
---|
107 |
|
---|
108 | Option dry_run_opt = new Option("r", "dry-run", false,
|
---|
109 | "Used to initiate a 'dry-run' where the files are all read in, but nothing is ingested/saved");
|
---|
110 | dry_run_opt.setRequired(false);
|
---|
111 | options.addOption(dry_run_opt);
|
---|
112 |
|
---|
113 | // need to work with CLI v1.2 as this is the JAR that is bundled with Hadoop/Spark
|
---|
114 | CommandLineParser parser = new GnuParser();
|
---|
115 | //CommandLineParser parser = new DefaultParser(); // if working with CLI v1.3 and above
|
---|
116 |
|
---|
117 | HelpFormatter formatter = new HelpFormatter();
|
---|
118 | CommandLine cmd = null;
|
---|
119 |
|
---|
120 | try {
|
---|
121 | cmd = parser.parse(options, args);
|
---|
122 | }
|
---|
123 | catch (ParseException e) {
|
---|
124 | System.err.println(e.getMessage());
|
---|
125 | print_usage(formatter,options);
|
---|
126 | System.exit(1);
|
---|
127 | }
|
---|
128 |
|
---|
129 |
|
---|
130 | String verbosity_str = cmd.getOptionValue("verbosity","0");
|
---|
131 | int verbosity = Integer.parseInt(verbosity_str);
|
---|
132 |
|
---|
133 | String output_dir = cmd.getOptionValue("output-dir",null);
|
---|
134 | String solr_url = cmd.getOptionValue("solr-url",null);
|
---|
135 | boolean dry_run = cmd.hasOption("dry-run");
|
---|
136 |
|
---|
137 | String[] filtered_args = cmd.getArgs();
|
---|
138 |
|
---|
139 | if (filtered_args.length != 2) {
|
---|
140 | print_usage(formatter,options);
|
---|
141 | System.exit(1);
|
---|
142 | }
|
---|
143 |
|
---|
144 | if (!dry_run && ((output_dir == null) && (solr_url==null))) {
|
---|
145 | System.err.println("Need to specify either --solr-url or --output-dir otherwise generated files are not ingested/saved");
|
---|
146 | print_usage(formatter,options);
|
---|
147 | System.exit(1);
|
---|
148 | }
|
---|
149 |
|
---|
150 | String input_dir = filtered_args[0];
|
---|
151 | String json_list_filename = filtered_args[1];
|
---|
152 |
|
---|
153 | PrepareForIngest prep_for_ingest
|
---|
154 | = new PrepareForIngest(input_dir,json_list_filename,solr_url,output_dir,verbosity);
|
---|
155 | prep_for_ingest.exec();
|
---|
156 |
|
---|
157 | }
|
---|
158 | }
|
---|