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

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

Fixed bloom test for init

  • Property svn:executable set to *
File size: 4.7 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 _whitelist_filename;
28
29 protected String _solr_url;
30 protected String _output_dir;
31
32 protected int _verbosity;
33
34 protected WhitelistBloomFilter _whitelist_bloomfilter;
35
36
37 protected DoubleAccumulator _progress_accum;
38 protected double _progress_step;
39
40 boolean _strict_file_io;
41
42 public PerPageJSONFlatmap(String input_dir, String whitelist_filename,
43 String solr_url, String output_dir, int verbosity,
44 DoubleAccumulator progress_accum, double progress_step,
45 boolean strict_file_io)
46 {
47 _input_dir = input_dir;
48 _whitelist_filename = whitelist_filename;
49
50 _solr_url = solr_url;
51 _output_dir = output_dir;
52 _verbosity = verbosity;
53
54 _progress_accum = progress_accum;
55 _progress_step = progress_step;
56
57 _strict_file_io = strict_file_io;
58
59 _whitelist_bloomfilter = null;
60 }
61
62
63 public Iterator<JSONObject> call(String json_file_in) throws IOException
64 {
65 if ((_whitelist_filename != null) && (_whitelist_bloomfilter == null)) {
66 _whitelist_bloomfilter = new WhitelistBloomFilter(_whitelist_filename,true);
67 }
68
69 String full_json_file_in = _input_dir + "/" + json_file_in;
70 JSONObject extracted_feature_record = JSONClusterFileIO.readJSONFile(full_json_file_in);
71
72 ArrayList<JSONObject> json_pages = new ArrayList<JSONObject>();
73
74 if (extracted_feature_record != null) {
75 String volume_id = extracted_feature_record.getString("id");
76
77 //JSONObject ef_metadata = extracted_feature_record.getJSONObject("metadata");
78 //String title= ef_metadata.getString("title");
79
80 JSONObject ef_features = extracted_feature_record.getJSONObject("features");
81
82
83 int ef_page_count = ef_features.getInt("pageCount");
84
85 if (_verbosity >= 1) {
86 System.out.println("Processing: " + json_file_in);
87 System.out.println(" pageCount = " + ef_page_count);
88 }
89
90 JSONArray ef_pages = ef_features.getJSONArray("pages");
91 int ef_num_pages = ef_pages.length();
92 if (ef_num_pages != ef_page_count) {
93 System.err.println("Warning: number of page elements in JSON (" + ef_num_pages + ")"
94 +" does not match 'pageCount' metadata (" + ef_page_count + ")");
95 }
96
97 // Make directory for page-level JSON output
98 String json_dir = ClusterFileIO.removeSuffix(json_file_in,".json.bz2");
99 String page_json_dir = json_dir + "/pages";
100
101 if (_output_dir != null) {
102 // Only need to do this once per volume, so easier to here than in the per-page Map
103 ClusterFileIO.createDirectoryAll(_output_dir + "/" + page_json_dir);
104 }
105 if (_verbosity >= 2) {
106 System.out.print(" Pages: ");
107 }
108
109 for (int i = 0; i < ef_page_count; i++) {
110 String formatted_i = String.format("page-%06d", i);
111 String page_id = volume_id + "." + formatted_i;
112
113 if (_verbosity >= 2) {
114 if (i>0) {
115 System.out.print(", ");
116 }
117 System.out.print(page_id);
118 }
119
120 String output_json_bz2 = page_json_dir +"/" + formatted_i + ".json.bz2";
121
122 if (i==(ef_page_count-1)) {
123 if (_verbosity >= 2) {
124 System.out.println();
125 }
126 System.out.println("Sample output JSON page file: " + output_json_bz2);
127 }
128
129 JSONObject ef_page = ef_pages.getJSONObject(i);
130
131 if (ef_page != null) {
132 // Convert to Solr add form
133 JSONObject solr_add_doc_json
134 = SolrDocJSON.generateSolrDocJSON(volume_id, page_id, ef_page, _whitelist_bloomfilter);
135 solr_add_doc_json.put("filename_json_bz2", output_json_bz2);
136
137 json_pages.add(solr_add_doc_json);
138
139
140 }
141 else {
142 System.err.println("Skipping: " + page_id);
143 }
144
145 }
146 }
147 else {
148 // File did not exist, or could not be parsed
149 String mess = "Failed to read in bzipped JSON file '" + full_json_file_in + "'";
150 if (_strict_file_io) {
151 throw new IOException(mess);
152 }
153 else {
154 System.err.println("Warning: " + mess);
155 System.out.println("Warning: " + mess);
156 }
157 }
158
159 _progress_accum.add(_progress_step);
160
161 return json_pages.iterator();
162 }
163
164
165}
166
Note: See TracBrowser for help on using the repository browser.