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

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

Relocated bloomfilter creation to within call() method, so done on the submitted side where the code runs

  • Property svn:executable set to *
File size: 4.4 KB
Line 
1package org.hathitrust.extractedfeatures;
2
3import org.apache.spark.api.java.function.VoidFunction;
4import org.apache.spark.util.DoubleAccumulator;
5import org.json.JSONArray;
6import org.json.JSONObject;
7
8/*
9class PagedJSON implements Function<String, Boolean> {
10
11 private static final long serialVersionUID = 1L;
12
13 public Boolean call(String s) { return s.contains("a"); }
14}
15 */
16
17
18public class PerVolumeJSON implements VoidFunction<String>
19{
20 private static final long serialVersionUID = 1L;
21 protected String _input_dir;
22 protected String _whitelist_filename;
23
24 protected String _solr_url;
25 protected String _output_dir;
26
27 protected int _verbosity;
28
29 protected WhitelistBloomFilter _whitelist_bloomfilter;
30
31 protected DoubleAccumulator _progress_accum;
32 protected double _progress_step;
33
34 public PerVolumeJSON(String input_dir, String whitelist_filename,
35 String solr_url, String output_dir, int verbosity,
36 DoubleAccumulator progress_accum, double progress_step)
37 {
38 _input_dir = input_dir;
39 _whitelist_filename = whitelist_filename;
40
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 _whitelist_bloomfilter = null;
49 }
50
51 //public Iterator<String> call(String json_file_in)
52 public void call(String json_file_in)
53 {
54 if ((_whitelist_filename != null) && (_whitelist_bloomfilter != null)) {
55 _whitelist_bloomfilter = new WhitelistBloomFilter(_whitelist_filename,true);
56 }
57
58 JSONObject extracted_feature_record = JSONClusterFileIO.readJSONFile(_input_dir + "/" + json_file_in);
59
60 String volume_id = extracted_feature_record.getString("id");
61
62 //JSONObject ef_metadata = extracted_feature_record.getJSONObject("metadata");
63 //String title= ef_metadata.getString("title");
64
65 JSONObject ef_features = extracted_feature_record.getJSONObject("features");
66
67
68 int ef_page_count = ef_features.getInt("pageCount");
69
70 if (_verbosity >= 1) {
71 System.out.println("Processing: " + json_file_in);
72 System.out.println(" pageCount = " + ef_page_count);
73 }
74
75 JSONArray ef_pages = ef_features.getJSONArray("pages");
76 int ef_num_pages = ef_pages.length();
77
78 // Make directory for page-level JSON output
79 String json_dir = ClusterFileIO.removeSuffix(json_file_in,".json.bz2");
80 String page_json_dir = json_dir + "/pages";
81
82 if (_output_dir != null) {
83 ClusterFileIO.createDirectoryAll(_output_dir + "/" + page_json_dir);
84 }
85
86 //ArrayList<String> ids = new ArrayList<String>(ef_num_pages);
87 for (int i = 0; i < ef_page_count; i++) {
88 String formatted_i = String.format("page-%06d", i);
89 String page_id = volume_id + "." + formatted_i;
90
91 if (_verbosity >= 2) {
92 System.out.println(" Page: " + page_id);
93 }
94
95 String output_json_bz2 = page_json_dir +"/" + formatted_i + ".json.bz2";
96 //ids.add(output_json_bz2); // ****
97
98 if (i==0) {
99 System.out.println("Sample output JSON page file: " + output_json_bz2);
100 }
101
102 JSONObject ef_page = ef_pages.getJSONObject(i);
103
104 if (ef_page != null) {
105 // Convert to Solr add form
106 JSONObject solr_add_doc_json
107 = SolrDocJSON.generateSolrDocJSON(volume_id, page_id, ef_page, _whitelist_bloomfilter);
108
109
110 if ((_verbosity >=2) && (i==20)) {
111 System.out.println("==================");
112 System.out.println("Sample output Solr add JSON [page 20]: " + solr_add_doc_json.toString());
113 System.out.println("==================");
114 }
115
116
117 if (_solr_url != null) {
118 if ((_verbosity >=2) && (i==20)) {
119 System.out.println("==================");
120 System.out.println("Posting to: " + _solr_url);
121 System.out.println("==================");
122 }
123 SolrDocJSON.postSolrDoc(_solr_url, solr_add_doc_json);
124 }
125
126 if (_output_dir != null) {
127 if ((_verbosity >=2) && (i==20)) {
128 System.out.println("==================");
129 System.out.println("Saving to: " + _output_dir);
130 System.out.println("==================");
131 }
132 SolrDocJSON.saveSolrDoc(solr_add_doc_json, _output_dir + "/" + output_json_bz2);
133 }
134 }
135 else {
136 System.err.println("Skipping: " + page_id);
137 }
138
139 }
140
141
142 //ids.add(volume_id);
143 _progress_accum.add(_progress_step);
144
145 //return ids.iterator();
146 }
147}
148
Note: See TracBrowser for help on using the repository browser.