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

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

Class name refactoring

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