1 | package org.hathitrust;
|
---|
2 |
|
---|
3 | import java.io.Serializable;
|
---|
4 | import org.apache.commons.cli.*;
|
---|
5 |
|
---|
6 | import org.apache.spark.api.java.*;
|
---|
7 | import org.apache.spark.SparkConf;
|
---|
8 |
|
---|
9 | public class PrepareForIngest implements Serializable
|
---|
10 | {
|
---|
11 | private static final long serialVersionUID = 1L;
|
---|
12 |
|
---|
13 | public static final int NUM_PARTITIONS = 6; // default would appear to be 2
|
---|
14 |
|
---|
15 | protected String _input_dir;
|
---|
16 | protected String _json_list_filename;
|
---|
17 | protected String _solr_url;
|
---|
18 | protected String _output_dir;
|
---|
19 |
|
---|
20 | protected int _verbosity;
|
---|
21 |
|
---|
22 | public PrepareForIngest(String input_dir, String json_list_filename,
|
---|
23 | String solr_url, String output_dir, int verbosity)
|
---|
24 | {
|
---|
25 | _input_dir = input_dir;
|
---|
26 | _json_list_filename = (json_list_filename != null) ? json_list_filename : input_dir;
|
---|
27 | _output_dir = output_dir;
|
---|
28 | _verbosity = verbosity;
|
---|
29 | }
|
---|
30 |
|
---|
31 | public void exec()
|
---|
32 | {
|
---|
33 | String spark_app_name = "HathiTrust Extract Features: Prepare for Solr Ingest";
|
---|
34 | spark_app_name += " [" + _json_list_filename + "]";
|
---|
35 |
|
---|
36 | SparkConf conf = new SparkConf().setAppName(spark_app_name);
|
---|
37 | JavaSparkContext jsc = new JavaSparkContext(conf);
|
---|
38 |
|
---|
39 | if (_verbosity >= 2) {
|
---|
40 | System.out.println("Default Minimum Partions: " + jsc.defaultMinPartitions());
|
---|
41 | System.out.println("Default Parallelism: " + jsc.defaultParallelism());
|
---|
42 | }
|
---|
43 |
|
---|
44 | JavaRDD<String> json_list_data = jsc.textFile(_json_list_filename,NUM_PARTITIONS).cache();
|
---|
45 |
|
---|
46 | PagedJSON paged_json = new PagedJSON(_input_dir,_solr_url, _output_dir,_verbosity);
|
---|
47 | JavaRDD<String> json_ids = json_list_data.flatMap(paged_json).cache();
|
---|
48 |
|
---|
49 | long num_ids = json_ids.count();
|
---|
50 | System.out.println("");
|
---|
51 | System.out.println("############");
|
---|
52 | System.out.println("# Number of page ids: " + num_ids);
|
---|
53 | System.out.println("############");
|
---|
54 | System.out.println("");
|
---|
55 |
|
---|
56 | if (_output_dir != null) {
|
---|
57 | String rdd_save_file = "rdd-solr-json-page-files";
|
---|
58 | json_ids.saveAsTextFile(rdd_save_file);
|
---|
59 | System.out.println("############");
|
---|
60 | System.out.println("# Saved RDD of Solr JSON page files, top-level, as:");
|
---|
61 | System.out.println("# " + rdd_save_file);
|
---|
62 | System.out.println("############");
|
---|
63 | System.out.println("");
|
---|
64 | }
|
---|
65 |
|
---|
66 | jsc.close();
|
---|
67 | }
|
---|
68 |
|
---|
69 | public static void print_usage(HelpFormatter formatter, Options options)
|
---|
70 | {
|
---|
71 | formatter.printHelp("RUN.bash [options] input-dir json-filelist.txt", options);
|
---|
72 | }
|
---|
73 | public static void main(String[] args) {
|
---|
74 |
|
---|
75 |
|
---|
76 | Options options = new Options();
|
---|
77 |
|
---|
78 | //.withType(Integer.class)
|
---|
79 | /*
|
---|
80 | options.addOption(OptionBuilder.withLongOpt("verbosity")
|
---|
81 | .withDescription("Set to control the level of debugging output [0=none, 1=some, 2=lots]")
|
---|
82 | .hasArg()
|
---|
83 | .withArgName("v")
|
---|
84 | .isRequired(false)
|
---|
85 | .create());
|
---|
86 | */
|
---|
87 | //Option num_cores_opt = new Option("n", "num-cores", true, "Number of cores to use");
|
---|
88 | //num_cores_opt.setRequired(false);
|
---|
89 | //options.addOption(num_cores_opt);
|
---|
90 |
|
---|
91 | Option verbosity_opt = new Option("v", "verbosity", true,
|
---|
92 | "Set to control the level of debugging output [0=none, 1=some, 2=lots]");
|
---|
93 | verbosity_opt.setRequired(false);
|
---|
94 | options.addOption(verbosity_opt);
|
---|
95 |
|
---|
96 | Option output_dir_opt = new Option("o", "output-dir", true,
|
---|
97 | "If specified, save BZipped Solr JSON files to this directory");
|
---|
98 | output_dir_opt.setRequired(false);
|
---|
99 | options.addOption(output_dir_opt);
|
---|
100 |
|
---|
101 | Option solr_url_opt = new Option("u", "solr-url", true,
|
---|
102 | "If specified, the URL to post the Solr JSON data to");
|
---|
103 | solr_url_opt.setRequired(false);
|
---|
104 | options.addOption(solr_url_opt);
|
---|
105 |
|
---|
106 | Option dry_run_opt = new Option("r", "dry-run", false,
|
---|
107 | "Used to initiate a 'dry-run' where the files are all read in, but nothing is ingested/saved");
|
---|
108 | dry_run_opt.setRequired(false);
|
---|
109 | options.addOption(dry_run_opt);
|
---|
110 |
|
---|
111 | // need to work with CLI v1.2 as this is the JAR that is bundled with Hadoop/Spark
|
---|
112 | CommandLineParser parser = new GnuParser();
|
---|
113 | //CommandLineParser parser = new DefaultParser(); // if working with CLI v1.3 and above
|
---|
114 |
|
---|
115 | HelpFormatter formatter = new HelpFormatter();
|
---|
116 | CommandLine cmd = null;
|
---|
117 |
|
---|
118 | try {
|
---|
119 | cmd = parser.parse(options, args);
|
---|
120 | }
|
---|
121 | catch (ParseException e) {
|
---|
122 | System.err.println(e.getMessage());
|
---|
123 | print_usage(formatter,options);
|
---|
124 | System.exit(1);
|
---|
125 | //return; // prevents 'cmd may not be assigned' compiler error in Eclipse
|
---|
126 | }
|
---|
127 |
|
---|
128 | //value = ((Integer)cmdLine.getParsedOptionValue("num-cores")).intValue();
|
---|
129 | //value = ((Integer)cmdLine.getOptionValue("num-cores","2")).intValue();
|
---|
130 |
|
---|
131 | //cmd.hasOption("json-filelist")
|
---|
132 |
|
---|
133 | String verbosity_str = cmd.getOptionValue("verbosity","0");
|
---|
134 | int verbosity = Integer.parseInt(verbosity_str);
|
---|
135 |
|
---|
136 | String output_dir = cmd.getOptionValue("output-dir",null);
|
---|
137 | String solr_url = cmd.getOptionValue("solr-url",null);
|
---|
138 | boolean dry_run = cmd.hasOption("dry-run");
|
---|
139 |
|
---|
140 | String[] filtered_args = cmd.getArgs();
|
---|
141 |
|
---|
142 | if (filtered_args.length != 2) {
|
---|
143 | print_usage(formatter,options);
|
---|
144 | System.exit(1);
|
---|
145 | }
|
---|
146 |
|
---|
147 | if (!dry_run && ((output_dir == null) && (solr_url==null))) {
|
---|
148 | System.err.println("Need to specify either --solr-url or --output-dir otherwise generated files are not ingested/saved");
|
---|
149 | print_usage(formatter,options);
|
---|
150 | System.exit(1);
|
---|
151 | }
|
---|
152 |
|
---|
153 | String input_dir = filtered_args[0];
|
---|
154 | String json_list_filename = filtered_args[1];
|
---|
155 | //String output_dir = filtered_args[2];
|
---|
156 |
|
---|
157 | PrepareForIngest prep_for_ingest
|
---|
158 | = new PrepareForIngest(input_dir,json_list_filename,solr_url,output_dir,verbosity);
|
---|
159 | prep_for_ingest.exec();
|
---|
160 |
|
---|
161 | }
|
---|
162 | }
|
---|