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

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

Changed flag to 'read-only' and changed the filed name full text saved under

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