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