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

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

Explicity default constructors added

  • Property svn:executable set to *
File size: 4.1 KB
Line 
1package org.hathitrust.extractedfeatures;
2
3import java.io.BufferedReader;
4import java.io.BufferedWriter;
5import java.io.IOException;
6import java.io.InputStreamReader;
7import java.io.OutputStream;
8import java.net.HttpURLConnection;
9import java.net.URL;
10import java.util.ArrayList;
11import java.util.Set;
12
13import org.apache.commons.compress.compressors.CompressorException;
14import org.apache.spark.api.java.function.FlatMapFunction;
15import org.apache.spark.api.java.function.VoidFunction;
16import org.apache.spark.util.DoubleAccumulator;
17import org.json.JSONArray;
18import org.json.JSONObject;
19
20/*
21class 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
30public class PerVolumeJSON extends BasePerJSON implements VoidFunction<String>
31{
32 private static final long serialVersionUID = 1L;
33
34 public PerVolumeJSON()
35 {
36 super();
37 }
38
39 public PerVolumeJSON(String input_dir, String solr_url, String output_dir, int verbosity,
40 DoubleAccumulator progress_accum, double progress_step)
41 {
42 super(input_dir,solr_url,output_dir,verbosity,progress_accum,progress_step);
43 }
44
45 //public Iterator<String> call(String json_file_in)
46 public void call(String json_file_in)
47 {
48 JSONObject extracted_feature_record = JSONClusterFileIO.readJSONFile(_input_dir + "/" + json_file_in);
49
50 String volume_id = extracted_feature_record.getString("id");
51
52 //JSONObject ef_metadata = extracted_feature_record.getJSONObject("metadata");
53 //String title= ef_metadata.getString("title");
54
55 JSONObject ef_features = extracted_feature_record.getJSONObject("features");
56
57
58 int ef_page_count = ef_features.getInt("pageCount");
59
60 if (_verbosity >= 1) {
61 System.out.println("Processing: " + json_file_in);
62 System.out.println(" pageCount = " + ef_page_count);
63 }
64
65 JSONArray ef_pages = ef_features.getJSONArray("pages");
66 int ef_num_pages = ef_pages.length();
67
68 // Make directory for page-level JSON output
69 String json_dir = ClusterFileIO.removeSuffix(json_file_in,".json.bz2");
70 String page_json_dir = json_dir + "/pages";
71 ClusterFileIO.createDirectoryAll(_output_dir + "/" + page_json_dir);
72
73 //ArrayList<String> ids = new ArrayList<String>(ef_num_pages);
74 for (int i = 0; i < ef_page_count; i++) {
75 String formatted_i = String.format("page-%06d", i);
76 String page_id = volume_id + "." + formatted_i;
77
78 if (_verbosity >= 2) {
79 System.out.println(" Page: " + page_id);
80 }
81
82 String output_json_bz2 = page_json_dir +"/" + formatted_i + ".json.bz2";
83 //ids.add(output_json_bz2); // ****
84
85 if (i==0) {
86 System.out.println("Sample output JSON page file: " + output_json_bz2);
87 }
88
89 JSONObject ef_page = ef_pages.getJSONObject(i);
90
91 if (ef_page != null) {
92 // Convert to Solr add form
93 JSONObject solr_add_doc_json = JSONSolrTransform.generateSolrDocJSON(volume_id, page_id, ef_page);
94
95
96 if ((_verbosity >=2) && (i==20)) {
97 System.out.println("==================");
98 System.out.println("Sample output Solr add JSON [page 20]: " + solr_add_doc_json.toString());
99 System.out.println("==================");
100 }
101
102
103 if (_solr_url != null) {
104 if ((_verbosity >=2) && (i==20)) {
105 System.out.println("==================");
106 System.out.println("Posting to: " + _solr_url);
107 System.out.println("==================");
108 }
109 JSONSolrTransform.postSolrDoc(_solr_url, solr_add_doc_json);
110 }
111
112 if (_output_dir != null) {
113 if ((_verbosity >=2) && (i==20)) {
114 System.out.println("==================");
115 System.out.println("Saving to: " + _output_dir);
116 System.out.println("==================");
117 }
118 JSONSolrTransform.saveSolrDoc(solr_add_doc_json, _output_dir + "/" + output_json_bz2);
119 }
120 }
121 else {
122 System.err.println("Skipping: " + page_id);
123 }
124
125 }
126
127
128 //ids.add(volume_id);
129 _progress_accum.add(_progress_step);
130
131 //return ids.iterator();
132 }
133}
134
Note: See TracBrowser for help on using the repository browser.