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

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

Missing argument added in

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