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