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

Last change on this file since 31015 was 31011, checked in by davidb, 8 years ago

Further RDD flatMap/map restructuring and refactoring, for per-page

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