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

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

Memory monitor debugging code, commented out

  • Property svn:executable set to *
File size: 4.3 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 ClusterFileIO.createDirectoryAll(_output_dir + "/" + page_json_dir);
89
90 if (_verbosity >= 2) {
91 System.out.print(" Pages: ");
92 }
93
94 for (int i = 0; i < ef_page_count; i++) {
95 String formatted_i = String.format("page-%06d", i);
96 String page_id = volume_id + "." + formatted_i;
97
98 if (_verbosity >= 2) {
99 if (i>0) {
100 System.out.print(", ");
101 }
102 System.out.print(page_id);
103 }
104
105 String output_json_bz2 = page_json_dir +"/" + formatted_i + ".json.bz2";
106
107 if (i==(ef_page_count-1)) {
108 if (_verbosity >= 2) {
109 System.out.println();
110 }
111 System.out.println("Sample output JSON page file: " + output_json_bz2);
112 }
113
114 JSONObject ef_page = ef_pages.getJSONObject(i);
115
116 if (ef_page != null) {
117 // Convert to Solr add form
118 JSONObject solr_add_doc_json = SolrDocJSON.generateSolrDocJSON(volume_id, page_id, ef_page);
119 solr_add_doc_json.put("filename_json_bz2", output_json_bz2);
120
121 json_pages.add(solr_add_doc_json);
122
123
124 }
125 else {
126 System.err.println("Skipping: " + page_id);
127 }
128
129 }
130 }
131 else {
132 // File did not exist, or could not be parsed
133 String mess = "Failed to read in bzipped JSON file '" + full_json_file_in + "'";
134 if (_strict_file_io) {
135 throw new IOException(mess);
136 }
137 else {
138 System.err.println("Warning: " + mess);
139 System.out.println("Warning: " + mess);
140 }
141 }
142
143 _progress_accum.add(_progress_step);
144
145 return json_pages.iterator();
146 }
147
148
149}
150
Note: See TracBrowser for help on using the repository browser.