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(String input_dir, String solr_url, String output_dir, int verbosity,
|
---|
27 | DoubleAccumulator progress_accum, double progress_step)
|
---|
28 | {
|
---|
29 | super(input_dir,solr_url,output_dir,verbosity,progress_accum,progress_step);
|
---|
30 | }
|
---|
31 |
|
---|
32 |
|
---|
33 | public Iterator<JSONObject> call(String json_file_in)
|
---|
34 | //public void call(String json_file_in)
|
---|
35 | {
|
---|
36 | JSONObject extracted_feature_record = JSONClusterFileIO.readJSONFile(_input_dir + "/" + json_file_in);
|
---|
37 |
|
---|
38 | String volume_id = extracted_feature_record.getString("id");
|
---|
39 |
|
---|
40 | //JSONObject ef_metadata = extracted_feature_record.getJSONObject("metadata");
|
---|
41 | //String title= ef_metadata.getString("title");
|
---|
42 |
|
---|
43 | JSONObject ef_features = extracted_feature_record.getJSONObject("features");
|
---|
44 |
|
---|
45 |
|
---|
46 | int ef_page_count = ef_features.getInt("pageCount");
|
---|
47 |
|
---|
48 | if (_verbosity >= 1) {
|
---|
49 | System.out.println("Processing: " + json_file_in);
|
---|
50 | System.out.println(" pageCount = " + ef_page_count);
|
---|
51 | }
|
---|
52 |
|
---|
53 | JSONArray ef_pages = ef_features.getJSONArray("pages");
|
---|
54 | int ef_num_pages = ef_pages.length();
|
---|
55 |
|
---|
56 | // Make directory for page-level JSON output
|
---|
57 | String json_dir = ClusterFileIO.removeSuffix(json_file_in,".json.bz2");
|
---|
58 | String page_json_dir = json_dir + "/pages";
|
---|
59 | ClusterFileIO.createDirectoryAll(_output_dir + "/" + page_json_dir);
|
---|
60 |
|
---|
61 | ArrayList<JSONObject> json_pages = new ArrayList<JSONObject>(ef_num_pages);
|
---|
62 | for (int i = 0; i < ef_page_count; i++) {
|
---|
63 | String formatted_i = String.format("page-%06d", i);
|
---|
64 | String page_id = volume_id + "." + formatted_i;
|
---|
65 |
|
---|
66 | if (_verbosity >= 2) {
|
---|
67 | System.out.println(" Page: " + page_id);
|
---|
68 | }
|
---|
69 |
|
---|
70 | String output_json_bz2 = page_json_dir +"/" + formatted_i + ".json.bz2";
|
---|
71 | //ids.add(output_json_bz2);
|
---|
72 |
|
---|
73 | if (i==0) {
|
---|
74 | System.out.println("Sample output JSON page file: " + output_json_bz2);
|
---|
75 | }
|
---|
76 |
|
---|
77 | JSONObject ef_page = ef_pages.getJSONObject(i);
|
---|
78 |
|
---|
79 | if (ef_page != null) {
|
---|
80 | // Convert to Solr add form
|
---|
81 | JSONObject solr_add_doc_json = JSONSolrTransform.generateSolrDocJSON(volume_id, page_id, ef_page);
|
---|
82 | solr_add_doc_json.put("filename_json_bz2", output_json_bz2);
|
---|
83 |
|
---|
84 | json_pages.add(solr_add_doc_json);
|
---|
85 |
|
---|
86 |
|
---|
87 | }
|
---|
88 | else {
|
---|
89 | System.err.println("Skipping: " + page_id);
|
---|
90 | }
|
---|
91 |
|
---|
92 | }
|
---|
93 |
|
---|
94 |
|
---|
95 | //ids.add(volume_id);
|
---|
96 | _progress_accum.add(_progress_step);
|
---|
97 |
|
---|
98 | //return ids.iterator();
|
---|
99 | return json_pages.iterator();
|
---|
100 | }
|
---|
101 |
|
---|
102 |
|
---|
103 | }
|
---|
104 |
|
---|