source: other-projects/hathitrust/wcsa/extracted-features-solr/trunk/solr-ingest/src/main/java/org/hathitrust/extractedfeatures/PerPageJSONFlatmap.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: 4.4 KB
Line 
1package org.hathitrust.extractedfeatures;
2
3import java.io.IOException;
4import java.util.ArrayList;
5import java.util.Iterator;
6import org.apache.spark.api.java.function.FlatMapFunction;
7import org.apache.spark.util.DoubleAccumulator;
8import org.json.JSONArray;
9import org.json.JSONObject;
10
11/*
12class PagedJSON implements Function<String, Boolean> {
13
14 private static final long serialVersionUID = 1L;
15
16 public Boolean call(String s) { return s.contains("a"); }
17}
18 */
19
20
21class PerPageJSONFlatmap implements FlatMapFunction<String, JSONObject>
22//public class PagedJSON implements VoidFunction<String>
23{
24 private static final long serialVersionUID = 1L;
25
26 protected String _input_dir;
27 protected String _solr_url;
28 protected String _output_dir;
29 protected int _verbosity;
30
31 protected DoubleAccumulator _progress_accum;
32 protected double _progress_step;
33
34 boolean _strict_file_io;
35
36 public PerPageJSONFlatmap(String input_dir, String solr_url, String output_dir, int verbosity,
37 DoubleAccumulator progress_accum, double progress_step,
38 boolean strict_file_io)
39 {
40 _input_dir = input_dir;
41 _solr_url = solr_url;
42 _output_dir = output_dir;
43 _verbosity = verbosity;
44
45 _progress_accum = progress_accum;
46 _progress_step = progress_step;
47
48 _strict_file_io = strict_file_io;
49 }
50
51
52 public Iterator<JSONObject> call(String json_file_in) throws IOException
53 //public void call(String json_file_in)
54 {
55 //ClusterFileIO.memory_usage("Before BZIP2 JSON file read");
56 String full_json_file_in = _input_dir + "/" + json_file_in;
57 JSONObject extracted_feature_record = JSONClusterFileIO.readJSONFile(full_json_file_in);
58 //ClusterFileIO.memory_usage("After BZIP2 JSON file read");
59
60 ArrayList<JSONObject> json_pages = new ArrayList<JSONObject>();
61
62 if (extracted_feature_record != null) {
63 String volume_id = extracted_feature_record.getString("id");
64
65 //JSONObject ef_metadata = extracted_feature_record.getJSONObject("metadata");
66 //String title= ef_metadata.getString("title");
67
68 JSONObject ef_features = extracted_feature_record.getJSONObject("features");
69
70
71 int ef_page_count = ef_features.getInt("pageCount");
72
73 if (_verbosity >= 1) {
74 System.out.println("Processing: " + json_file_in);
75 System.out.println(" pageCount = " + ef_page_count);
76 }
77
78 JSONArray ef_pages = ef_features.getJSONArray("pages");
79 int ef_num_pages = ef_pages.length();
80 if (ef_num_pages != ef_page_count) {
81 System.err.println("Warning: number of page elements in JSON (" + ef_num_pages + ")"
82 +" does not match 'pageCount' metadata (" + ef_page_count + ")");
83 }
84
85 // Make directory for page-level JSON output
86 String json_dir = ClusterFileIO.removeSuffix(json_file_in,".json.bz2");
87 String page_json_dir = json_dir + "/pages";
88
89 if (_output_dir != null) {
90 // Only need to do this once per volume, so easier to here than in the per-page Map
91 ClusterFileIO.createDirectoryAll(_output_dir + "/" + page_json_dir);
92 }
93 if (_verbosity >= 2) {
94 System.out.print(" Pages: ");
95 }
96
97 for (int i = 0; i < ef_page_count; i++) {
98 String formatted_i = String.format("page-%06d", i);
99 String page_id = volume_id + "." + formatted_i;
100
101 if (_verbosity >= 2) {
102 if (i>0) {
103 System.out.print(", ");
104 }
105 System.out.print(page_id);
106 }
107
108 String output_json_bz2 = page_json_dir +"/" + formatted_i + ".json.bz2";
109
110 if (i==(ef_page_count-1)) {
111 if (_verbosity >= 2) {
112 System.out.println();
113 }
114 System.out.println("Sample output JSON page file: " + output_json_bz2);
115 }
116
117 JSONObject ef_page = ef_pages.getJSONObject(i);
118
119 if (ef_page != null) {
120 // Convert to Solr add form
121 JSONObject solr_add_doc_json = SolrDocJSON.generateSolrDocJSON(volume_id, page_id, ef_page);
122 solr_add_doc_json.put("filename_json_bz2", output_json_bz2);
123
124 json_pages.add(solr_add_doc_json);
125
126
127 }
128 else {
129 System.err.println("Skipping: " + page_id);
130 }
131
132 }
133 }
134 else {
135 // File did not exist, or could not be parsed
136 String mess = "Failed to read in bzipped JSON file '" + full_json_file_in + "'";
137 if (_strict_file_io) {
138 throw new IOException(mess);
139 }
140 else {
141 System.err.println("Warning: " + mess);
142 System.out.println("Warning: " + mess);
143 }
144 }
145
146 _progress_accum.add(_progress_step);
147
148 return json_pages.iterator();
149 }
150
151
152}
153
Note: See TracBrowser for help on using the repository browser.