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 | import org.apache.hadoop.io.Text;
|
---|
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.storage.StorageLevel;
|
---|
14 | import org.apache.spark.util.DoubleAccumulator;
|
---|
15 | import scala.Tuple2;
|
---|
16 |
|
---|
17 | import org.apache.spark.SparkConf;
|
---|
18 |
|
---|
19 | public class ProcessForCatalogLangCount implements Serializable
|
---|
20 | {
|
---|
21 | private static final long serialVersionUID = 1L;
|
---|
22 |
|
---|
23 | protected static final int DEFAULT_FILES_PER_PARTITION = 3000;
|
---|
24 |
|
---|
25 | protected String _input_dir;
|
---|
26 | protected String _json_list_filename;
|
---|
27 |
|
---|
28 | protected int _verbosity;
|
---|
29 |
|
---|
30 | public ProcessForCatalogLangCount(String input_dir, String json_list_filename, int verbosity)
|
---|
31 | {
|
---|
32 | _input_dir = input_dir;
|
---|
33 | _json_list_filename = (json_list_filename != null) ? json_list_filename : input_dir;
|
---|
34 |
|
---|
35 | _verbosity = verbosity;
|
---|
36 | }
|
---|
37 |
|
---|
38 | protected String generateSparkAppName(String exec_mode)
|
---|
39 | {
|
---|
40 | String spark_app_name = "[" + exec_mode + "] Extracted Features: Process for Catalog Language Labels";
|
---|
41 | spark_app_name += " [" + _json_list_filename + "]";
|
---|
42 |
|
---|
43 | return spark_app_name;
|
---|
44 | }
|
---|
45 |
|
---|
46 | public void execCatalogLangCountSparkDirect()
|
---|
47 | {
|
---|
48 | String spark_app_name = generateSparkAppName("Spark-Direct + Per Volume");
|
---|
49 |
|
---|
50 | SparkConf conf = new SparkConf().setAppName(spark_app_name);
|
---|
51 | JavaSparkContext jsc = new JavaSparkContext(conf);
|
---|
52 |
|
---|
53 | String filename_root = _json_list_filename.replaceAll(".*/","").replaceAll("\\..*$","");
|
---|
54 | String output_directory = "catalog-lang-" + filename_root + "-out";
|
---|
55 | if (ClusterFileIO.exists(output_directory))
|
---|
56 | {
|
---|
57 | System.err.println("Error: " + output_directory + " already exists. Spark unable to write output data");
|
---|
58 | jsc.close();
|
---|
59 | System.exit(1);
|
---|
60 | }
|
---|
61 |
|
---|
62 | //int num_partitions = Integer.getInteger("wcsa-ef-ingest.num-partitions", DEFAULT_NUM_PARTITIONS);
|
---|
63 | int files_per_partition = Integer.getInteger("wcsa-ef-ingest.files-per-partition", DEFAULT_FILES_PER_PARTITION);
|
---|
64 |
|
---|
65 | JavaRDD<String> json_list_data = jsc.textFile(_json_list_filename).cache();
|
---|
66 | json_list_data.setName("JSON-file-list");
|
---|
67 |
|
---|
68 | long num_volumes = json_list_data.count();
|
---|
69 | double per_vol = 100.0/(double)num_volumes;
|
---|
70 |
|
---|
71 | int num_partitions = (int)(num_volumes/files_per_partition)+1;
|
---|
72 |
|
---|
73 | JavaRDD<String> json_list_data_rp = json_list_data.repartition(num_partitions);
|
---|
74 | json_list_data_rp.setName("JSON-file-list--repartitioned");
|
---|
75 |
|
---|
76 | DoubleAccumulator per_vol_progress_accum = jsc.sc().doubleAccumulator("Per Volume Progress Percent");
|
---|
77 |
|
---|
78 | boolean strict_file_io = Boolean.getBoolean("wcsa-ef-ingest.strict-file-io");
|
---|
79 |
|
---|
80 | PerVolumeCatalogLangStreamFlatmap volume_catalog_langfreq_flatmap
|
---|
81 | = new PerVolumeCatalogLangStreamFlatmap(_input_dir,_verbosity,
|
---|
82 | per_vol_progress_accum,per_vol,
|
---|
83 | strict_file_io);
|
---|
84 | JavaRDD<String> catalog_lang_list = json_list_data_rp.flatMap(volume_catalog_langfreq_flatmap);
|
---|
85 | catalog_lang_list.persist(StorageLevel.MEMORY_AND_DISK());
|
---|
86 | catalog_lang_list.setName("catalog-lang-stream");
|
---|
87 |
|
---|
88 |
|
---|
89 | JavaPairRDD<String, Long> catalog_lang_pairs = catalog_lang_list.mapToPair(s -> new Tuple2<String, Long>(s, 1L));
|
---|
90 | catalog_lang_pairs.setName("single-catalog-lang-count");
|
---|
91 |
|
---|
92 | JavaPairRDD<String, Long> catalog_lang_counts = catalog_lang_pairs.reduceByKey((a, b) -> a + b);
|
---|
93 | catalog_lang_counts.setName("catalog-lang-frequency");
|
---|
94 |
|
---|
95 | JavaPairRDD<Long, String> catalog_lang_counts_swapped_pair
|
---|
96 | = catalog_lang_counts.mapToPair(item -> item.swap());
|
---|
97 | catalog_lang_counts_swapped_pair.setName("frequency-catalog-lang-swap");
|
---|
98 |
|
---|
99 | JavaPairRDD<Long, String> catalog_lang_counts_swapped_pair_sorted
|
---|
100 | = catalog_lang_counts_swapped_pair.sortByKey(false, num_partitions);
|
---|
101 | catalog_lang_counts_swapped_pair_sorted.setName("descending-sorted-frequency-catalog-lang");
|
---|
102 |
|
---|
103 | JavaPairRDD<String, Long> catalog_lang_count_sorted
|
---|
104 | = catalog_lang_counts_swapped_pair_sorted.mapToPair(item -> item.swap());
|
---|
105 | catalog_lang_count_sorted.setName("descending-catalog-lang-frequency");
|
---|
106 |
|
---|
107 |
|
---|
108 | catalog_lang_count_sorted.saveAsTextFile(output_directory);
|
---|
109 | jsc.close();
|
---|
110 | }
|
---|
111 |
|
---|
112 | public void execCatalogLangCount()
|
---|
113 | {
|
---|
114 |
|
---|
115 |
|
---|
116 | String spark_app_name = generateSparkAppName("YARN Cluster + Per Volume");
|
---|
117 |
|
---|
118 | SparkConf conf = new SparkConf().setAppName(spark_app_name);
|
---|
119 | JavaSparkContext jsc = new JavaSparkContext(conf);
|
---|
120 | jsc.hadoopConfiguration().set("io.compression.codec.bzip2.library", "java-builtin");
|
---|
121 |
|
---|
122 | String packed_sequence_path = "hdfs:///user/capitanu/data/packed-ef";
|
---|
123 |
|
---|
124 | JavaPairRDD<String, Text> inputRdd = jsc.sequenceFile(packed_sequence_path, String.class, Text.class);
|
---|
125 | JavaRDD<Text> jsonTextRdd = inputRdd.map(Tuple2::_2);
|
---|
126 |
|
---|
127 | /*
|
---|
128 | jsonTextRdd.map(
|
---|
129 | jsonText -> "" // parse JSON text and do whatever ...
|
---|
130 | );
|
---|
131 | */
|
---|
132 |
|
---|
133 | boolean strict_file_io = Boolean.getBoolean("wcsa-ef-ingest.strict-file-io");
|
---|
134 |
|
---|
135 | PerVolumeCatalogLangSequenceFileMap volume_catalog_langfreq_map
|
---|
136 | = new PerVolumeCatalogLangSequenceFileMap(_input_dir,_verbosity,strict_file_io);
|
---|
137 | JavaRDD<String> catalog_lang_list = jsonTextRdd.map(volume_catalog_langfreq_map);
|
---|
138 | //catalog_lang_list.persist(StorageLevel.MEMORY_AND_DISK());
|
---|
139 | catalog_lang_list.setName("catalog-lang-stream");
|
---|
140 |
|
---|
141 |
|
---|
142 | JavaPairRDD<String, Long> catalog_lang_pairs = catalog_lang_list.mapToPair(s -> new Tuple2<String, Long>(s, 1L));
|
---|
143 | catalog_lang_pairs.setName("single-catalog-lang-count");
|
---|
144 |
|
---|
145 | JavaPairRDD<String, Long> catalog_lang_counts = catalog_lang_pairs.reduceByKey((a, b) -> a + b);
|
---|
146 | catalog_lang_counts.setName("catalog-lang-frequency");
|
---|
147 |
|
---|
148 | JavaPairRDD<Long, String> catalog_lang_counts_swapped_pair
|
---|
149 | = catalog_lang_counts.mapToPair(item -> item.swap());
|
---|
150 | catalog_lang_counts_swapped_pair.setName("frequency-catalog-lang-swap");
|
---|
151 |
|
---|
152 | JavaPairRDD<Long, String> catalog_lang_counts_swapped_pair_sorted
|
---|
153 | = catalog_lang_counts_swapped_pair.sortByKey(false);
|
---|
154 | catalog_lang_counts_swapped_pair_sorted.setName("descending-sorted-frequency-catalog-lang");
|
---|
155 |
|
---|
156 | JavaPairRDD<String, Long> catalog_lang_count_sorted
|
---|
157 | = catalog_lang_counts_swapped_pair_sorted.mapToPair(item -> item.swap());
|
---|
158 | catalog_lang_count_sorted.setName("descending-catalog-lang-frequency");
|
---|
159 |
|
---|
160 | String filename_root = _json_list_filename.replaceAll(".*/","").replaceAll("\\..*$","");
|
---|
161 | String output_directory = "catalog-lang-" + filename_root + "-out";
|
---|
162 | catalog_lang_count_sorted.saveAsTextFile(output_directory);
|
---|
163 | jsc.close();
|
---|
164 | }
|
---|
165 |
|
---|
166 |
|
---|
167 |
|
---|
168 | public static void print_usage(HelpFormatter formatter, Options options)
|
---|
169 | {
|
---|
170 | formatter.printHelp("RUN.bash [options] input-dir json-filelist.txt", options);
|
---|
171 | }
|
---|
172 |
|
---|
173 | public static void main(String[] args) {
|
---|
174 | Options options = new Options();
|
---|
175 |
|
---|
176 | Option verbosity_opt = new Option("v", "verbosity", true,
|
---|
177 | "Set to control the level of debugging output [0=none, 1=some, 2=lots]");
|
---|
178 | verbosity_opt.setRequired(false);
|
---|
179 | options.addOption(verbosity_opt);
|
---|
180 |
|
---|
181 | Option properties_opt = new Option("p", "properties", true,
|
---|
182 | "Read in the specified Java properties file");
|
---|
183 | properties_opt.setRequired(false);
|
---|
184 | options.addOption(properties_opt);
|
---|
185 |
|
---|
186 | // Need to work with CLI v1.2 as this is the JAR that is bundled with Hadoop/Spark
|
---|
187 | CommandLineParser parser = new GnuParser();
|
---|
188 | //CommandLineParser parser = new DefaultParser(); // if working with CLI v1.3 and above
|
---|
189 |
|
---|
190 | HelpFormatter formatter = new HelpFormatter();
|
---|
191 | CommandLine cmd = null;
|
---|
192 |
|
---|
193 | try {
|
---|
194 | cmd = parser.parse(options, args);
|
---|
195 | }
|
---|
196 | catch (ParseException e) {
|
---|
197 | System.err.println(e.getMessage());
|
---|
198 | print_usage(formatter,options);
|
---|
199 | System.exit(1);
|
---|
200 | }
|
---|
201 |
|
---|
202 | String verbosity_str = cmd.getOptionValue("verbosity","1");
|
---|
203 | int verbosity = Integer.parseInt(verbosity_str);
|
---|
204 |
|
---|
205 | String property_filename = cmd.getOptionValue("properties",null);
|
---|
206 |
|
---|
207 | String[] filtered_args = cmd.getArgs();
|
---|
208 |
|
---|
209 | if (filtered_args.length != 2) {
|
---|
210 | print_usage(formatter,options);
|
---|
211 | System.exit(1);
|
---|
212 | }
|
---|
213 |
|
---|
214 | if (property_filename != null) {
|
---|
215 | try {
|
---|
216 | FileInputStream fis = new FileInputStream(property_filename);
|
---|
217 | BufferedInputStream bis = new BufferedInputStream(fis);
|
---|
218 |
|
---|
219 | System.getProperties().load(bis);
|
---|
220 | }
|
---|
221 | catch (FileNotFoundException e) {
|
---|
222 | e.printStackTrace();
|
---|
223 | System.err.println("File not found: '" + property_filename + "'. Skipping property file read");
|
---|
224 | }
|
---|
225 | catch (IOException e) {
|
---|
226 | System.err.println("IO Exception for: '" + property_filename + "'. Malformed syntax? Skipping property file read");
|
---|
227 | }
|
---|
228 | }
|
---|
229 |
|
---|
230 | String input_dir = filtered_args[0];
|
---|
231 | String json_list_filename = filtered_args[1];
|
---|
232 |
|
---|
233 | ProcessForCatalogLangCount prep_for_lang
|
---|
234 | = new ProcessForCatalogLangCount(input_dir,json_list_filename,verbosity);
|
---|
235 |
|
---|
236 | prep_for_lang.execCatalogLangCount();
|
---|
237 |
|
---|
238 | }
|
---|
239 | }
|
---|