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

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

Comment out section, useful for controlling a smaller run

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