source: other-projects/hathitrust/wcsa/extracted-features-solr/trunk/solr-ingest/src/main/java/org/hathitrust/extractedfeatures/ProcessForSolrIngest.java@ 31201

Last change on this file since 31201 was 31201, checked in by davidb, 7 years ago

Trigger serialization of whitelist in main program

  • Property svn:executable set to *
File size: 10.9 KB
Line 
1package org.hathitrust.extractedfeatures;
2
3import java.io.BufferedInputStream;
4import java.io.FileInputStream;
5import java.io.FileNotFoundException;
6import java.io.IOException;
7import java.io.Serializable;
8import java.util.ArrayList;
9
10import org.apache.commons.cli.*;
11
12import org.apache.spark.api.java.*;
13import org.apache.spark.util.DoubleAccumulator;
14import org.apache.spark.util.LongAccumulator;
15import org.hathitrust.extractedfeatures.PerPageJSONFlatmap;
16import org.json.JSONObject;
17import org.apache.spark.SparkConf;
18
19public class ProcessForSolrIngest implements Serializable
20{
21 private static final long serialVersionUID = 1L;
22
23 // Following details on number of partitions to use given in
24 // "Parallelized collections" section of:
25 // https://spark.apache.org/docs/2.0.1/programming-guide.html
26 //
27 // For a more detailed discussion see:
28 // http://blog.cloudera.com/blog/2015/03/how-to-tune-your-apache-spark-jobs-part-2/
29
30 protected static final int DEFAULT_NUM_CORES = 6;
31 protected static final int DEFAULT_NUM_PARTITIONS = 3*DEFAULT_NUM_CORES;
32
33 protected String _input_dir;
34 protected String _json_list_filename;
35 protected String _solr_url;
36 protected String _output_dir;
37
38 protected int _verbosity;
39
40 public ProcessForSolrIngest(String input_dir, String json_list_filename,
41 String solr_url, String output_dir, int verbosity)
42 {
43 _input_dir = input_dir;
44 _json_list_filename = (json_list_filename != null) ? json_list_filename : input_dir;
45
46 _solr_url = solr_url;
47 _output_dir = output_dir;
48 _verbosity = verbosity;
49 }
50
51 protected String generateSparkAppName(String exec_mode)
52 {
53 String spark_app_name = "[" + exec_mode + "] Extract Features: Process for Solr Ingest";
54 spark_app_name += " [" + _json_list_filename + "]";
55
56 if (_solr_url != null) {
57 spark_app_name += " solr_url=" + _solr_url;
58 }
59
60 if (_output_dir != null) {
61 spark_app_name += " output_dir=" + _output_dir;
62 }
63
64 return spark_app_name;
65 }
66
67 public ArrayList<String> extrapolateSolrEndpoints()
68 {
69 ArrayList<String> solr_endpoints = new ArrayList<String>();
70
71 if (_solr_url != null) {
72 String solr_cloud_nodes = System.getProperty("wcsa-ef-ingest.solr-cloud-nodes",null);
73 if (solr_cloud_nodes != null) {
74 String [] cloud_nodes = solr_cloud_nodes.split(",");
75 for (String cn : cloud_nodes) {
76 String solr_endpoint = _solr_url.replaceFirst("//.*?:\\d+/", "//"+cn+"/");
77 solr_endpoints.add(solr_endpoint);
78 }
79 }
80 else {
81 solr_endpoints.add(_solr_url);
82 }
83 }
84
85 return solr_endpoints;
86 }
87
88 public void execPerVolume()
89 {
90 String spark_app_name = generateSparkAppName("Per Volume");
91
92 SparkConf conf = new SparkConf().setAppName(spark_app_name);
93 JavaSparkContext jsc = new JavaSparkContext(conf);
94
95 if (_verbosity >= 2) {
96 System.out.println("Default Minimum Partions: " + jsc.defaultMinPartitions());
97 System.out.println("Default Parallelism: " + jsc.defaultParallelism());
98 }
99
100 int num_partitions = Integer.getInteger("wcsa-ef-ingest.num-partitions", DEFAULT_NUM_PARTITIONS);
101
102 JavaRDD<String> json_list_data = jsc.textFile(_json_list_filename,num_partitions).cache();
103
104 long num_volumes = json_list_data.count();
105 double per_vol = 100.0/(double)num_volumes;
106
107 //JavaRDD<String> json_list_data_rp = json_list_data.repartition((int)(num_volumes/100));
108
109 DoubleAccumulator progress_accum = jsc.sc().doubleAccumulator("Progress Percent");
110
111 System.err.println();
112 System.err.println();
113 System.err.println();
114 System.err.println("****##### _input_dir = " + _input_dir);
115 System.err.println();
116 System.err.println();
117 System.err.println();
118
119 PerVolumeJSON per_vol_json = new PerVolumeJSON(_input_dir,_solr_url,_output_dir,_verbosity, progress_accum,per_vol);
120
121 json_list_data.foreach(per_vol_json);
122
123 long num_ids = num_volumes;
124
125 System.out.println("");
126 System.out.println("############");
127 System.out.println("# Number of volume ids: " + num_ids);
128 System.out.println("############");
129 System.out.println("");
130
131 jsc.close();
132 }
133
134
135
136 public void execPerPage()
137 {
138 String spark_app_name = generateSparkAppName("Per Page");
139
140 SparkConf conf = new SparkConf().setAppName(spark_app_name);
141 JavaSparkContext jsc = new JavaSparkContext(conf);
142
143 if (_verbosity >= 2) {
144 System.out.println("Default Minimum Partions: " + jsc.defaultMinPartitions());
145 System.out.println("Default Parallelism: " + jsc.defaultParallelism());
146 }
147
148 int num_partitions = Integer.getInteger("wcsa-ef-ingest.num-partitions", DEFAULT_NUM_PARTITIONS);
149 JavaRDD<String> json_list_data = jsc.textFile(_json_list_filename,num_partitions).cache();
150
151 long num_volumes = json_list_data.count();
152 double per_vol = 100.0/(double)num_volumes;
153
154 //JavaRDD<String> json_list_data_rp = json_list_data.repartition((int)(num_volumes/100));
155
156 DoubleAccumulator per_vol_progress_accum = jsc.sc().doubleAccumulator("Per Volume Progress Percent");
157
158 //String strict_file_io_str = System.getProperty("wcsa-ef-ingest.strict-file-io","true");
159 boolean strict_file_io = Boolean.getBoolean("wcsa-ef-ingest.strict-file-io");
160
161 PerPageJSONFlatmap paged_solr_json_flatmap
162 = new PerPageJSONFlatmap(_input_dir,_solr_url,_output_dir,_verbosity,
163 per_vol_progress_accum,per_vol,
164 strict_file_io);
165 JavaRDD<JSONObject> per_page_jsonobjects = json_list_data.flatMap(paged_solr_json_flatmap).cache();
166
167 //long num_page_ids = per_page_jsonobjects.count(); // trigger lazy eval of: flatmap:per-vol
168
169 LongAccumulator per_page_progress_accum = jsc.sc().longAccumulator("Pages Processed");
170 ArrayList<String> solr_endpoints = extrapolateSolrEndpoints();
171
172
173 PerPageJSONMap paged_json_id_map
174 = new PerPageJSONMap(_input_dir,solr_endpoints,_output_dir,_verbosity,
175 per_page_progress_accum,1);
176 JavaRDD<String> per_page_ids = per_page_jsonobjects.map(paged_json_id_map);
177
178/*
179 System.out.println("");
180 System.out.println("############");
181 System.out.println("# Progress Accumulator: " + progress_accum.value());
182 System.out.println("############");
183 System.out.println("");
184*/
185
186 long num_page_ids = per_page_ids.count(); // trigger lazy eval of: flatmap:per-vol -> map:per-page
187
188 System.out.println("");
189 System.out.println("############");
190 System.out.println("# Number of page ids: " + num_page_ids);
191 System.out.println("############");
192 System.out.println("");
193
194 /*
195 if (_output_dir != null) {
196 String rdd_save_file = "rdd-solr-json-page-files";
197 json_ids.saveAsTextFile(rdd_save_file);
198 System.out.println("############");
199 System.out.println("# Saved RDD of Solr JSON page files, top-level, as:");
200 System.out.println("# " + rdd_save_file);
201 System.out.println("############");
202 System.out.println("");
203 }
204 */
205
206 jsc.close();
207 }
208
209
210
211
212 public static void print_usage(HelpFormatter formatter, Options options)
213 {
214 formatter.printHelp("RUN.bash [options] input-dir json-filelist.txt", options);
215 }
216
217 public static void main(String[] args) {
218 Options options = new Options();
219
220 Option verbosity_opt = new Option("v", "verbosity", true,
221 "Set to control the level of debugging output [0=none, 1=some, 2=lots]");
222 verbosity_opt.setRequired(false);
223 options.addOption(verbosity_opt);
224
225 Option properties_opt = new Option("p", "properties", true,
226 "Read in the specified Java properties file");
227 properties_opt.setRequired(false);
228 options.addOption(properties_opt);
229
230 Option output_dir_opt = new Option("o", "output-dir", true,
231 "If specified, save BZipped Solr JSON files to this directory");
232 output_dir_opt.setRequired(false);
233 options.addOption(output_dir_opt);
234
235 Option solr_url_opt = new Option("u", "solr-url", true,
236 "If specified, the URL to post the Solr JSON data to");
237 solr_url_opt.setRequired(false);
238 options.addOption(solr_url_opt);
239
240 Option read_only_opt = new Option("r", "read-only", false,
241 "Used to initiate a run where the files are all read in, but nothing is ingested/saved");
242 read_only_opt.setRequired(false);
243 options.addOption(read_only_opt);
244
245 // Need to work with CLI v1.2 as this is the JAR that is bundled with Hadoop/Spark
246 CommandLineParser parser = new GnuParser();
247 //CommandLineParser parser = new DefaultParser(); // if working with CLI v1.3 and above
248
249 HelpFormatter formatter = new HelpFormatter();
250 CommandLine cmd = null;
251
252 try {
253 cmd = parser.parse(options, args);
254 }
255 catch (ParseException e) {
256 System.err.println(e.getMessage());
257 print_usage(formatter,options);
258 System.exit(1);
259 }
260
261
262 String verbosity_str = cmd.getOptionValue("verbosity","1");
263 int verbosity = Integer.parseInt(verbosity_str);
264
265 String property_filename = cmd.getOptionValue("properties",null);
266
267 String output_dir = cmd.getOptionValue("output-dir",null);
268 String solr_url = cmd.getOptionValue("solr-url",null);
269 boolean read_only = cmd.hasOption("read-only");
270
271 String[] filtered_args = cmd.getArgs();
272
273 if (filtered_args.length != 2) {
274 print_usage(formatter,options);
275 System.exit(1);
276 }
277
278 if (property_filename != null) {
279 try {
280 FileInputStream fis = new FileInputStream(property_filename);
281 BufferedInputStream bis = new BufferedInputStream(fis);
282
283 System.getProperties().load(bis);
284 }
285 catch (FileNotFoundException e) {
286 // TODO Auto-generated catch block
287 e.printStackTrace();
288 System.err.println("File not found: '" + property_filename + "'. Skipping property file read");
289 }
290 catch (IOException e) {
291 System.err.println("IO Exception for: '" + property_filename + "'. Malformed syntax? Skipping property file read");
292 }
293 }
294
295 if (!read_only && ((output_dir == null) && (solr_url==null))) {
296 System.err.println("Need to specify either --solr-url or --output-dir otherwise generated files are not ingested/saved");
297 print_usage(formatter,options);
298 System.exit(1);
299 }
300 if (read_only) {
301 // For this case, need to ensure solr-url and output-dir are null
302 output_dir = null;
303 solr_url = null;
304 }
305
306 String input_dir = filtered_args[0];
307 String json_list_filename = filtered_args[1];
308
309 ProcessForSolrIngest prep_for_ingest
310 = new ProcessForSolrIngest(input_dir,json_list_filename,solr_url,output_dir,verbosity);
311
312
313 boolean use_whitelist = Boolean.getBoolean("wcsa-ef-ingest.use-whitelist");
314
315 if (use_whitelist) {
316 String whitelist_filename = System.getProperty("wcsa-ef-ingest.whitelist-filename");
317
318 WhitelistBloomFilter whitelist_bloomfilter = new WhitelistBloomFilter(whitelist_filename,true);
319 whitelist_bloomfilter.contains("foo");
320 }
321
322
323 String process_ef_json_mode = System.getProperty("wcsa-ef-ingest.process-ef-json-mode","per-page");
324 if (process_ef_json_mode.equals("per-volume")) {
325 prep_for_ingest.execPerVolume();
326 }
327 else {
328 prep_for_ingest.execPerPage();
329 }
330 }
331}
Note: See TracBrowser for help on using the repository browser.