1 | package org.hathitrust.extractedfeatures;
|
---|
2 |
|
---|
3 | import java.io.BufferedInputStream;
|
---|
4 | import java.io.FileInputStream;
|
---|
5 | import java.io.FileNotFoundException;
|
---|
6 | import java.io.IOException;
|
---|
7 | import java.io.Serializable;
|
---|
8 | import org.apache.commons.cli.*;
|
---|
9 |
|
---|
10 | import org.apache.spark.api.java.*;
|
---|
11 | import org.apache.spark.api.java.function.Function2;
|
---|
12 | import org.apache.spark.api.java.function.PairFunction;
|
---|
13 | import org.apache.spark.util.DoubleAccumulator;
|
---|
14 | import scala.Tuple2;
|
---|
15 |
|
---|
16 | import org.apache.spark.SparkConf;
|
---|
17 |
|
---|
18 | public class ProcessForPOSCount implements Serializable
|
---|
19 | {
|
---|
20 | private static final long serialVersionUID = 1L;
|
---|
21 |
|
---|
22 | // Following details on number of partitions to use given in
|
---|
23 | // "Parallelized collections" section of:
|
---|
24 | // https://spark.apache.org/docs/2.0.1/programming-guide.html
|
---|
25 | //
|
---|
26 | // For a more detailed discussion see:
|
---|
27 | // http://blog.cloudera.com/blog/2015/03/how-to-tune-your-apache-spark-jobs-part-2/
|
---|
28 |
|
---|
29 | //protected static final int DEFAULT_NUM_CORES = 6;
|
---|
30 | //protected static final int DEFAULT_NUM_PARTITIONS = 3*DEFAULT_NUM_CORES;
|
---|
31 | protected static final int DEFAULT_FILES_PER_PARTITION = 3000;
|
---|
32 |
|
---|
33 | protected String _input_dir;
|
---|
34 | protected String _json_list_filename;
|
---|
35 |
|
---|
36 | protected int _verbosity;
|
---|
37 |
|
---|
38 | public ProcessForPOSCount(String input_dir, String json_list_filename, int verbosity)
|
---|
39 | {
|
---|
40 | _input_dir = input_dir;
|
---|
41 | _json_list_filename = (json_list_filename != null) ? json_list_filename : input_dir;
|
---|
42 |
|
---|
43 | _verbosity = verbosity;
|
---|
44 | }
|
---|
45 |
|
---|
46 | protected String generateSparkAppName(String exec_mode)
|
---|
47 | {
|
---|
48 | String spark_app_name = "[" + exec_mode + "] Extracted Features: Process for POS Count";
|
---|
49 | spark_app_name += " [" + _json_list_filename + "]";
|
---|
50 |
|
---|
51 | return spark_app_name;
|
---|
52 | }
|
---|
53 |
|
---|
54 | public void execPOSCount()
|
---|
55 | {
|
---|
56 | String spark_app_name = generateSparkAppName("Per Volume");
|
---|
57 |
|
---|
58 | SparkConf conf = new SparkConf().setAppName(spark_app_name);
|
---|
59 | JavaSparkContext jsc = new JavaSparkContext(conf);
|
---|
60 |
|
---|
61 | String filename_root = _json_list_filename.replaceAll(".*/","").replaceAll("\\..*$","");
|
---|
62 | String output_directory = "pos-" + filename_root + "-out";
|
---|
63 | if (ClusterFileIO.exists(output_directory))
|
---|
64 | {
|
---|
65 | System.err.println("Error: " + output_directory + " already exists. Spark unable to write output data");
|
---|
66 | jsc.close();
|
---|
67 | System.exit(1);
|
---|
68 | }
|
---|
69 |
|
---|
70 | //int num_partitions = Integer.getInteger("wcsa-ef-ingest.num-partitions", DEFAULT_NUM_PARTITIONS);
|
---|
71 | int files_per_partition = Integer.getInteger("wcsa-ef-ingest.num-partitions", DEFAULT_FILES_PER_PARTITION);
|
---|
72 |
|
---|
73 |
|
---|
74 | JavaRDD<String> json_list_data = jsc.textFile(_json_list_filename).cache();
|
---|
75 | json_list_data.setName("JSON-file-list");
|
---|
76 |
|
---|
77 | long num_volumes = json_list_data.count();
|
---|
78 | double per_vol = 100.0/(double)num_volumes;
|
---|
79 |
|
---|
80 | int num_partitions = (int)(num_volumes/files_per_partition)+1;
|
---|
81 | JavaRDD<String> json_list_data_rp = json_list_data.repartition(num_partitions);
|
---|
82 | json_list_data_rp.setName("JSON-file-list--repartitioned");
|
---|
83 |
|
---|
84 | DoubleAccumulator per_vol_progress_accum = jsc.sc().doubleAccumulator("Per Volume Progress Percent");
|
---|
85 |
|
---|
86 | boolean strict_file_io = Boolean.getBoolean("wcsa-ef-ingest.strict-file-io");
|
---|
87 |
|
---|
88 | PerVolumePOSStreamFlatmap paged_solr_posfreq_flatmap
|
---|
89 | = new PerVolumePOSStreamFlatmap(_input_dir,_verbosity,
|
---|
90 | per_vol_progress_accum,per_vol,
|
---|
91 | strict_file_io);
|
---|
92 | JavaRDD<String> pos_list = json_list_data_rp.flatMap(paged_solr_posfreq_flatmap);
|
---|
93 | pos_list.setName("pos-stream");
|
---|
94 |
|
---|
95 | /*
|
---|
96 | JavaPairRDD<String, Integer> pos_pairs = pos_list.mapToPair(new PairFunction<String, String, Integer>() {
|
---|
97 | public Tuple2<String, Integer> call(String s) { return new Tuple2<String, Integer>(s, 1); }
|
---|
98 | });
|
---|
99 | pos_pairs.setName("single-pos-count");
|
---|
100 |
|
---|
101 | JavaPairRDD<String, Integer> pos_counts = pos_pairs.reduceByKey(new Function2<Integer, Integer, Integer>() {
|
---|
102 | public Integer call(Integer a, Integer b) { return a + b; }
|
---|
103 | });
|
---|
104 | pos_counts.setName("pos-frequency");
|
---|
105 | */
|
---|
106 |
|
---|
107 | JavaPairRDD<String, Long> pos_pairs = pos_list.mapToPair(s -> new Tuple2<String, Long>(s, 1L));
|
---|
108 | pos_pairs.setName("single-pos-count");
|
---|
109 |
|
---|
110 | JavaPairRDD<String, Long> pos_counts = pos_pairs.reduceByKey((a, b) -> a + b);
|
---|
111 | pos_counts.setName("pos-frequency");
|
---|
112 |
|
---|
113 | JavaPairRDD<Long, String> pos_counts_swapped_pair
|
---|
114 | = pos_counts.mapToPair(item -> item.swap());
|
---|
115 | pos_counts_swapped_pair.setName("frequency-pos-swap");
|
---|
116 |
|
---|
117 | JavaPairRDD<Long, String> pos_counts_swapped_pair_sorted
|
---|
118 | = pos_counts_swapped_pair.sortByKey(false, num_partitions);
|
---|
119 | pos_counts_swapped_pair_sorted.setName("descending-sorted-frequency-pos");
|
---|
120 |
|
---|
121 | JavaPairRDD<String, Long> pos_count_sorted
|
---|
122 | = pos_counts_swapped_pair_sorted.mapToPair(item -> item.swap());
|
---|
123 | pos_count_sorted.setName("descending-pos-frequency");
|
---|
124 |
|
---|
125 |
|
---|
126 | pos_count_sorted.saveAsTextFile(output_directory);
|
---|
127 | jsc.close();
|
---|
128 | }
|
---|
129 |
|
---|
130 |
|
---|
131 | public static void print_usage(HelpFormatter formatter, Options options)
|
---|
132 | {
|
---|
133 | formatter.printHelp("RUN.bash [options] input-dir json-filelist.txt", options);
|
---|
134 | }
|
---|
135 |
|
---|
136 | public static void main(String[] args) {
|
---|
137 | Options options = new Options();
|
---|
138 |
|
---|
139 | Option verbosity_opt = new Option("v", "verbosity", true,
|
---|
140 | "Set to control the level of debugging output [0=none, 1=some, 2=lots]");
|
---|
141 | verbosity_opt.setRequired(false);
|
---|
142 | options.addOption(verbosity_opt);
|
---|
143 |
|
---|
144 | Option properties_opt = new Option("p", "properties", true,
|
---|
145 | "Read in the specified Java properties file");
|
---|
146 | properties_opt.setRequired(false);
|
---|
147 | options.addOption(properties_opt);
|
---|
148 |
|
---|
149 | // Need to work with CLI v1.2 as this is the JAR that is bundled with Hadoop/Spark
|
---|
150 | CommandLineParser parser = new GnuParser();
|
---|
151 | //CommandLineParser parser = new DefaultParser(); // if working with CLI v1.3 and above
|
---|
152 |
|
---|
153 | HelpFormatter formatter = new HelpFormatter();
|
---|
154 | CommandLine cmd = null;
|
---|
155 |
|
---|
156 | try {
|
---|
157 | cmd = parser.parse(options, args);
|
---|
158 | }
|
---|
159 | catch (ParseException e) {
|
---|
160 | System.err.println(e.getMessage());
|
---|
161 | print_usage(formatter,options);
|
---|
162 | System.exit(1);
|
---|
163 | }
|
---|
164 |
|
---|
165 | String verbosity_str = cmd.getOptionValue("verbosity","1");
|
---|
166 | int verbosity = Integer.parseInt(verbosity_str);
|
---|
167 |
|
---|
168 | String property_filename = cmd.getOptionValue("properties",null);
|
---|
169 |
|
---|
170 | String[] filtered_args = cmd.getArgs();
|
---|
171 |
|
---|
172 | if (filtered_args.length != 2) {
|
---|
173 | print_usage(formatter,options);
|
---|
174 | System.exit(1);
|
---|
175 | }
|
---|
176 |
|
---|
177 | if (property_filename != null) {
|
---|
178 | try {
|
---|
179 | FileInputStream fis = new FileInputStream(property_filename);
|
---|
180 | BufferedInputStream bis = new BufferedInputStream(fis);
|
---|
181 |
|
---|
182 | System.getProperties().load(bis);
|
---|
183 | }
|
---|
184 | catch (FileNotFoundException e) {
|
---|
185 | e.printStackTrace();
|
---|
186 | System.err.println("File not found: '" + property_filename + "'. Skipping property file read");
|
---|
187 | }
|
---|
188 | catch (IOException e) {
|
---|
189 | System.err.println("IO Exception for: '" + property_filename + "'. Malformed syntax? Skipping property file read");
|
---|
190 | }
|
---|
191 | }
|
---|
192 |
|
---|
193 | String input_dir = filtered_args[0];
|
---|
194 | String json_list_filename = filtered_args[1];
|
---|
195 |
|
---|
196 | ProcessForPOSCount prep_for_pos
|
---|
197 | = new ProcessForPOSCount(input_dir,json_list_filename,verbosity);
|
---|
198 |
|
---|
199 | prep_for_pos.execPOSCount();
|
---|
200 |
|
---|
201 | }
|
---|
202 | }
|
---|