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

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

_solr_url needs to be stored in class!

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