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