1 | package org.hathitrust;
|
---|
2 |
|
---|
3 | import java.io.IOException;
|
---|
4 | import java.io.Serializable;
|
---|
5 | import java.nio.charset.StandardCharsets;
|
---|
6 | import java.nio.file.Files;
|
---|
7 | import java.nio.file.Path;
|
---|
8 | import java.nio.file.Paths;
|
---|
9 | import java.util.List;
|
---|
10 |
|
---|
11 | import org.apache.commons.cli.*;
|
---|
12 |
|
---|
13 | import org.apache.spark.api.java.*;
|
---|
14 | import org.apache.spark.SparkConf;
|
---|
15 | import org.apache.spark.api.java.function.Function;
|
---|
16 |
|
---|
17 | public class PrepareForIngest implements Serializable
|
---|
18 | {
|
---|
19 | private static final long serialVersionUID = 1L;
|
---|
20 |
|
---|
21 | protected String _input_dir;
|
---|
22 | protected String _json_list_filename;
|
---|
23 | protected String _output_dir;
|
---|
24 |
|
---|
25 | protected int _verbosity;
|
---|
26 |
|
---|
27 | public PrepareForIngest(String input_dir, String json_list_filename, String output_dir, int verbosity)
|
---|
28 | {
|
---|
29 | _input_dir = input_dir;
|
---|
30 | _json_list_filename = (json_list_filename != null) ? json_list_filename : input_dir;
|
---|
31 | _output_dir = output_dir;
|
---|
32 | _verbosity = verbosity;
|
---|
33 | }
|
---|
34 |
|
---|
35 | public void exec()
|
---|
36 | {
|
---|
37 | String spark_app_name = "HathiTrust Extract Features: Prepare for Solr Ingest";
|
---|
38 | spark_app_name += "[" + _json_list_filename + "]";
|
---|
39 |
|
---|
40 | SparkConf conf = new SparkConf().setAppName(spark_app_name);
|
---|
41 | JavaSparkContext jsc = new JavaSparkContext(conf);
|
---|
42 | ClusterFileIO.init(_input_dir);
|
---|
43 |
|
---|
44 | // Check output directory exists, and create it if not
|
---|
45 |
|
---|
46 |
|
---|
47 | JavaRDD<String> json_list_data = jsc.textFile(_json_list_filename).cache();
|
---|
48 |
|
---|
49 | JavaRDD<String> json_ids = json_list_data.flatMap(new PagedJSON(_input_dir));
|
---|
50 |
|
---|
51 |
|
---|
52 | //long numAs = json_list_data.filter(new ContainsA()).count();
|
---|
53 |
|
---|
54 |
|
---|
55 | /*
|
---|
56 | long numBs = json_list_data.filter(new Function<String, Boolean>() {
|
---|
57 | public Boolean call(String s) { return s.contains("b"); }
|
---|
58 | }).count();
|
---|
59 |
|
---|
60 | System.out.println("#### Lines with a: " + numAs + ", lines with b: " + numBs);
|
---|
61 | */
|
---|
62 | long num_ids = json_ids.count();
|
---|
63 | System.out.println("");
|
---|
64 | System.out.println("############");
|
---|
65 | System.out.println("# number of IDS: " + num_ids);
|
---|
66 | System.out.println("############");
|
---|
67 | System.out.println("");
|
---|
68 |
|
---|
69 | jsc.close();
|
---|
70 | }
|
---|
71 |
|
---|
72 | public static void main(String[] args) {
|
---|
73 |
|
---|
74 |
|
---|
75 | Options options = new Options();
|
---|
76 |
|
---|
77 | //.withType(Integer.class)
|
---|
78 |
|
---|
79 | options.addOption(OptionBuilder.withLongOpt("verbosity")
|
---|
80 | .withDescription("Set to control the level of debugging output [0=none, 1=some, 2=lots]")
|
---|
81 | .hasArg()
|
---|
82 | .withArgName("v")
|
---|
83 | .isRequired(false)
|
---|
84 | .create());
|
---|
85 |
|
---|
86 | //Option num_cores_opt = new Option("n", "num-cores", true, "Number of cores to use");
|
---|
87 | //num_cores_opt.setRequired(false);
|
---|
88 | //options.addOption(num_cores_opt);
|
---|
89 |
|
---|
90 | //CommandLineParser parser = new DefaultParser(); // 1.3 and above
|
---|
91 | CommandLineParser parser = new GnuParser();
|
---|
92 | HelpFormatter formatter = new HelpFormatter();
|
---|
93 | CommandLine cmd;
|
---|
94 |
|
---|
95 | try {
|
---|
96 | cmd = parser.parse(options, args);
|
---|
97 | }
|
---|
98 | catch (ParseException e) {
|
---|
99 | System.err.println(e.getMessage());
|
---|
100 | formatter.printHelp("RUN.bash [options] json-file-list.txt input-dir output-dir", options);
|
---|
101 | System.exit(1);
|
---|
102 | return;
|
---|
103 | }
|
---|
104 |
|
---|
105 | //value = ((Integer)cmdLine.getParsedOptionValue("num-cores")).intValue();
|
---|
106 | //value = ((Integer)cmdLine.getOptionValue("num-cores","2")).intValue();
|
---|
107 |
|
---|
108 | //cmd.hasOption("json-filelist")
|
---|
109 | String verbosity_str = cmd.getOptionValue("verbosity","0");
|
---|
110 | int verbosity = Integer.parseInt(verbosity_str);
|
---|
111 |
|
---|
112 | //System.out.println(inputFilePath);
|
---|
113 | //System.out.println(outputFilePath);
|
---|
114 |
|
---|
115 |
|
---|
116 | String[] filtered_args = cmd.getArgs();
|
---|
117 |
|
---|
118 | if (filtered_args.length != 3) {
|
---|
119 | formatter.printHelp("RUN.bash [options] json-filelist.txt input-dir output-dir", options);
|
---|
120 | System.exit(1);
|
---|
121 | }
|
---|
122 | String json_list_filename = filtered_args[0];
|
---|
123 | String input_dir = filtered_args[1];
|
---|
124 | String output_dir = filtered_args[2];
|
---|
125 |
|
---|
126 |
|
---|
127 | //String json_list_filename = cmd.getArgs()[0]; // args[0];
|
---|
128 | //String json_list_filename = args[0];
|
---|
129 | //int num_cores = 2;
|
---|
130 |
|
---|
131 | PrepareForIngest prep_for_ingest = new PrepareForIngest(input_dir,json_list_filename,output_dir,verbosity);
|
---|
132 | prep_for_ingest.exec();
|
---|
133 |
|
---|
134 | }
|
---|
135 | }
|
---|