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

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

Only need to create a volume's pages output directory is _output_dir has been specified

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