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

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

Need to separate flatMap and foreach calls in PagedJSON

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