[31175] | 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 ProcessForWhitelist 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 |
|
---|
| 32 | protected String _input_dir;
|
---|
| 33 | protected String _json_list_filename;
|
---|
| 34 |
|
---|
| 35 | protected int _verbosity;
|
---|
| 36 |
|
---|
| 37 | public ProcessForWhitelist(String input_dir, String json_list_filename, int verbosity)
|
---|
| 38 | {
|
---|
| 39 | _input_dir = input_dir;
|
---|
| 40 | _json_list_filename = (json_list_filename != null) ? json_list_filename : input_dir;
|
---|
| 41 |
|
---|
| 42 | _verbosity = verbosity;
|
---|
| 43 | }
|
---|
| 44 |
|
---|
| 45 | protected String generateSparkAppName(String exec_mode)
|
---|
| 46 | {
|
---|
| 47 | String spark_app_name = "[" + exec_mode + "] Extracted Features: Process for Whitelist";
|
---|
| 48 | spark_app_name += " [" + _json_list_filename + "]";
|
---|
| 49 |
|
---|
| 50 | return spark_app_name;
|
---|
| 51 | }
|
---|
| 52 |
|
---|
| 53 |
|
---|
| 54 |
|
---|
| 55 | public void execWordCount()
|
---|
| 56 | {
|
---|
| 57 | String spark_app_name = generateSparkAppName("Per Page");
|
---|
| 58 |
|
---|
| 59 | SparkConf conf = new SparkConf().setAppName(spark_app_name);
|
---|
| 60 | JavaSparkContext jsc = new JavaSparkContext(conf);
|
---|
| 61 |
|
---|
| 62 | /*
|
---|
| 63 | if (_verbosity >= 2) {
|
---|
| 64 | System.out.println("Default Minimum Partions: " + jsc.defaultMinPartitions());
|
---|
| 65 | System.out.println("Default Parallelism: " + jsc.defaultParallelism());
|
---|
| 66 | }
|
---|
| 67 | */
|
---|
| 68 |
|
---|
| 69 | int num_partitions = Integer.getInteger("wcsa-ef-ingest.num-partitions", DEFAULT_NUM_PARTITIONS);
|
---|
| 70 | JavaRDD<String> json_list_data = jsc.textFile(_json_list_filename,num_partitions).cache();
|
---|
| 71 |
|
---|
| 72 | long num_volumes = json_list_data.count();
|
---|
| 73 | double per_vol = 100.0/(double)num_volumes;
|
---|
| 74 |
|
---|
| 75 | //JavaRDD<String> json_list_data_rp = json_list_data.repartition((int)(num_volumes/100));
|
---|
| 76 |
|
---|
| 77 | DoubleAccumulator per_vol_progress_accum = jsc.sc().doubleAccumulator("Per Volume Progress Percent");
|
---|
| 78 |
|
---|
| 79 | //String strict_file_io_str = System.getProperty("wcsa-ef-ingest.strict-file-io","true");
|
---|
| 80 | boolean strict_file_io = Boolean.getBoolean("wcsa-ef-ingest.strict-file-io");
|
---|
| 81 |
|
---|
| 82 | PerVolumeWordStreamFlatmap paged_solr_wordfreq_flatmap
|
---|
| 83 | = new PerVolumeWordStreamFlatmap(_input_dir,_verbosity,
|
---|
| 84 | per_vol_progress_accum,per_vol,
|
---|
| 85 | strict_file_io);
|
---|
| 86 | JavaRDD<String> words = json_list_data.flatMap(paged_solr_wordfreq_flatmap); // .cache() *****
|
---|
| 87 |
|
---|
| 88 |
|
---|
| 89 | JavaPairRDD<String, Integer> pairs = words.mapToPair(new PairFunction<String, String, Integer>() {
|
---|
| 90 | public Tuple2<String, Integer> call(String s) { return new Tuple2<String, Integer>(s, 1); }
|
---|
| 91 | });
|
---|
| 92 |
|
---|
| 93 | JavaPairRDD<String, Integer> counts = pairs.reduceByKey(new Function2<Integer, Integer, Integer>() {
|
---|
| 94 | public Integer call(Integer a, Integer b) { return a + b; }
|
---|
| 95 | });
|
---|
| 96 |
|
---|
| 97 |
|
---|
| 98 | //JavaRDD<String> per_page_ids = per_page_jsonobjects.map(paged_json_id_map);
|
---|
| 99 |
|
---|
| 100 | //long num_page_ids = per_page_ids.count(); // trigger lazy eval of: flatmap:per-vol -> map:per-page
|
---|
| 101 |
|
---|
| 102 | counts.saveAsTextFile(_json_list_filename + ".out");
|
---|
| 103 |
|
---|
| 104 |
|
---|
| 105 | //System.out.println("");
|
---|
| 106 | //System.out.println("############");
|
---|
| 107 | //System.out.println("# Number of page ids: " + num_page_ids);
|
---|
| 108 | //System.out.println("############");
|
---|
| 109 |
|
---|
| 110 | jsc.close();
|
---|
| 111 | }
|
---|
| 112 |
|
---|
| 113 |
|
---|
| 114 | public static void print_usage(HelpFormatter formatter, Options options)
|
---|
| 115 | {
|
---|
| 116 | formatter.printHelp("RUN.bash [options] input-dir json-filelist.txt", options);
|
---|
| 117 | }
|
---|
| 118 |
|
---|
| 119 | public static void main(String[] args) {
|
---|
| 120 | Options options = new Options();
|
---|
| 121 |
|
---|
| 122 | Option verbosity_opt = new Option("v", "verbosity", true,
|
---|
| 123 | "Set to control the level of debugging output [0=none, 1=some, 2=lots]");
|
---|
| 124 | verbosity_opt.setRequired(false);
|
---|
| 125 | options.addOption(verbosity_opt);
|
---|
| 126 |
|
---|
| 127 | Option properties_opt = new Option("p", "properties", true,
|
---|
| 128 | "Read in the specified Java properties file");
|
---|
| 129 | properties_opt.setRequired(false);
|
---|
| 130 | options.addOption(properties_opt);
|
---|
| 131 |
|
---|
| 132 | // Need to work with CLI v1.2 as this is the JAR that is bundled with Hadoop/Spark
|
---|
| 133 | CommandLineParser parser = new GnuParser();
|
---|
| 134 | //CommandLineParser parser = new DefaultParser(); // if working with CLI v1.3 and above
|
---|
| 135 |
|
---|
| 136 | HelpFormatter formatter = new HelpFormatter();
|
---|
| 137 | CommandLine cmd = null;
|
---|
| 138 |
|
---|
| 139 | try {
|
---|
| 140 | cmd = parser.parse(options, args);
|
---|
| 141 | }
|
---|
| 142 | catch (ParseException e) {
|
---|
| 143 | System.err.println(e.getMessage());
|
---|
| 144 | print_usage(formatter,options);
|
---|
| 145 | System.exit(1);
|
---|
| 146 | }
|
---|
| 147 |
|
---|
| 148 | String verbosity_str = cmd.getOptionValue("verbosity","1");
|
---|
| 149 | int verbosity = Integer.parseInt(verbosity_str);
|
---|
| 150 |
|
---|
| 151 | String property_filename = cmd.getOptionValue("properties",null);
|
---|
| 152 |
|
---|
| 153 | String[] filtered_args = cmd.getArgs();
|
---|
| 154 |
|
---|
| 155 | if (filtered_args.length != 2) {
|
---|
| 156 | print_usage(formatter,options);
|
---|
| 157 | System.exit(1);
|
---|
| 158 | }
|
---|
| 159 |
|
---|
| 160 | if (property_filename != null) {
|
---|
| 161 | try {
|
---|
| 162 | FileInputStream fis = new FileInputStream(property_filename);
|
---|
| 163 | BufferedInputStream bis = new BufferedInputStream(fis);
|
---|
| 164 |
|
---|
| 165 | System.getProperties().load(bis);
|
---|
| 166 | }
|
---|
| 167 | catch (FileNotFoundException e) {
|
---|
| 168 | e.printStackTrace();
|
---|
| 169 | System.err.println("File not found: '" + property_filename + "'. Skipping property file read");
|
---|
| 170 | }
|
---|
| 171 | catch (IOException e) {
|
---|
| 172 | System.err.println("IO Exception for: '" + property_filename + "'. Malformed syntax? Skipping property file read");
|
---|
| 173 | }
|
---|
| 174 | }
|
---|
| 175 |
|
---|
| 176 | String input_dir = filtered_args[0];
|
---|
| 177 | String json_list_filename = filtered_args[1];
|
---|
| 178 |
|
---|
| 179 | ProcessForWhitelist prep_for_whitelist
|
---|
| 180 | = new ProcessForWhitelist(input_dir,json_list_filename,verbosity);
|
---|
| 181 |
|
---|
| 182 | //String process_ef_json_mode = System.getProperty("wcsa-ef-ingest.process-ef-json-mode","per-page");
|
---|
| 183 | prep_for_whitelist.execWordCount();
|
---|
| 184 |
|
---|
| 185 | }
|
---|
| 186 | }
|
---|