source: other-projects/hathitrust/wcsa/extracted-features-solr/trunk/solr-ingest/src/main/java/org/hathitrust/extractedfeatures/PerVolumeJSON.java@ 31500

Last change on this file since 31500 was 31500, checked in by davidb, 7 years ago

Synchronize on reading in of white-list and universal-lang-pos

  • Property svn:executable set to *
File size: 8.5 KB
Line 
1package org.hathitrust.extractedfeatures;
2
3import java.io.IOException;
4import java.util.ArrayList;
5import java.util.HashMap;
6import java.util.Iterator;
7
8import org.apache.hadoop.io.Text;
9import org.apache.spark.api.java.function.FlatMapFunction;
10import org.apache.spark.api.java.function.Function;
11import org.apache.spark.api.java.function.VoidFunction;
12import org.apache.spark.util.DoubleAccumulator;
13import org.json.JSONArray;
14import org.json.JSONObject;
15
16/*
17class PagedJSON implements Function<String, Boolean> {
18
19 private static final long serialVersionUID = 1L;
20
21 public Boolean call(String s) { return s.contains("a"); }
22}
23 */
24
25
26//public class PerVolumeJSON implements VoidFunction<String>
27public class PerVolumeJSON implements Function<Text,Integer>
28{
29 private static final long serialVersionUID = 1L;
30 protected String _input_dir;
31 protected String _whitelist_filename;
32 protected String _langmap_directory;
33
34 protected final ArrayList<String> _solr_endpoints;
35 protected final int _solr_endpoints_len;
36
37 //protected String _solr_url;
38 protected String _output_dir;
39
40 protected int _verbosity;
41
42 protected WhitelistBloomFilter _whitelist_bloomfilter;
43 protected UniversalPOSLangMap _universal_langmap;
44
45 boolean _icu_tokenize;
46 boolean _strict_file_io;
47
48 public PerVolumeJSON(String input_dir, String whitelist_filename, String langmap_directory,
49 ArrayList<String> solr_endpoints, String output_dir, int verbosity,
50 boolean icu_tokenize, boolean strict_file_io)
51 {
52 System.out.println("*** PerVolumeJSON Constructor, langmap_directory = " + langmap_directory);
53
54 _input_dir = input_dir;
55 _whitelist_filename = whitelist_filename;
56 _langmap_directory = langmap_directory;
57
58 _solr_endpoints = solr_endpoints;
59 _solr_endpoints_len = solr_endpoints.size();
60
61 //_solr_url = solr_url;
62 _output_dir = output_dir;
63 _verbosity = verbosity;
64
65 _icu_tokenize = icu_tokenize;
66 _strict_file_io = strict_file_io;
67
68 _whitelist_bloomfilter = null;
69 _universal_langmap = null;
70 }
71
72
73 public Integer call(Text json_text) throws IOException
74
75 {
76 if (_whitelist_filename != null) {
77
78 synchronized (_whitelist_filename) {
79 if (_whitelist_bloomfilter == null) {
80
81 _whitelist_bloomfilter = new WhitelistBloomFilter(_whitelist_filename,true);
82 }
83 }
84 }
85
86 if (_langmap_directory != null) {
87
88 synchronized (_langmap_directory) {
89 if (_universal_langmap == null) {
90 _universal_langmap = new UniversalPOSLangMap(_langmap_directory);
91 }
92 }
93 }
94
95 int ef_num_pages = 0;
96
97 String solr_url = null;
98 if (_solr_endpoints_len > 0) {
99 int random_choice = (int)(_solr_endpoints_len * Math.random());
100 solr_url = _solr_endpoints.get(random_choice);
101 }
102
103 try {
104
105
106 JSONObject extracted_feature_record = new JSONObject(json_text.toString());
107
108 if (extracted_feature_record != null) {
109 String volume_id = extracted_feature_record.getString("id");
110
111 //JSONObject ef_metadata = extracted_feature_record.getJSONObject("metadata");
112 //String title= ef_metadata.getString("title");
113
114 JSONObject ef_features = extracted_feature_record.getJSONObject("features");
115
116 int ef_page_count = ef_features.getInt("pageCount");
117
118 if (_verbosity >= 1) {
119 System.out.println("Processing: " + volume_id);
120 System.out.println(" pageCount = " + ef_page_count);
121 }
122
123 JSONArray ef_pages = ef_features.getJSONArray("pages");
124 ef_num_pages = ef_pages.length();
125
126
127 for (int i = 0; i < ef_page_count; i++) {
128 String formatted_i = String.format("page-%06d", i);
129 String page_id = volume_id + "." + formatted_i;
130
131 if (_verbosity >= 2) {
132 System.out.println(" Page: " + page_id);
133 }
134
135
136 JSONObject ef_page = ef_pages.getJSONObject(i);
137
138 if (ef_page != null) {
139 // Convert to Solr add form
140 JSONObject solr_add_doc_json
141 = SolrDocJSON.generateSolrDocJSON(volume_id, page_id, ef_page, _whitelist_bloomfilter, _universal_langmap, _icu_tokenize);
142
143
144 if ((_verbosity >=2) && (i==20)) {
145 System.out.println("==================");
146 System.out.println("Sample output Solr add JSON [page 20]: " + solr_add_doc_json.toString());
147 System.out.println("==================");
148 }
149
150
151 if (solr_url != null) {
152 if ((_verbosity >=2) && (i==20)) {
153 System.out.println("==================");
154 System.out.println("Posting to: " + solr_url);
155 System.out.println("==================");
156 }
157 SolrDocJSON.postSolrDoc(solr_url, solr_add_doc_json,
158 volume_id, page_id);
159 }
160
161
162 }
163 else {
164 System.err.println("Skipping: " + page_id);
165 }
166
167 }
168 }
169 }
170 catch (Exception e) {
171 if (_strict_file_io) {
172 throw e;
173 }
174 else {
175 e.printStackTrace();
176 }
177 }
178
179 return ef_num_pages;
180
181 }
182
183 /*
184 //public void call(String json_file_in) throws IOException
185 public Integer call(String json_file_in) throws IOException
186
187 {
188 if ((_whitelist_filename != null) && (_whitelist_bloomfilter == null)) {
189 _whitelist_bloomfilter = new WhitelistBloomFilter(_whitelist_filename,true);
190 }
191
192 int ef_num_pages = 0;
193
194 ArrayList<String> ids = new ArrayList<String>(); // want it to be non-null so can return valid iterator
195
196 String full_json_file_in = _input_dir + "/" + json_file_in;
197 JSONObject extracted_feature_record = JSONClusterFileIO.readJSONFile(full_json_file_in);
198
199 if (extracted_feature_record != null) {
200 String volume_id = extracted_feature_record.getString("id");
201
202 //JSONObject ef_metadata = extracted_feature_record.getJSONObject("metadata");
203 //String title= ef_metadata.getString("title");
204
205 JSONObject ef_features = extracted_feature_record.getJSONObject("features");
206
207 int ef_page_count = ef_features.getInt("pageCount");
208
209 if (_verbosity >= 1) {
210 System.out.println("Processing: " + json_file_in);
211 System.out.println(" pageCount = " + ef_page_count);
212 }
213
214 JSONArray ef_pages = ef_features.getJSONArray("pages");
215 ef_num_pages = ef_pages.length();
216
217 // Make directory for page-level JSON output
218 String json_dir = ClusterFileIO.removeSuffix(json_file_in,".json.bz2");
219 String page_json_dir = json_dir + "/pages";
220
221 if (_output_dir != null) {
222 ClusterFileIO.createDirectoryAll(_output_dir + "/" + page_json_dir);
223 }
224
225 ids = new ArrayList<String>(ef_num_pages);
226 for (int i = 0; i < ef_page_count; i++) {
227 String formatted_i = String.format("page-%06d", i);
228 String page_id = volume_id + "." + formatted_i;
229
230 if (_verbosity >= 2) {
231 System.out.println(" Page: " + page_id);
232 }
233
234 String output_json_bz2 = page_json_dir +"/" + formatted_i + ".json.bz2";
235 ids.add(page_id);
236
237 if (_verbosity >=2) {
238 if (i==0) {
239 System.out.println("Sample output JSON page file [i=0]: " + output_json_bz2);
240 }
241 }
242 JSONObject ef_page = ef_pages.getJSONObject(i);
243
244 if (ef_page != null) {
245 // Convert to Solr add form
246 JSONObject solr_add_doc_json
247 = SolrDocJSON.generateSolrDocJSON(volume_id, page_id, ef_page, _whitelist_bloomfilter, _icu_tokenize);
248
249
250 if ((_verbosity >=2) && (i==20)) {
251 System.out.println("==================");
252 System.out.println("Sample output Solr add JSON [page 20]: " + solr_add_doc_json.toString());
253 System.out.println("==================");
254 }
255
256
257 if (_solr_url != null) {
258 if ((_verbosity >=2) && (i==20)) {
259 System.out.println("==================");
260 System.out.println("Posting to: " + _solr_url);
261 System.out.println("==================");
262 }
263 SolrDocJSON.postSolrDoc(_solr_url, solr_add_doc_json);
264 }
265
266 if (_output_dir != null) {
267 if ((_verbosity >=2) && (i==20)) {
268 System.out.println("==================");
269 System.out.println("Saving to: " + _output_dir);
270 System.out.println("==================");
271 }
272 SolrDocJSON.saveSolrDoc(solr_add_doc_json, _output_dir + "/" + output_json_bz2);
273 }
274 }
275 else {
276 System.err.println("Skipping: " + page_id);
277 }
278
279 }
280 }
281 else {
282 // File did not exist, or could not be parsed
283 String mess = "Failed to read in bzipped JSON file '" + full_json_file_in + "'";
284 if (_strict_file_io) {
285 throw new IOException(mess);
286 }
287 else {
288 System.err.println("Warning: " + mess);
289 System.out.println("Warning: " + mess);
290 }
291 }
292
293 return ef_num_pages;
294
295 }
296 */
297}
298
Note: See TracBrowser for help on using the repository browser.