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

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

Support for icu-tokenize property added, plus relevant refactoring.

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