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

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

shift to using solr-base-url and a specified solr-collection

  • Property svn:executable set to *
File size: 8.3 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) && (_whitelist_bloomfilter == null)) {
77 _whitelist_bloomfilter = new WhitelistBloomFilter(_whitelist_filename,true);
78 }
79
80 if ((_langmap_directory != null) && (_universal_langmap == null)) {
81 _universal_langmap = new UniversalPOSLangMap(_langmap_directory);
82 }
83 int ef_num_pages = 0;
84
85 String solr_url = null;
86 if (_solr_endpoints_len > 0) {
87 int random_choice = (int)(_solr_endpoints_len * Math.random());
88 solr_url = _solr_endpoints.get(random_choice);
89 }
90
91 try {
92
93
94 JSONObject extracted_feature_record = new JSONObject(json_text.toString());
95
96 if (extracted_feature_record != null) {
97 String volume_id = extracted_feature_record.getString("id");
98
99 //JSONObject ef_metadata = extracted_feature_record.getJSONObject("metadata");
100 //String title= ef_metadata.getString("title");
101
102 JSONObject ef_features = extracted_feature_record.getJSONObject("features");
103
104 int ef_page_count = ef_features.getInt("pageCount");
105
106 if (_verbosity >= 1) {
107 System.out.println("Processing: " + volume_id);
108 System.out.println(" pageCount = " + ef_page_count);
109 }
110
111 JSONArray ef_pages = ef_features.getJSONArray("pages");
112 ef_num_pages = ef_pages.length();
113
114
115 for (int i = 0; i < ef_page_count; i++) {
116 String formatted_i = String.format("page-%06d", i);
117 String page_id = volume_id + "." + formatted_i;
118
119 if (_verbosity >= 2) {
120 System.out.println(" Page: " + page_id);
121 }
122
123
124 JSONObject ef_page = ef_pages.getJSONObject(i);
125
126 if (ef_page != null) {
127 // Convert to Solr add form
128 JSONObject solr_add_doc_json
129 = SolrDocJSON.generateSolrDocJSON(volume_id, page_id, ef_page, _whitelist_bloomfilter, _universal_langmap, _icu_tokenize);
130
131
132 if ((_verbosity >=2) && (i==20)) {
133 System.out.println("==================");
134 System.out.println("Sample output Solr add JSON [page 20]: " + solr_add_doc_json.toString());
135 System.out.println("==================");
136 }
137
138
139 if (solr_url != null) {
140 if ((_verbosity >=2) && (i==20)) {
141 System.out.println("==================");
142 System.out.println("Posting to: " + solr_url);
143 System.out.println("==================");
144 }
145 SolrDocJSON.postSolrDoc(solr_url, solr_add_doc_json);
146 }
147
148
149 }
150 else {
151 System.err.println("Skipping: " + page_id);
152 }
153
154 }
155 }
156 }
157 catch (Exception e) {
158 if (_strict_file_io) {
159 throw e;
160 }
161 else {
162 e.printStackTrace();
163 }
164 }
165
166 return ef_num_pages;
167
168 }
169
170 /*
171 //public void call(String json_file_in) throws IOException
172 public Integer call(String json_file_in) throws IOException
173
174 {
175 if ((_whitelist_filename != null) && (_whitelist_bloomfilter == null)) {
176 _whitelist_bloomfilter = new WhitelistBloomFilter(_whitelist_filename,true);
177 }
178
179 int ef_num_pages = 0;
180
181 ArrayList<String> ids = new ArrayList<String>(); // want it to be non-null so can return valid iterator
182
183 String full_json_file_in = _input_dir + "/" + json_file_in;
184 JSONObject extracted_feature_record = JSONClusterFileIO.readJSONFile(full_json_file_in);
185
186 if (extracted_feature_record != null) {
187 String volume_id = extracted_feature_record.getString("id");
188
189 //JSONObject ef_metadata = extracted_feature_record.getJSONObject("metadata");
190 //String title= ef_metadata.getString("title");
191
192 JSONObject ef_features = extracted_feature_record.getJSONObject("features");
193
194 int ef_page_count = ef_features.getInt("pageCount");
195
196 if (_verbosity >= 1) {
197 System.out.println("Processing: " + json_file_in);
198 System.out.println(" pageCount = " + ef_page_count);
199 }
200
201 JSONArray ef_pages = ef_features.getJSONArray("pages");
202 ef_num_pages = ef_pages.length();
203
204 // Make directory for page-level JSON output
205 String json_dir = ClusterFileIO.removeSuffix(json_file_in,".json.bz2");
206 String page_json_dir = json_dir + "/pages";
207
208 if (_output_dir != null) {
209 ClusterFileIO.createDirectoryAll(_output_dir + "/" + page_json_dir);
210 }
211
212 ids = new ArrayList<String>(ef_num_pages);
213 for (int i = 0; i < ef_page_count; i++) {
214 String formatted_i = String.format("page-%06d", i);
215 String page_id = volume_id + "." + formatted_i;
216
217 if (_verbosity >= 2) {
218 System.out.println(" Page: " + page_id);
219 }
220
221 String output_json_bz2 = page_json_dir +"/" + formatted_i + ".json.bz2";
222 ids.add(page_id);
223
224 if (_verbosity >=2) {
225 if (i==0) {
226 System.out.println("Sample output JSON page file [i=0]: " + output_json_bz2);
227 }
228 }
229 JSONObject ef_page = ef_pages.getJSONObject(i);
230
231 if (ef_page != null) {
232 // Convert to Solr add form
233 JSONObject solr_add_doc_json
234 = SolrDocJSON.generateSolrDocJSON(volume_id, page_id, ef_page, _whitelist_bloomfilter, _icu_tokenize);
235
236
237 if ((_verbosity >=2) && (i==20)) {
238 System.out.println("==================");
239 System.out.println("Sample output Solr add JSON [page 20]: " + solr_add_doc_json.toString());
240 System.out.println("==================");
241 }
242
243
244 if (_solr_url != null) {
245 if ((_verbosity >=2) && (i==20)) {
246 System.out.println("==================");
247 System.out.println("Posting to: " + _solr_url);
248 System.out.println("==================");
249 }
250 SolrDocJSON.postSolrDoc(_solr_url, solr_add_doc_json);
251 }
252
253 if (_output_dir != null) {
254 if ((_verbosity >=2) && (i==20)) {
255 System.out.println("==================");
256 System.out.println("Saving to: " + _output_dir);
257 System.out.println("==================");
258 }
259 SolrDocJSON.saveSolrDoc(solr_add_doc_json, _output_dir + "/" + output_json_bz2);
260 }
261 }
262 else {
263 System.err.println("Skipping: " + page_id);
264 }
265
266 }
267 }
268 else {
269 // File did not exist, or could not be parsed
270 String mess = "Failed to read in bzipped JSON file '" + full_json_file_in + "'";
271 if (_strict_file_io) {
272 throw new IOException(mess);
273 }
274 else {
275 System.err.println("Warning: " + mess);
276 System.out.println("Warning: " + mess);
277 }
278 }
279
280 return ef_num_pages;
281
282 }
283 */
284}
285
Note: See TracBrowser for help on using the repository browser.