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