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

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

shift to using solr-base-url and a specified solr-collection

  • Property svn:executable set to *
File size: 12.8 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.*;
11import org.apache.hadoop.io.Text;
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 protected static final int DEFAULT_NUM_CORES = 10;
24 protected static final int MINIMUM_NUM_PARTITIONS = 10*DEFAULT_NUM_CORES;
25
26 protected static final int DEFAULT_FILES_PER_PARTITION = 3000;
27
28 protected String _input_dir;
29 protected String _solr_base_url;
30 protected String _solr_collection;
31
32 protected String _whitelist_filename;
33 protected String _langmap_directory;
34
35 //protected String _solr_url;
36 protected String _output_dir;
37
38 protected int _verbosity;
39
40 public ProcessForSolrIngest(String input_dir, String solr_collection,
41 String solr_base_url, String output_dir, int verbosity)
42 {
43 _input_dir = input_dir;
44 _solr_collection = solr_collection;
45
46 boolean use_whitelist = Boolean.getBoolean("wcsa-ef-ingest.use-whitelist");
47 _whitelist_filename = (use_whitelist) ? System.getProperty("wcsa-ef-ingest.whitelist-filename") : null;
48
49 boolean use_langmap = Boolean.getBoolean("wcsa-ef-ingest.use-langmap");
50 _langmap_directory = (use_langmap) ? System.getProperty("wcsa-ef-ingest.langmap-directory") : null;
51
52
53 _solr_base_url = solr_base_url;
54 _output_dir = output_dir;
55 _verbosity = verbosity;
56 }
57
58 protected String generateSparkAppName(String exec_mode)
59 {
60 String spark_app_name = "[" + exec_mode + "] Extract Features: Process for Solr Ingest";
61 spark_app_name += " [" + _solr_collection + "]";
62
63 if (_solr_base_url != null) {
64 spark_app_name += " solr_base_url=" + _solr_base_url;
65 }
66
67 if (_output_dir != null) {
68 spark_app_name += " output_dir=" + _output_dir;
69 }
70
71 return spark_app_name;
72 }
73
74 public ArrayList<String> extrapolateSolrEndpoints(String solr_collection)
75 {
76 ArrayList<String> solr_endpoints = new ArrayList<String>();
77
78 if (_solr_base_url != null) {
79 String solr_url = _solr_base_url + "/" + solr_collection + "/update";
80
81 String solr_cloud_nodes = System.getProperty("wcsa-ef-ingest.solr-cloud-nodes",null);
82 if (solr_cloud_nodes != null) {
83 String [] cloud_nodes = solr_cloud_nodes.split(",");
84 for (String cn : cloud_nodes) {
85 String solr_endpoint = solr_url.replaceFirst("//.*?:\\d+/", "//"+cn+"/");
86 solr_endpoints.add(solr_endpoint);
87 }
88 }
89 else {
90 solr_endpoints.add(solr_url);
91 }
92 }
93
94 return solr_endpoints;
95 }
96
97 public void execPerVolumeSequenceFile()
98 {
99 String spark_app_name = generateSparkAppName("Per Volume");
100
101 SparkConf conf = new SparkConf().setAppName(spark_app_name);
102 JavaSparkContext jsc = new JavaSparkContext(conf);
103 jsc.hadoopConfiguration().set("io.compression.codec.bzip2.library", "java-builtin");
104
105 //String packed_sequence_path = "hdfs:///user/capitanu/data/packed-ef";
106 String packed_sequence_path = _input_dir;
107
108 JavaPairRDD<Text, Text> input_pair_rdd = jsc.sequenceFile(packed_sequence_path, Text.class, Text.class);
109
110 JavaRDD<Text> json_text_rdd = input_pair_rdd.map(item -> item._2);
111
112 boolean icu_tokenize = Boolean.getBoolean("wcsa-ef-ingest.icu-tokenize");
113 boolean strict_file_io = Boolean.getBoolean("wcsa-ef-ingest.strict-file-io");
114
115 ArrayList<String> solr_endpoints = extrapolateSolrEndpoints(_solr_collection);
116
117 System.out.println("*** away to create PerVolumeJSON class, _langmap_directory = " + _langmap_directory);
118 PerVolumeJSON per_vol_json = new PerVolumeJSON(_input_dir,_whitelist_filename, _langmap_directory,
119 solr_endpoints,_output_dir,_verbosity,
120 icu_tokenize,strict_file_io);
121
122
123 JavaRDD<Integer> per_volume_page_count = json_text_rdd.map(per_vol_json);
124
125 Integer num_page_ids = per_volume_page_count.reduce((a, b) -> a + b);
126
127 System.out.println("");
128 System.out.println("############");
129 System.out.println("# Number of page ids: " + num_page_ids);
130 System.out.println("############");
131 System.out.println("");
132
133 jsc.close();
134
135 }
136
137 /*
138 public void execPerVolume()
139 {
140 String spark_app_name = generateSparkAppName("Per Volume");
141
142 SparkConf conf = new SparkConf().setAppName(spark_app_name);
143 JavaSparkContext jsc = new JavaSparkContext(conf);
144
145 //int num_partitions = Integer.getInteger("wcsa-ef-ingest.num-partitions", DEFAULT_NUM_PARTITIONS);
146 int files_per_partition = Integer.getInteger("wcsa-ef-ingest.files-per-partition", DEFAULT_FILES_PER_PARTITION);
147
148 JavaRDD<String> json_list_data = jsc.textFile(_json_list_filename).cache();
149
150 long num_volumes = json_list_data.count();
151 double per_vol = 100.0/(double)num_volumes;
152
153 int num_partitions = (int)(num_volumes/files_per_partition)+1;
154 if (num_partitions < MINIMUM_NUM_PARTITIONS) {
155 num_partitions = MINIMUM_NUM_PARTITIONS;
156 }
157 JavaRDD<String> json_list_data_rp = json_list_data.repartition(num_partitions);
158
159 DoubleAccumulator progress_accum = jsc.sc().doubleAccumulator("Progress Percent");
160
161 boolean icu_tokenize = Boolean.getBoolean("wcsa-ef-ingest.icu-tokenize");
162 boolean strict_file_io = Boolean.getBoolean("wcsa-ef-ingest.strict-file-io");
163
164 PerVolumeJSON per_vol_json = new PerVolumeJSON(_input_dir,_whitelist_filename,
165 _solr_url,_output_dir,_verbosity, progress_accum,per_vol,
166 icu_tokenize,strict_file_io);
167
168 //json_list_data_rp.foreach(per_vol_json);
169 JavaRDD<String> per_page_ids = json_list_data_rp.flatMap(per_vol_json);
170 long num_page_ids = per_page_ids.count(); // trigger lazy eval of: flatmap:per-vol
171
172 //long num_ids = num_volumes;
173
174 System.out.println("");
175 System.out.println("############");
176 System.out.println("# Number of page ids: " + num_page_ids);
177 System.out.println("############");
178 System.out.println("");
179
180 jsc.close();
181 }
182 */
183
184 /*
185 public void execPerPage()
186 {
187 String spark_app_name = generateSparkAppName("Per Page");
188
189 SparkConf conf = new SparkConf().setAppName(spark_app_name);
190 JavaSparkContext jsc = new JavaSparkContext(conf);
191
192
193
194 //int num_partitions = Integer.getInteger("wcsa-ef-ingest.num-partitions", DEFAULT_NUM_PARTITIONS);
195 int files_per_partition = Integer.getInteger("wcsa-ef-ingest.num-partitions", DEFAULT_FILES_PER_PARTITION);
196
197 JavaRDD<String> json_list_data = jsc.textFile(_json_list_filename).cache();
198
199 long num_volumes = json_list_data.count();
200 double per_vol = 100.0/(double)num_volumes;
201
202 int num_partitions = (int)(num_volumes/files_per_partition)+1;
203 JavaRDD<String> json_list_data_rp = json_list_data.repartition(num_partitions);
204
205 DoubleAccumulator per_vol_progress_accum = jsc.sc().doubleAccumulator("Per Volume Progress Percent");
206
207 boolean icu_tokenize = Boolean.getBoolean("wcsa-ef-ingest.icu-tokenize");
208 boolean strict_file_io = Boolean.getBoolean("wcsa-ef-ingest.strict-file-io");
209
210 PerPageJSONFlatmap paged_solr_json_flatmap
211 = new PerPageJSONFlatmap(_input_dir,_whitelist_filename,
212 _solr_url,_output_dir,_verbosity,
213 per_vol_progress_accum,per_vol,
214 icu_tokenize,strict_file_io);
215 //JavaRDD<JSONObject> per_page_jsonobjects = json_list_data_rp.flatMap(paged_solr_json_flatmap).cache();
216 JavaRDD<JSONObject> per_page_jsonobjects = json_list_data_rp.flatMap(paged_solr_json_flatmap);
217
218 //long num_page_ids = per_page_jsonobjects.count(); // trigger lazy eval of: flatmap:per-vol
219
220 LongAccumulator per_page_progress_accum = jsc.sc().longAccumulator("Pages Processed");
221 ArrayList<String> solr_endpoints = extrapolateSolrEndpoints();
222
223
224 PerPageJSONMap paged_json_id_map
225 = new PerPageJSONMap(_input_dir,solr_endpoints,_output_dir,_verbosity,
226 per_page_progress_accum,1);
227 JavaRDD<String> per_page_ids = per_page_jsonobjects.map(paged_json_id_map);
228
229
230 long num_page_ids = per_page_ids.count(); // trigger lazy eval of: flatmap:per-vol -> map:per-page
231
232 System.out.println("");
233 System.out.println("############");
234 System.out.println("# Number of page ids: " + num_page_ids);
235 System.out.println("############");
236 System.out.println("");
237
238
239 //if (_output_dir != null) {
240 //String rdd_save_file = "rdd-solr-json-page-files";
241 //json_ids.saveAsTextFile(rdd_save_file);
242 //System.out.println("############");
243 //System.out.println("# Saved RDD of Solr JSON page files, top-level, as:");
244 //System.out.println("# " + rdd_save_file);
245 //System.out.println("############");
246 //System.out.println("");
247 //}
248
249
250 jsc.close();
251 }
252*/
253
254
255
256 public static void print_usage(HelpFormatter formatter, Options options)
257 {
258 formatter.printHelp("RUN.bash [options] input-dir solr-collection", options);
259 }
260
261 public static void main(String[] args) {
262 Options options = new Options();
263
264 Option verbosity_opt = new Option("v", "verbosity", true,
265 "Set to control the level of debugging output [0=none, 1=some, 2=lots]");
266 verbosity_opt.setRequired(false);
267 options.addOption(verbosity_opt);
268
269 Option properties_opt = new Option("p", "properties", true,
270 "Read in the specified Java properties file");
271 properties_opt.setRequired(false);
272 options.addOption(properties_opt);
273
274 Option output_dir_opt = new Option("o", "output-dir", true,
275 "If specified, save BZipped Solr JSON files to this directory");
276 output_dir_opt.setRequired(false);
277 options.addOption(output_dir_opt);
278
279 Option solr_base_url_opt = new Option("u", "solr-base-url", true,
280 "If specified, the base URL to post the Solr JSON data to");
281 solr_base_url_opt.setRequired(false);
282 options.addOption(solr_base_url_opt);
283
284 Option read_only_opt = new Option("r", "read-only", false,
285 "Used to initiate a run where the files are all read in, but nothing is ingested/saved");
286 read_only_opt.setRequired(false);
287 options.addOption(read_only_opt);
288
289 // Need to work with CLI v1.2 as this is the JAR that is bundled with Hadoop/Spark
290 CommandLineParser parser = new GnuParser();
291 //CommandLineParser parser = new DefaultParser(); // if working with CLI v1.3 and above
292
293 HelpFormatter formatter = new HelpFormatter();
294 CommandLine cmd = null;
295
296 try {
297 cmd = parser.parse(options, args);
298 }
299 catch (ParseException e) {
300 System.err.println(e.getMessage());
301 print_usage(formatter,options);
302 System.exit(1);
303 }
304
305
306 String verbosity_str = cmd.getOptionValue("verbosity","1");
307 int verbosity = Integer.parseInt(verbosity_str);
308
309 String property_filename = cmd.getOptionValue("properties",null);
310
311 String output_dir = cmd.getOptionValue("output-dir",null);
312 String solr_base_url = cmd.getOptionValue("solr-base-url",null);
313 boolean read_only = cmd.hasOption("read-only");
314
315 String[] filtered_args = cmd.getArgs();
316
317 if (filtered_args.length != 2) {
318 print_usage(formatter,options);
319 System.exit(1);
320 }
321
322 if (property_filename != null) {
323 try {
324 FileInputStream fis = new FileInputStream(property_filename);
325 BufferedInputStream bis = new BufferedInputStream(fis);
326
327 System.getProperties().load(bis);
328 }
329 catch (FileNotFoundException e) {
330 // TODO Auto-generated catch block
331 e.printStackTrace();
332 System.err.println("File not found: '" + property_filename + "'. Skipping property file read");
333 }
334 catch (IOException e) {
335 System.err.println("IO Exception for: '" + property_filename + "'. Malformed syntax? Skipping property file read");
336 }
337 }
338
339 if (!read_only && ((output_dir == null) && (solr_base_url==null))) {
340 System.err.println("Need to specify either --solr-base-url or --output-dir otherwise generated files are not ingested/saved");
341 print_usage(formatter,options);
342 System.exit(1);
343 }
344 if (read_only) {
345 // For this case, need to ensure solr-url and output-dir are null
346 output_dir = null;
347 solr_base_url = null;
348 }
349
350 String input_dir = filtered_args[0];
351 String solr_collection = filtered_args[1];
352
353 ProcessForSolrIngest prep_for_ingest
354 = new ProcessForSolrIngest(input_dir,solr_collection,solr_base_url,output_dir,verbosity);
355
356 prep_for_ingest.execPerVolumeSequenceFile();
357
358 /*
359 String process_ef_json_mode = System.getProperty("wcsa-ef-ingest.process-ef-json-mode","per-page");
360 if (process_ef_json_mode.equals("per-volume")) {
361 prep_for_ingest.execPerVolume();
362 }
363 else {
364 prep_for_ingest.execPerPage();
365 }*/
366 }
367}
Note: See TracBrowser for help on using the repository browser.