source: other-projects/hathitrust/solr-extracted-features/trunk/src/main/java/org/hathitrust/PrepareForIngest.java@ 30995

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

Adjustment of NUM_PARTITIONS to be based on Spark recommended calculation

  • Property svn:executable set to *
File size: 5.7 KB
Line 
1package org.hathitrust;
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.apache.spark.SparkConf;
9
10public class PrepareForIngest implements Serializable
11{
12 private static final long serialVersionUID = 1L;
13
14 // Following details on number of partitions to use given in
15 // "Parallelized collections" section of:
16 // https://spark.apache.org/docs/2.0.1/programming-guide.html
17 //
18 // For a more detailed discussion see:
19 // http://blog.cloudera.com/blog/2015/03/how-to-tune-your-apache-spark-jobs-part-2/
20
21 public static final int NUM_CORES = 6;
22 public static final int NUM_PARTITIONS = 2*NUM_CORES; // default would appear to be 2
23
24 protected String _input_dir;
25 protected String _json_list_filename;
26 protected String _solr_url;
27 protected String _output_dir;
28
29 protected int _verbosity;
30
31 public PrepareForIngest(String input_dir, String json_list_filename,
32 String solr_url, String output_dir, int verbosity)
33 {
34 _input_dir = input_dir;
35 _json_list_filename = (json_list_filename != null) ? json_list_filename : input_dir;
36
37 _solr_url = solr_url;
38 _output_dir = output_dir;
39 _verbosity = verbosity;
40 }
41
42 public void exec()
43 {
44 String spark_app_name = "HathiTrust Extract Features: Prepare for Solr Ingest";
45 spark_app_name += " [" + _json_list_filename + "]";
46
47 SparkConf conf = new SparkConf().setAppName(spark_app_name);
48 JavaSparkContext jsc = new JavaSparkContext(conf);
49
50 if (_verbosity >= 2) {
51 System.out.println("Default Minimum Partions: " + jsc.defaultMinPartitions());
52 System.out.println("Default Parallelism: " + jsc.defaultParallelism());
53 }
54
55 JavaRDD<String> json_list_data = jsc.textFile(_json_list_filename,NUM_PARTITIONS).cache();
56
57 long num_volumes = json_list_data.count();
58 double per_vol = 100.0/(double)num_volumes;
59
60 DoubleAccumulator progress_accum = jsc.sc().doubleAccumulator("Progress Percent");
61
62 PagedJSON paged_json = new PagedJSON(_input_dir,_solr_url,_output_dir,_verbosity, progress_accum,per_vol);
63 //JavaRDD<String> json_ids = json_list_data.flatMap(paged_json).cache();
64
65 json_list_data.foreach(paged_json);
66
67
68/*
69 System.out.println("");
70 System.out.println("############");
71 System.out.println("# Progress Accumulator: " + progress_accum.value());
72 System.out.println("############");
73 System.out.println("");
74*/
75
76 //long num_ids = json_ids.count();
77 long num_ids = num_volumes;
78
79 System.out.println("");
80 System.out.println("############");
81 System.out.println("# Number of page ids: " + num_ids);
82 System.out.println("############");
83 System.out.println("");
84
85 /*
86 if (_output_dir != null) {
87 String rdd_save_file = "rdd-solr-json-page-files";
88 json_ids.saveAsTextFile(rdd_save_file);
89 System.out.println("############");
90 System.out.println("# Saved RDD of Solr JSON page files, top-level, as:");
91 System.out.println("# " + rdd_save_file);
92 System.out.println("############");
93 System.out.println("");
94 }
95 */
96
97 jsc.close();
98 }
99
100 public static void print_usage(HelpFormatter formatter, Options options)
101 {
102 formatter.printHelp("RUN.bash [options] input-dir json-filelist.txt", options);
103 }
104 public static void main(String[] args) {
105
106
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 PrepareForIngest prep_for_ingest
175 = new PrepareForIngest(input_dir,json_list_filename,solr_url,output_dir,verbosity);
176 prep_for_ingest.exec();
177
178 }
179}
Note: See TracBrowser for help on using the repository browser.