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
RevLine 
[31007]1package org.hathitrust.extractedfeatures;
2
[31045]3import java.io.IOException;
[31007]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;
[31225]27 protected String _whitelist_filename;
[31220]28
[31007]29 protected String _solr_url;
30 protected String _output_dir;
[31225]31
[31007]32 protected int _verbosity;
33
[31225]34 protected WhitelistBloomFilter _whitelist_bloomfilter;
35
36
[31007]37 protected DoubleAccumulator _progress_accum;
38 protected double _progress_step;
39
[31252]40 boolean _icu_tokenize;
[31045]41 boolean _strict_file_io;
42
[31220]43 public PerPageJSONFlatmap(String input_dir, String whitelist_filename,
44 String solr_url, String output_dir, int verbosity,
[31045]45 DoubleAccumulator progress_accum, double progress_step,
[31252]46 boolean icu_tokenize, boolean strict_file_io)
[31007]47 {
48 _input_dir = input_dir;
[31225]49 _whitelist_filename = whitelist_filename;
[31220]50
[31007]51 _solr_url = solr_url;
52 _output_dir = output_dir;
53 _verbosity = verbosity;
54
55 _progress_accum = progress_accum;
56 _progress_step = progress_step;
[31045]57
[31252]58 _icu_tokenize = icu_tokenize;
[31045]59 _strict_file_io = strict_file_io;
[31225]60
61 _whitelist_bloomfilter = null;
[31007]62 }
63
[31090]64
[31045]65 public Iterator<JSONObject> call(String json_file_in) throws IOException
[31007]66 {
[31226]67 if ((_whitelist_filename != null) && (_whitelist_bloomfilter == null)) {
[31225]68 _whitelist_bloomfilter = new WhitelistBloomFilter(_whitelist_filename,true);
69 }
70
[31220]71 String full_json_file_in = _input_dir + "/" + json_file_in;
[31045]72 JSONObject extracted_feature_record = JSONClusterFileIO.readJSONFile(full_json_file_in);
[31220]73
[31045]74 ArrayList<JSONObject> json_pages = new ArrayList<JSONObject>();
[31007]75
[31045]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);
[31030]90 }
[31045]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
[31096]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 }
[31007]107 if (_verbosity >= 2) {
[31045]108 System.out.print(" Pages: ");
[31007]109 }
[31045]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
[31030]115 if (_verbosity >= 2) {
[31045]116 if (i>0) {
117 System.out.print(", ");
118 }
119 System.out.print(page_id);
[31030]120 }
[31045]121
122 String output_json_bz2 = page_json_dir +"/" + formatted_i + ".json.bz2";
[31007]123
[31045]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
[31221]135 JSONObject solr_add_doc_json
[31252]136 = SolrDocJSON.generateSolrDocJSON(volume_id, page_id, ef_page, _whitelist_bloomfilter,_icu_tokenize);
[31045]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
[31007]147 }
[31045]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 }
[31007]155 else {
[31045]156 System.err.println("Warning: " + mess);
157 System.out.println("Warning: " + mess);
[31007]158 }
159 }
[31045]160
[31007]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.