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 | String rdd_save_file = "rdd-solr-json-page-files";
|
---|
57 | json_ids.saveAsTextFile(rdd_save_file);
|
---|
58 | System.out.println("############");
|
---|
59 | System.out.println("# Saved RDD of Solr JSON page files, top-level, as:");
|
---|
60 | System.out.println("# " + rdd_save_file);
|
---|
61 | System.out.println("############");
|
---|
62 | System.out.println("");
|
---|
63 |
|
---|
64 | jsc.close();
|
---|
65 | }
|
---|
66 |
|
---|
67 | public static void print_usage(HelpFormatter formatter, Options options)
|
---|
68 | {
|
---|
69 | formatter.printHelp("RUN.bash [options] input-dir json-filelist.txt", options);
|
---|
70 | }
|
---|
71 | public static void main(String[] args) {
|
---|
72 |
|
---|
73 |
|
---|
74 | Options options = new Options();
|
---|
75 |
|
---|
76 | //.withType(Integer.class)
|
---|
77 | /*
|
---|
78 | options.addOption(OptionBuilder.withLongOpt("verbosity")
|
---|
79 | .withDescription("Set to control the level of debugging output [0=none, 1=some, 2=lots]")
|
---|
80 | .hasArg()
|
---|
81 | .withArgName("v")
|
---|
82 | .isRequired(false)
|
---|
83 | .create());
|
---|
84 | */
|
---|
85 | //Option num_cores_opt = new Option("n", "num-cores", true, "Number of cores to use");
|
---|
86 | //num_cores_opt.setRequired(false);
|
---|
87 | //options.addOption(num_cores_opt);
|
---|
88 |
|
---|
89 | Option verbosity_opt = new Option("v", "verbosity", true,
|
---|
90 | "Set to control the level of debugging output [0=none, 1=some, 2=lots]");
|
---|
91 | verbosity_opt.setRequired(false);
|
---|
92 | options.addOption(verbosity_opt);
|
---|
93 |
|
---|
94 | Option output_dir_opt = new Option("o", "output-dir", true,
|
---|
95 | "If specified, save BZipped Solr JSON files to this directory");
|
---|
96 | output_dir_opt.setRequired(false);
|
---|
97 | options.addOption(output_dir_opt);
|
---|
98 |
|
---|
99 | Option solr_url_opt = new Option("u", "solr-url", true,
|
---|
100 | "If specified, the URL to post the Solr JSON data to");
|
---|
101 | solr_url_opt.setRequired(false);
|
---|
102 | options.addOption(solr_url_opt);
|
---|
103 |
|
---|
104 | Option dry_run_opt = new Option("r", "dry-run", false,
|
---|
105 | "Used to initiate a 'dry-run' where the files are all read in, but nothing is ingested/saved");
|
---|
106 | dry_run_opt.setRequired(false);
|
---|
107 | options.addOption(dry_run_opt);
|
---|
108 |
|
---|
109 | // need to work with CLI v1.2 as this is the JAR that is bundled with Hadoop/Spark
|
---|
110 | CommandLineParser parser = new GnuParser();
|
---|
111 | //CommandLineParser parser = new DefaultParser(); // if working with CLI v1.3 and above
|
---|
112 |
|
---|
113 | HelpFormatter formatter = new HelpFormatter();
|
---|
114 | CommandLine cmd = null;
|
---|
115 |
|
---|
116 | try {
|
---|
117 | cmd = parser.parse(options, args);
|
---|
118 | }
|
---|
119 | catch (ParseException e) {
|
---|
120 | System.err.println(e.getMessage());
|
---|
121 | print_usage(formatter,options);
|
---|
122 | System.exit(1);
|
---|
123 | //return; // prevents 'cmd may not be assigned' compiler error in Eclipse
|
---|
124 | }
|
---|
125 |
|
---|
126 | //value = ((Integer)cmdLine.getParsedOptionValue("num-cores")).intValue();
|
---|
127 | //value = ((Integer)cmdLine.getOptionValue("num-cores","2")).intValue();
|
---|
128 |
|
---|
129 | //cmd.hasOption("json-filelist")
|
---|
130 |
|
---|
131 | String verbosity_str = cmd.getOptionValue("verbosity","0");
|
---|
132 | int verbosity = Integer.parseInt(verbosity_str);
|
---|
133 |
|
---|
134 | String output_dir = cmd.getOptionValue("output-dir",null);
|
---|
135 | String solr_url = cmd.getOptionValue("solr-url",null);
|
---|
136 | boolean dry_run = cmd.hasOption("dry-run");
|
---|
137 |
|
---|
138 | String[] filtered_args = cmd.getArgs();
|
---|
139 |
|
---|
140 | if (filtered_args.length != 2) {
|
---|
141 | print_usage(formatter,options);
|
---|
142 | System.exit(1);
|
---|
143 | }
|
---|
144 |
|
---|
145 | if (!dry_run && ((output_dir == null) && (solr_url==null))) {
|
---|
146 | System.err.println("Need to specify either --solr-url or --output-dir otherwise generated files are not ingested/saved");
|
---|
147 | print_usage(formatter,options);
|
---|
148 | }
|
---|
149 | String json_list_filename = filtered_args[0];
|
---|
150 | String input_dir = filtered_args[1];
|
---|
151 | //String output_dir = filtered_args[2];
|
---|
152 |
|
---|
153 | PrepareForIngest prep_for_ingest
|
---|
154 | = new PrepareForIngest(input_dir,json_list_filename,solr_url,output_dir,verbosity);
|
---|
155 | prep_for_ingest.exec();
|
---|
156 |
|
---|
157 | }
|
---|
158 | }
|
---|