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