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

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

Code to work per-volume and per-page

  • Property svn:executable set to *
File size: 7.0 KB
Line 
1package org.hathitrust.extractedfeatures;
2
3import java.io.Serializable;
4import org.apache.commons.cli.*;
5
6import org.apache.spark.api.java.*;
7import org.apache.spark.util.DoubleAccumulator;
8import org.hathitrust.extractedfeatures.PagedJSON;
9import org.json.JSONObject;
10import org.apache.spark.SparkConf;
11
12public class ProcessForSolrIngest implements Serializable
13{
14 private static final long serialVersionUID = 1L;
15
16 // Following details on number of partitions to use given in
17 // "Parallelized collections" section of:
18 // https://spark.apache.org/docs/2.0.1/programming-guide.html
19 //
20 // For a more detailed discussion see:
21 // http://blog.cloudera.com/blog/2015/03/how-to-tune-your-apache-spark-jobs-part-2/
22
23 public static final int NUM_CORES = 6;
24 public static final int NUM_PARTITIONS = 2*NUM_CORES; // default would appear to be 2
25
26 protected String _input_dir;
27 protected String _json_list_filename;
28 protected String _solr_url;
29 protected String _output_dir;
30
31 protected int _verbosity;
32
33 public ProcessForSolrIngest(String input_dir, String json_list_filename,
34 String solr_url, String output_dir, int verbosity)
35 {
36 _input_dir = input_dir;
37 _json_list_filename = (json_list_filename != null) ? json_list_filename : input_dir;
38
39 _solr_url = solr_url;
40 _output_dir = output_dir;
41 _verbosity = verbosity;
42 }
43
44 public void execPerVolume()
45 {
46 String spark_app_name = "[Per Volume] Extract Features: Process for Solr Ingest";
47 spark_app_name += " [" + _json_list_filename + "]";
48
49 SparkConf conf = new SparkConf().setAppName(spark_app_name);
50 JavaSparkContext jsc = new JavaSparkContext(conf);
51
52 if (_verbosity >= 2) {
53 System.out.println("Default Minimum Partions: " + jsc.defaultMinPartitions());
54 System.out.println("Default Parallelism: " + jsc.defaultParallelism());
55 }
56
57 JavaRDD<String> json_list_data = jsc.textFile(_json_list_filename,NUM_PARTITIONS).cache();
58
59 long num_volumes = json_list_data.count();
60 double per_vol = 100.0/(double)num_volumes;
61
62 DoubleAccumulator progress_accum = jsc.sc().doubleAccumulator("Progress Percent");
63
64 PerVolumeJSON per_vol_json = new PerVolumeJSON(_input_dir,_solr_url,_output_dir,_verbosity, progress_accum,per_vol);
65
66 json_list_data.foreach(per_vol_json);
67
68 long num_ids = num_volumes;
69
70 System.out.println("");
71 System.out.println("############");
72 System.out.println("# Number of volume ids: " + num_ids);
73 System.out.println("############");
74 System.out.println("");
75
76 jsc.close();
77 }
78
79 public void execPerPage()
80 {
81 String spark_app_name = "[Per Page] Extract Features: Process for Solr Ingest";
82 spark_app_name += " [" + _json_list_filename + "]";
83
84 SparkConf conf = new SparkConf().setAppName(spark_app_name);
85 JavaSparkContext jsc = new JavaSparkContext(conf);
86
87 if (_verbosity >= 2) {
88 System.out.println("Default Minimum Partions: " + jsc.defaultMinPartitions());
89 System.out.println("Default Parallelism: " + jsc.defaultParallelism());
90 }
91
92 JavaRDD<String> json_list_data = jsc.textFile(_json_list_filename,NUM_PARTITIONS).cache();
93
94 long num_volumes = json_list_data.count();
95 double per_vol = 100.0/(double)num_volumes;
96
97 DoubleAccumulator progress_accum = jsc.sc().doubleAccumulator("Progress Percent");
98
99 PagedJSON paged_json = new PagedJSON(_input_dir,_solr_url,_output_dir,_verbosity, progress_accum,per_vol);
100 JavaRDD<JSONObject> json_per_page_ids = json_list_data.flatMap(paged_json).cache();
101
102 json_per_page_ids.foreach(paged_json);
103
104/*
105 System.out.println("");
106 System.out.println("############");
107 System.out.println("# Progress Accumulator: " + progress_accum.value());
108 System.out.println("############");
109 System.out.println("");
110*/
111
112 long num_page_ids = json_per_page_ids.count();
113
114 System.out.println("");
115 System.out.println("############");
116 System.out.println("# Number of page ids: " + num_page_ids);
117 System.out.println("############");
118 System.out.println("");
119
120 /*
121 if (_output_dir != null) {
122 String rdd_save_file = "rdd-solr-json-page-files";
123 json_ids.saveAsTextFile(rdd_save_file);
124 System.out.println("############");
125 System.out.println("# Saved RDD of Solr JSON page files, top-level, as:");
126 System.out.println("# " + rdd_save_file);
127 System.out.println("############");
128 System.out.println("");
129 }
130 */
131
132 jsc.close();
133 }
134
135
136
137
138 public static void print_usage(HelpFormatter formatter, Options options)
139 {
140 formatter.printHelp("RUN.bash [options] input-dir json-filelist.txt", options);
141 }
142
143 public static void main(String[] args) {
144 Options options = new Options();
145
146 Option verbosity_opt = new Option("v", "verbosity", true,
147 "Set to control the level of debugging output [0=none, 1=some, 2=lots]");
148 verbosity_opt.setRequired(false);
149 options.addOption(verbosity_opt);
150
151 Option output_dir_opt = new Option("o", "output-dir", true,
152 "If specified, save BZipped Solr JSON files to this directory");
153 output_dir_opt.setRequired(false);
154 options.addOption(output_dir_opt);
155
156 Option solr_url_opt = new Option("u", "solr-url", true,
157 "If specified, the URL to post the Solr JSON data to");
158 solr_url_opt.setRequired(false);
159 options.addOption(solr_url_opt);
160
161 Option read_only_opt = new Option("r", "read-only", false,
162 "Used to initiate a run where the files are all read in, but nothing is ingested/saved");
163 read_only_opt.setRequired(false);
164 options.addOption(read_only_opt);
165
166 // Need to work with CLI v1.2 as this is the JAR that is bundled with Hadoop/Spark
167 CommandLineParser parser = new GnuParser();
168 //CommandLineParser parser = new DefaultParser(); // if working with CLI v1.3 and above
169
170 HelpFormatter formatter = new HelpFormatter();
171 CommandLine cmd = null;
172
173 try {
174 cmd = parser.parse(options, args);
175 }
176 catch (ParseException e) {
177 System.err.println(e.getMessage());
178 print_usage(formatter,options);
179 System.exit(1);
180 }
181
182
183 String verbosity_str = cmd.getOptionValue("verbosity","0");
184 int verbosity = Integer.parseInt(verbosity_str);
185
186 String output_dir = cmd.getOptionValue("output-dir",null);
187 String solr_url = cmd.getOptionValue("solr-url",null);
188 boolean read_only = cmd.hasOption("read-only");
189
190 String[] filtered_args = cmd.getArgs();
191
192 if (filtered_args.length != 2) {
193 print_usage(formatter,options);
194 System.exit(1);
195 }
196
197 if (!read_only && ((output_dir == null) && (solr_url==null))) {
198 System.err.println("Need to specify either --solr-url or --output-dir otherwise generated files are not ingested/saved");
199 print_usage(formatter,options);
200 System.exit(1);
201 }
202 if (read_only) {
203 // For this case, need to ensure solr-url and output-dir are null
204 output_dir = null;
205 solr_url = null;
206 }
207
208 String input_dir = filtered_args[0];
209 String json_list_filename = filtered_args[1];
210
211 ProcessForSolrIngest prep_for_ingest
212 = new ProcessForSolrIngest(input_dir,json_list_filename,solr_url,output_dir,verbosity);
213 prep_for_ingest.execPerVolume();
214 }
215}
Note: See TracBrowser for help on using the repository browser.