[31001] | 1 | package org.hathitrust.extractedfeatures;
|
---|
| 2 |
|
---|
[31252] | 3 | import java.io.IOException;
|
---|
[31266] | 4 | import java.util.ArrayList;
|
---|
| 5 | import java.util.Iterator;
|
---|
[31252] | 6 |
|
---|
[31266] | 7 | import org.apache.spark.api.java.function.FlatMapFunction;
|
---|
[31001] | 8 | import org.apache.spark.api.java.function.VoidFunction;
|
---|
| 9 | import org.apache.spark.util.DoubleAccumulator;
|
---|
| 10 | import org.json.JSONArray;
|
---|
| 11 | import org.json.JSONObject;
|
---|
| 12 |
|
---|
| 13 | /*
|
---|
| 14 | class PagedJSON implements Function<String, Boolean> {
|
---|
| 15 |
|
---|
| 16 | private static final long serialVersionUID = 1L;
|
---|
| 17 |
|
---|
| 18 | public Boolean call(String s) { return s.contains("a"); }
|
---|
| 19 | }
|
---|
| 20 | */
|
---|
| 21 |
|
---|
| 22 |
|
---|
[31266] | 23 | //public class PerVolumeJSON implements VoidFunction<String>
|
---|
| 24 | public class PerVolumeJSON implements FlatMapFunction<String,String>
|
---|
[31001] | 25 | {
|
---|
| 26 | private static final long serialVersionUID = 1L;
|
---|
[31005] | 27 | protected String _input_dir;
|
---|
[31225] | 28 | protected String _whitelist_filename;
|
---|
| 29 |
|
---|
[31005] | 30 | protected String _solr_url;
|
---|
| 31 | protected String _output_dir;
|
---|
[31225] | 32 |
|
---|
[31005] | 33 | protected int _verbosity;
|
---|
[31001] | 34 |
|
---|
[31225] | 35 | protected WhitelistBloomFilter _whitelist_bloomfilter;
|
---|
| 36 |
|
---|
[31005] | 37 | protected DoubleAccumulator _progress_accum;
|
---|
| 38 | protected double _progress_step;
|
---|
[31003] | 39 |
|
---|
[31252] | 40 | boolean _icu_tokenize;
|
---|
| 41 | boolean _strict_file_io;
|
---|
| 42 |
|
---|
[31220] | 43 | public PerVolumeJSON(String input_dir, String whitelist_filename,
|
---|
| 44 | String solr_url, String output_dir, int verbosity,
|
---|
[31252] | 45 | DoubleAccumulator progress_accum, double progress_step,
|
---|
| 46 | boolean icu_tokenize, boolean strict_file_io)
|
---|
[31001] | 47 | {
|
---|
[31005] | 48 | _input_dir = input_dir;
|
---|
[31225] | 49 | _whitelist_filename = whitelist_filename;
|
---|
[31220] | 50 |
|
---|
[31005] | 51 | _solr_url = solr_url;
|
---|
| 52 | _output_dir = output_dir;
|
---|
| 53 | _verbosity = verbosity;
|
---|
| 54 |
|
---|
| 55 | _progress_accum = progress_accum;
|
---|
| 56 | _progress_step = progress_step;
|
---|
[31225] | 57 |
|
---|
[31252] | 58 | _icu_tokenize = icu_tokenize;
|
---|
| 59 | _strict_file_io = strict_file_io;
|
---|
| 60 |
|
---|
[31225] | 61 | _whitelist_bloomfilter = null;
|
---|
[31001] | 62 | }
|
---|
[31005] | 63 |
|
---|
[31266] | 64 | //public void call(String json_file_in) throws IOException
|
---|
| 65 | public Iterator<String> call(String json_file_in) throws IOException
|
---|
| 66 |
|
---|
[31001] | 67 | {
|
---|
[31226] | 68 | if ((_whitelist_filename != null) && (_whitelist_bloomfilter == null)) {
|
---|
[31225] | 69 | _whitelist_bloomfilter = new WhitelistBloomFilter(_whitelist_filename,true);
|
---|
| 70 | }
|
---|
| 71 |
|
---|
[31278] | 72 | ArrayList<String> ids = new ArrayList<String>(); // want it to be non-null so can return valid iterator
|
---|
[31266] | 73 |
|
---|
[31252] | 74 | String full_json_file_in = _input_dir + "/" + json_file_in;
|
---|
| 75 | JSONObject extracted_feature_record = JSONClusterFileIO.readJSONFile(full_json_file_in);
|
---|
[31001] | 76 |
|
---|
[31252] | 77 | if (extracted_feature_record != null) {
|
---|
| 78 | String volume_id = extracted_feature_record.getString("id");
|
---|
| 79 |
|
---|
| 80 | //JSONObject ef_metadata = extracted_feature_record.getJSONObject("metadata");
|
---|
| 81 | //String title= ef_metadata.getString("title");
|
---|
| 82 |
|
---|
| 83 | JSONObject ef_features = extracted_feature_record.getJSONObject("features");
|
---|
| 84 |
|
---|
| 85 | int ef_page_count = ef_features.getInt("pageCount");
|
---|
| 86 |
|
---|
| 87 | if (_verbosity >= 1) {
|
---|
| 88 | System.out.println("Processing: " + json_file_in);
|
---|
| 89 | System.out.println(" pageCount = " + ef_page_count);
|
---|
[31001] | 90 | }
|
---|
[31252] | 91 |
|
---|
| 92 | JSONArray ef_pages = ef_features.getJSONArray("pages");
|
---|
| 93 | int ef_num_pages = ef_pages.length();
|
---|
| 94 |
|
---|
| 95 | // Make directory for page-level JSON output
|
---|
| 96 | String json_dir = ClusterFileIO.removeSuffix(json_file_in,".json.bz2");
|
---|
| 97 | String page_json_dir = json_dir + "/pages";
|
---|
| 98 |
|
---|
| 99 | if (_output_dir != null) {
|
---|
| 100 | ClusterFileIO.createDirectoryAll(_output_dir + "/" + page_json_dir);
|
---|
[31001] | 101 | }
|
---|
[31278] | 102 |
|
---|
[31266] | 103 | ids = new ArrayList<String>(ef_num_pages);
|
---|
[31252] | 104 | for (int i = 0; i < ef_page_count; i++) {
|
---|
| 105 | String formatted_i = String.format("page-%06d", i);
|
---|
| 106 | String page_id = volume_id + "." + formatted_i;
|
---|
[31001] | 107 |
|
---|
[31252] | 108 | if (_verbosity >= 2) {
|
---|
| 109 | System.out.println(" Page: " + page_id);
|
---|
[31001] | 110 | }
|
---|
[31252] | 111 |
|
---|
| 112 | String output_json_bz2 = page_json_dir +"/" + formatted_i + ".json.bz2";
|
---|
[31266] | 113 | ids.add(page_id);
|
---|
[31252] | 114 |
|
---|
[31269] | 115 | if (_verbosity >=2) {
|
---|
| 116 | if (i==0) {
|
---|
| 117 | System.out.println("Sample output JSON page file [i=0]: " + output_json_bz2);
|
---|
| 118 | }
|
---|
[31001] | 119 | }
|
---|
[31252] | 120 | JSONObject ef_page = ef_pages.getJSONObject(i);
|
---|
| 121 |
|
---|
| 122 | if (ef_page != null) {
|
---|
| 123 | // Convert to Solr add form
|
---|
| 124 | JSONObject solr_add_doc_json
|
---|
| 125 | = SolrDocJSON.generateSolrDocJSON(volume_id, page_id, ef_page, _whitelist_bloomfilter, _icu_tokenize);
|
---|
| 126 |
|
---|
| 127 |
|
---|
[31001] | 128 | if ((_verbosity >=2) && (i==20)) {
|
---|
| 129 | System.out.println("==================");
|
---|
[31252] | 130 | System.out.println("Sample output Solr add JSON [page 20]: " + solr_add_doc_json.toString());
|
---|
[31001] | 131 | System.out.println("==================");
|
---|
| 132 | }
|
---|
[31252] | 133 |
|
---|
| 134 |
|
---|
| 135 | if (_solr_url != null) {
|
---|
| 136 | if ((_verbosity >=2) && (i==20)) {
|
---|
| 137 | System.out.println("==================");
|
---|
| 138 | System.out.println("Posting to: " + _solr_url);
|
---|
| 139 | System.out.println("==================");
|
---|
| 140 | }
|
---|
| 141 | SolrDocJSON.postSolrDoc(_solr_url, solr_add_doc_json);
|
---|
| 142 | }
|
---|
| 143 |
|
---|
| 144 | if (_output_dir != null) {
|
---|
| 145 | if ((_verbosity >=2) && (i==20)) {
|
---|
| 146 | System.out.println("==================");
|
---|
| 147 | System.out.println("Saving to: " + _output_dir);
|
---|
| 148 | System.out.println("==================");
|
---|
| 149 | }
|
---|
| 150 | SolrDocJSON.saveSolrDoc(solr_add_doc_json, _output_dir + "/" + output_json_bz2);
|
---|
| 151 | }
|
---|
[31001] | 152 | }
|
---|
[31252] | 153 | else {
|
---|
| 154 | System.err.println("Skipping: " + page_id);
|
---|
| 155 | }
|
---|
| 156 |
|
---|
[31001] | 157 | }
|
---|
[31252] | 158 | }
|
---|
| 159 | else {
|
---|
| 160 | // File did not exist, or could not be parsed
|
---|
| 161 | String mess = "Failed to read in bzipped JSON file '" + full_json_file_in + "'";
|
---|
| 162 | if (_strict_file_io) {
|
---|
| 163 | throw new IOException(mess);
|
---|
| 164 | }
|
---|
[31001] | 165 | else {
|
---|
[31252] | 166 | System.err.println("Warning: " + mess);
|
---|
| 167 | System.out.println("Warning: " + mess);
|
---|
[31001] | 168 | }
|
---|
| 169 | }
|
---|
| 170 |
|
---|
| 171 | //ids.add(volume_id);
|
---|
| 172 | _progress_accum.add(_progress_step);
|
---|
| 173 |
|
---|
[31266] | 174 | return ids.iterator();
|
---|
[31001] | 175 | }
|
---|
| 176 | }
|
---|
| 177 |
|
---|