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

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

Class name refactoring

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