source: other-projects/hathitrust/solr-extracted-features/trunk/src/main/java/org/hathitrust/PagedJSON.java@ 30970

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

Added in mapping of EF-JSON to Solr 'add' JSON format

  • Property svn:executable set to *
File size: 8.4 KB
Line 
1package org.hathitrust;
2
3import java.io.BufferedReader;
4import java.io.BufferedWriter;
5import java.io.IOException;
6import java.io.OutputStream;
7import java.net.HttpURLConnection;
8import java.net.URL;
9import java.util.ArrayList;
10import java.util.Iterator;
11import java.util.Set;
12
13import org.apache.commons.compress.compressors.CompressorException;
14import org.apache.spark.api.java.function.FlatMapFunction;
15import org.json.JSONArray;
16import org.json.JSONObject;
17
18/*
19class PagedJSON implements Function<String, Boolean> {
20
21 private static final long serialVersionUID = 1L;
22
23 public Boolean call(String s) { return s.contains("a"); }
24}
25 */
26
27
28class PagedJSON implements FlatMapFunction<String, String>
29{
30 private static final long serialVersionUID = 1L;
31
32 protected String _input_dir;
33 protected String _output_dir;
34 protected int _verbosity;
35
36 public PagedJSON(String input_dir, String output_dir, int verbosity)
37 {
38 _input_dir = input_dir;
39 _output_dir = output_dir;
40 _verbosity = verbosity;
41 }
42
43 protected JSONObject readJSONFile(String filename)
44 {
45 StringBuilder sb = new StringBuilder();
46
47 try {
48
49 String str;
50 BufferedReader br = ClusterFileIO.getBufferedReaderForCompressedFile(_input_dir + "/" + filename);
51 while ((str = br.readLine()) != null) {
52 sb.append(str);
53 }
54
55 br.close();
56 }
57 catch (Exception e) {
58 e.printStackTrace();
59 }
60
61 JSONObject json_obj = new JSONObject(sb.toString());
62
63
64 return json_obj;
65 }
66
67 protected String generateSolrText(JSONObject ef_token_pos_count)
68 {
69 StringBuilder sb = new StringBuilder();
70
71 Iterator<String> token_iter = ef_token_pos_count.keys();
72 while (token_iter.hasNext()) {
73 String token = token_iter.next();
74
75 sb.append(token);
76 if (token_iter.hasNext()) {
77 sb.append(" ");
78 }
79 }
80
81 /*
82 Set<String> token_keys = ef_token_pos_count.keySet();
83 for (String token : token_keys) {
84 sb.append(token + " ");
85 }
86*/
87
88 return sb.toString();
89 }
90
91 protected JSONObject generateSolrDocJSON(String volume_id, String page_id, JSONObject ef_page)
92 {
93 JSONObject solr_add_json = null;
94
95 if (ef_page != null) {
96 JSONObject ef_body = ef_page.getJSONObject("body");
97 if (ef_body != null) {
98 JSONObject ef_token_pos_count = ef_body.getJSONObject("tokenPosCount");
99 if (ef_token_pos_count != null) {
100
101 solr_add_json = new JSONObject();
102
103 String text = generateSolrText(ef_token_pos_count);
104
105
106
107 JSONObject solr_doc_json = new JSONObject();
108 solr_doc_json.put("id", page_id);
109 solr_doc_json.put("volumeid", volume_id);
110 solr_doc_json.put("text_t", text);
111 solr_doc_json.put("commitWithin", 5000);
112
113 solr_add_json.put("add", solr_doc_json);
114 }
115 else {
116 System.err.println("Warning: empty tokenPosCount field for '" + page_id + "'");
117 }
118 }
119 else {
120 System.err.println("Warning: empty body field for '" + page_id + "'");
121 }
122
123 }
124 else {
125 System.err.println("Warning: null page for '" + page_id + "'");
126 }
127
128
129 /*
130
131 /update/json/docs
132 */
133
134 // For Reference ...
135 // Example documentation on Solr JSON syntax:
136 // https://cwiki.apache.org/confluence/display/solr/Uploading+Data+with+Index+Handlers
137 // #UploadingDatawithIndexHandlers-JSONFormattedIndexUpdates
138
139 /*
140 curl -X POST -H 'Content-Type: application/json' 'http://localhost:8983/solr/my_collection/update' --data-binary '
141 {
142 "add": {
143 "doc": {
144 "id": "DOC1",
145 "my_boosted_field": { use a map with boost/value for a boosted field
146 "boost": 2.3,
147 "value": "test"
148 },
149 "my_multivalued_field": [ "aaa", "bbb" ] Can use an array for a multi-valued field
150 }
151 },
152 "add": {
153 "commitWithin": 5000, commit this document within 5 seconds
154 "overwrite": false, don't check for existing documents with the same uniqueKey
155 "boost": 3.45, a document boost
156 "doc": {
157 "f1": "v1", Can use repeated keys for a multi-valued field
158 "f1": "v2"
159 }
160 },
161
162 "commit": {},
163 "optimize": { "waitSearcher":false },
164
165 "delete": { "id":"ID" }, delete by ID
166 "delete": { "query":"QUERY" } delete by query
167 }'
168 */
169
170 //return solr_doc_json;
171 return solr_add_json;
172 }
173
174 protected void postSolrDoc(JSONObject solr_add_doc_json)
175 {
176 // "http://10.11.0.53:8983/solr/"
177 String post_url = "http://10.11.0.53:8983/solr/htrc-pd-ef/update";
178
179 //String curl_popen = "curl -X POST -H 'Content-Type: application/json'";
180 //curl_popen += " 'http://10.11.0.53:8983/solr/htrc-pd-ef/update'";
181 //curl_popen += " --data-binary '";
182 //curl_popen += "'"
183
184
185 try {
186 HttpURLConnection httpcon = (HttpURLConnection) ((new URL(post_url).openConnection()));
187 httpcon.setDoOutput(true);
188 httpcon.setRequestProperty("Content-Type", "application/json");
189 httpcon.setRequestProperty("Accept", "application/json");
190 httpcon.setRequestMethod("POST");
191 httpcon.connect();
192
193 byte[] outputBytes = solr_add_doc_json.toString().getBytes("UTF-8");
194 OutputStream os = httpcon.getOutputStream();
195 os.write(outputBytes);
196 os.close();
197 }
198 catch (Exception e) {
199 e.printStackTrace();
200 }
201
202 }
203 public Iterator<String> call(String json_file_in)
204 {
205 JSONObject extracted_feature_record = readJSONFile(json_file_in);
206
207 // Check output directory for volume exists, and create it if not
208
209
210 String volume_id = extracted_feature_record.getString("id");
211
212 //JSONObject ef_metadata = extracted_feature_record.getJSONObject("metadata");
213 //String title= ef_metadata.getString("title");
214
215 JSONObject ef_features = extracted_feature_record.getJSONObject("features");
216
217
218 int ef_page_count = ef_features.getInt("pageCount");
219
220 if (_verbosity >= 1) {
221 System.out.println("Processing: " + json_file_in);
222 System.out.println(" pageCount = " + ef_page_count);
223 }
224
225 JSONArray ef_pages = ef_features.getJSONArray("pages");
226 int ef_num_pages = ef_pages.length();
227
228 // Make directory for page-level JSON output
229 String json_dir = ClusterFileIO.removeSuffix(json_file_in,".json.bz2");
230 String page_json_dir = json_dir + "/pages";
231 ClusterFileIO.createDirectoryAll(_output_dir + "/" + page_json_dir);
232
233 ArrayList<String> ids = new ArrayList<String>(ef_num_pages);
234 for (int i = 0; i < ef_page_count; i++) {
235 String formatted_i = String.format("page-%06d", i);
236 String page_id = volume_id + "." + formatted_i;
237
238 if (_verbosity >= 2) {
239 System.out.println(" Page: " + page_id);
240 }
241
242 String output_json_bz2 = page_json_dir +"/" + formatted_i + ".json.bz2";
243 ids.add(output_json_bz2);
244
245 if (i==0) {
246 System.out.println("Sample output JSON page file: " + output_json_bz2);
247 }
248
249 JSONObject ef_page = ef_pages.getJSONObject(i);
250
251 if (ef_page != null) {
252 // Convert to Solr add form
253 JSONObject solr_add_doc_json = generateSolrDocJSON(volume_id, page_id, ef_page);
254
255 if (i==20) {
256 System.out.println("Sample output Solr add JSON [page 20]: " + solr_add_doc_json.toString());
257 System.out.println("==================");
258 //System.out.println("Sample text [page 20]: " + solr_add_doc_json.getString("text_t"));
259 }
260
261 // create JSON obj of just the page (for now), and write it out
262 // write out the JSONOBject as a bz2 compressed file
263 /*
264 try {
265 BufferedWriter bw = ClusterFileIO.getBufferedWriterForCompressedFile(_output_dir + "/" + output_json_bz2);
266 bw.write(ef_page.toString());
267 bw.close();
268 } catch (IOException e) {
269 e.printStackTrace();
270 } catch (CompressorException e) {
271 e.printStackTrace();
272 }
273 */
274
275 //postSolrDoc(solr_add_doc_json);
276
277 }
278 else {
279 System.err.println("Skipping: " + page_id);
280 }
281
282 }
283
284 /*
285 for (int i = 0; i < ef_num_pages; i++)
286 {
287 //String post_id = ef_pages.getJSONObject(i).getString("post_id");
288 //......
289 }
290 */
291 //String pageName = json_obj.getJSONObject("pageInfo").getString("pageName");
292/*
293 JSONArray arr = obj.getJSONArray("posts");
294 for (int i = 0; i < arr.length(); i++)
295 {
296 String post_id = arr.getJSONObject(i).getString("post_id");
297 ......
298 }
299*/
300
301
302 ids.add(volume_id);
303
304 return ids.iterator();
305 }
306}
307
Note: See TracBrowser for help on using the repository browser.