1 | package org.hathitrust.extractedfeatures;
|
---|
2 |
|
---|
3 | import java.io.BufferedReader;
|
---|
4 | import java.io.BufferedWriter;
|
---|
5 | import java.io.IOException;
|
---|
6 | import java.io.InputStreamReader;
|
---|
7 | import java.io.OutputStream;
|
---|
8 | import java.net.HttpURLConnection;
|
---|
9 | import java.net.URL;
|
---|
10 | import java.util.ArrayList;
|
---|
11 | import java.util.Set;
|
---|
12 |
|
---|
13 | import org.apache.commons.compress.compressors.CompressorException;
|
---|
14 | import org.apache.spark.api.java.function.FlatMapFunction;
|
---|
15 | import org.apache.spark.api.java.function.VoidFunction;
|
---|
16 | import org.apache.spark.util.DoubleAccumulator;
|
---|
17 | import org.json.JSONArray;
|
---|
18 | import org.json.JSONObject;
|
---|
19 |
|
---|
20 | /*
|
---|
21 | class PagedJSON implements Function<String, Boolean> {
|
---|
22 |
|
---|
23 | private static final long serialVersionUID = 1L;
|
---|
24 |
|
---|
25 | public Boolean call(String s) { return s.contains("a"); }
|
---|
26 | }
|
---|
27 | */
|
---|
28 |
|
---|
29 |
|
---|
30 | //class PagedJSON implements FlatMapFunction<String, String>
|
---|
31 | public class PagedJSON implements VoidFunction<String>
|
---|
32 | {
|
---|
33 | private static final long serialVersionUID = 1L;
|
---|
34 |
|
---|
35 | protected String _input_dir;
|
---|
36 | protected String _solr_url;
|
---|
37 | protected String _output_dir;
|
---|
38 | protected int _verbosity;
|
---|
39 |
|
---|
40 | DoubleAccumulator _progress_accum;
|
---|
41 | double _progress_step;
|
---|
42 |
|
---|
43 | public PagedJSON(String input_dir, String solr_url, String output_dir, int verbosity,
|
---|
44 | DoubleAccumulator progress_accum, double progress_step)
|
---|
45 | {
|
---|
46 | _input_dir = input_dir;
|
---|
47 | _solr_url = solr_url;
|
---|
48 | _output_dir = output_dir;
|
---|
49 | _verbosity = verbosity;
|
---|
50 |
|
---|
51 | _progress_accum = progress_accum;
|
---|
52 | _progress_step = progress_step;
|
---|
53 | }
|
---|
54 |
|
---|
55 | public static void saveSolrDoc(JSONObject solr_add_doc_json, String output_file_json_bz2)
|
---|
56 | {
|
---|
57 | try {
|
---|
58 | BufferedWriter bw = ClusterFileIO.getBufferedWriterForCompressedFile(output_file_json_bz2);
|
---|
59 | bw.write(solr_add_doc_json.toString());
|
---|
60 | bw.close();
|
---|
61 | } catch (IOException e) {
|
---|
62 | e.printStackTrace();
|
---|
63 | } catch (CompressorException e) {
|
---|
64 | e.printStackTrace();
|
---|
65 | }
|
---|
66 | }
|
---|
67 |
|
---|
68 | public static void postSolrDoc(String post_url, JSONObject solr_add_doc_json)
|
---|
69 | {
|
---|
70 |
|
---|
71 | //String curl_popen = "curl -X POST -H 'Content-Type: application/json'";
|
---|
72 | //curl_popen += " 'http://10.11.0.53:8983/solr/htrc-pd-ef/update'";
|
---|
73 | //curl_popen += " --data-binary '";
|
---|
74 | //curl_popen += "'"
|
---|
75 |
|
---|
76 |
|
---|
77 | try {
|
---|
78 | HttpURLConnection httpcon = (HttpURLConnection) ((new URL(post_url).openConnection()));
|
---|
79 | httpcon.setDoOutput(true);
|
---|
80 | httpcon.setRequestProperty("Content-Type", "application/json");
|
---|
81 | httpcon.setRequestProperty("Accept", "application/json");
|
---|
82 | httpcon.setRequestMethod("POST");
|
---|
83 | httpcon.connect();
|
---|
84 |
|
---|
85 | byte[] outputBytes = solr_add_doc_json.toString().getBytes("UTF-8");
|
---|
86 | OutputStream os = httpcon.getOutputStream();
|
---|
87 | os.write(outputBytes);
|
---|
88 | os.close();
|
---|
89 |
|
---|
90 |
|
---|
91 | // Read response
|
---|
92 | StringBuilder sb = new StringBuilder();
|
---|
93 | BufferedReader in = new BufferedReader(new InputStreamReader(httpcon.getInputStream()));
|
---|
94 | String decodedString;
|
---|
95 | while ((decodedString = in.readLine()) != null) {
|
---|
96 | sb.append(decodedString);
|
---|
97 | }
|
---|
98 | in.close();
|
---|
99 |
|
---|
100 | JSONObject solr_status_json = new JSONObject(sb.toString());
|
---|
101 | JSONObject response_header_json = solr_status_json.getJSONObject("responseHeader");
|
---|
102 | if (response_header_json != null) {
|
---|
103 | int status = response_header_json.getInt("status");
|
---|
104 | if (status != 0) {
|
---|
105 | System.err.println("Warning: POST request to " + post_url + " returned status " + status);
|
---|
106 | System.err.println("Full response was: " + sb);
|
---|
107 | }
|
---|
108 | }
|
---|
109 | else {
|
---|
110 | System.err.println("Failed response to Solr POST: " + sb);
|
---|
111 | }
|
---|
112 |
|
---|
113 |
|
---|
114 |
|
---|
115 | }
|
---|
116 | catch (Exception e) {
|
---|
117 | e.printStackTrace();
|
---|
118 | }
|
---|
119 |
|
---|
120 | }
|
---|
121 |
|
---|
122 | //public Iterator<String> call(String json_file_in)
|
---|
123 | public void call(String json_file_in)
|
---|
124 | {
|
---|
125 | JSONObject extracted_feature_record = JSONClusterFileIO.readJSONFile(_input_dir + "/" + json_file_in);
|
---|
126 |
|
---|
127 | String volume_id = extracted_feature_record.getString("id");
|
---|
128 |
|
---|
129 | //JSONObject ef_metadata = extracted_feature_record.getJSONObject("metadata");
|
---|
130 | //String title= ef_metadata.getString("title");
|
---|
131 |
|
---|
132 | JSONObject ef_features = extracted_feature_record.getJSONObject("features");
|
---|
133 |
|
---|
134 |
|
---|
135 | int ef_page_count = ef_features.getInt("pageCount");
|
---|
136 |
|
---|
137 | if (_verbosity >= 1) {
|
---|
138 | System.out.println("Processing: " + json_file_in);
|
---|
139 | System.out.println(" pageCount = " + ef_page_count);
|
---|
140 | }
|
---|
141 |
|
---|
142 | JSONArray ef_pages = ef_features.getJSONArray("pages");
|
---|
143 | int ef_num_pages = ef_pages.length();
|
---|
144 |
|
---|
145 | // Make directory for page-level JSON output
|
---|
146 | String json_dir = ClusterFileIO.removeSuffix(json_file_in,".json.bz2");
|
---|
147 | String page_json_dir = json_dir + "/pages";
|
---|
148 | ClusterFileIO.createDirectoryAll(_output_dir + "/" + page_json_dir);
|
---|
149 |
|
---|
150 | ArrayList<String> ids = new ArrayList<String>(ef_num_pages);
|
---|
151 | for (int i = 0; i < ef_page_count; i++) {
|
---|
152 | String formatted_i = String.format("page-%06d", i);
|
---|
153 | String page_id = volume_id + "." + formatted_i;
|
---|
154 |
|
---|
155 | if (_verbosity >= 2) {
|
---|
156 | System.out.println(" Page: " + page_id);
|
---|
157 | }
|
---|
158 |
|
---|
159 | String output_json_bz2 = page_json_dir +"/" + formatted_i + ".json.bz2";
|
---|
160 | ids.add(output_json_bz2);
|
---|
161 |
|
---|
162 | if (i==0) {
|
---|
163 | System.out.println("Sample output JSON page file: " + output_json_bz2);
|
---|
164 | }
|
---|
165 |
|
---|
166 | JSONObject ef_page = ef_pages.getJSONObject(i);
|
---|
167 |
|
---|
168 | if (ef_page != null) {
|
---|
169 | // Convert to Solr add form
|
---|
170 | JSONObject solr_add_doc_json = JSONSolrTransform.generateSolrDocJSON(volume_id, page_id, ef_page);
|
---|
171 |
|
---|
172 |
|
---|
173 | if ((_verbosity >=2) && (i==20)) {
|
---|
174 | System.out.println("==================");
|
---|
175 | System.out.println("Sample output Solr add JSON [page 20]: " + solr_add_doc_json.toString());
|
---|
176 | System.out.println("==================");
|
---|
177 | }
|
---|
178 |
|
---|
179 |
|
---|
180 | if (_solr_url != null) {
|
---|
181 | if ((_verbosity >=2) && (i==20)) {
|
---|
182 | System.out.println("==================");
|
---|
183 | System.out.println("Posting to: " + _solr_url);
|
---|
184 | System.out.println("==================");
|
---|
185 | }
|
---|
186 | postSolrDoc(_solr_url, solr_add_doc_json);
|
---|
187 | }
|
---|
188 |
|
---|
189 | if (_output_dir != null) {
|
---|
190 | if ((_verbosity >=2) && (i==20)) {
|
---|
191 | System.out.println("==================");
|
---|
192 | System.out.println("Saving to: " + _output_dir);
|
---|
193 | System.out.println("==================");
|
---|
194 | }
|
---|
195 | saveSolrDoc(solr_add_doc_json, _output_dir + "/" + output_json_bz2);
|
---|
196 | }
|
---|
197 | }
|
---|
198 | else {
|
---|
199 | System.err.println("Skipping: " + page_id);
|
---|
200 | }
|
---|
201 |
|
---|
202 | }
|
---|
203 |
|
---|
204 |
|
---|
205 | ids.add(volume_id);
|
---|
206 | _progress_accum.add(_progress_step);
|
---|
207 |
|
---|
208 | //return ids.iterator();
|
---|
209 | }
|
---|
210 | }
|
---|
211 |
|
---|