[30898] | 1 | package org.hathitrust;
|
---|
| 2 |
|
---|
| 3 | import java.io.BufferedReader;
|
---|
| 4 | import java.util.ArrayList;
|
---|
| 5 | import java.util.Iterator;
|
---|
| 6 |
|
---|
| 7 | import org.apache.spark.api.java.function.FlatMapFunction;
|
---|
| 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 |
|
---|
[30918] | 20 |
|
---|
[30898] | 21 | class PagedJSON implements FlatMapFunction<String, String>
|
---|
| 22 | {
|
---|
| 23 | private static final long serialVersionUID = 1L;
|
---|
| 24 |
|
---|
| 25 | protected String _input_dir;
|
---|
[30945] | 26 | protected String _output_dir;
|
---|
[30942] | 27 | protected int _verbosity;
|
---|
[30898] | 28 |
|
---|
[30945] | 29 | public PagedJSON(String input_dir, String output_dir, int verbosity)
|
---|
[30898] | 30 | {
|
---|
[30945] | 31 | _input_dir = input_dir;
|
---|
| 32 | _output_dir = output_dir;
|
---|
| 33 | _verbosity = verbosity;
|
---|
[30898] | 34 | }
|
---|
| 35 |
|
---|
| 36 | protected JSONObject readJSONFile(String filename)
|
---|
| 37 | {
|
---|
| 38 | //Path path = Paths.get(filename);
|
---|
| 39 |
|
---|
| 40 | StringBuilder sb = new StringBuilder();
|
---|
| 41 |
|
---|
| 42 | try {
|
---|
| 43 |
|
---|
| 44 | String str;
|
---|
[30937] | 45 | BufferedReader br = ClusterFileIO.getBufferedReaderForCompressedFile(_input_dir + "/" + filename);
|
---|
[30898] | 46 | while ((str = br.readLine()) != null) {
|
---|
| 47 | sb.append(str);
|
---|
| 48 | }
|
---|
| 49 |
|
---|
| 50 | br.close();
|
---|
| 51 | }
|
---|
| 52 | catch (Exception e) {
|
---|
| 53 | e.printStackTrace();
|
---|
| 54 | }
|
---|
| 55 |
|
---|
| 56 | JSONObject json_obj = new JSONObject(sb.toString());
|
---|
| 57 |
|
---|
| 58 | return json_obj;
|
---|
| 59 | }
|
---|
| 60 |
|
---|
[30937] | 61 | public Iterator<String> call(String json_file_in)
|
---|
[30898] | 62 | {
|
---|
[30937] | 63 | JSONObject extracted_feature_record = readJSONFile(json_file_in);
|
---|
[30898] | 64 |
|
---|
[30937] | 65 | // Check output directory for volume exists, and create it if not
|
---|
| 66 |
|
---|
| 67 |
|
---|
[30898] | 68 | String id = extracted_feature_record.getString("id");
|
---|
| 69 |
|
---|
[30945] | 70 | //JSONObject ef_metadata = extracted_feature_record.getJSONObject("metadata");
|
---|
[30898] | 71 | JSONObject ef_features = extracted_feature_record.getJSONObject("features");
|
---|
| 72 |
|
---|
| 73 |
|
---|
| 74 | int ef_page_count = ef_features.getInt("pageCount");
|
---|
| 75 |
|
---|
[30942] | 76 | if (_verbosity >= 1) {
|
---|
| 77 | System.out.println("Processing: " + json_file_in);
|
---|
| 78 | System.out.println(" pageCount = " + ef_page_count);
|
---|
| 79 | }
|
---|
| 80 |
|
---|
[30898] | 81 | JSONArray ef_pages = ef_features.getJSONArray("pages");
|
---|
| 82 | int ef_num_pages = ef_pages.length();
|
---|
| 83 |
|
---|
[30945] | 84 | // Make directory for page-level JSON output
|
---|
| 85 | String json_dir = ClusterFileIO.removeSuffix(json_file_in,".json.bz2");
|
---|
| 86 | String page_json_dir = json_dir + "/pages";
|
---|
| 87 | //ClusterFileIO.createDirectoryAll(_output_dir + "/" + page_json_dir);
|
---|
| 88 | System.out.println("mkdir: " + _output_dir + "/" + page_json_dir);
|
---|
| 89 |
|
---|
[30898] | 90 | ArrayList<String> ids = new ArrayList<String>(ef_num_pages);
|
---|
| 91 | for (int i = 0; i < ef_page_count; i++) {
|
---|
[30945] | 92 | String formatted_i = String.format("page-%06d", i);
|
---|
| 93 | String page_id = id + "." + formatted_i;
|
---|
| 94 |
|
---|
| 95 | if (_verbosity >= 2) {
|
---|
| 96 | System.out.println(" Page: " + page_id);
|
---|
| 97 | }
|
---|
| 98 |
|
---|
| 99 | // create JSON obj of just the page (for now)
|
---|
| 100 | // write it out
|
---|
| 101 |
|
---|
| 102 | ids.add(page_json_dir +"/" + page_id + ".json.bz2");
|
---|
| 103 |
|
---|
| 104 | if (i==0) {
|
---|
| 105 | System.out.println("Sample output JSON page file: " + page_json_dir +"/" + page_id + ".json.bz2");
|
---|
| 106 | }
|
---|
[30898] | 107 | }
|
---|
| 108 |
|
---|
| 109 | /*
|
---|
| 110 | for (int i = 0; i < ef_num_pages; i++)
|
---|
| 111 | {
|
---|
| 112 | //String post_id = ef_pages.getJSONObject(i).getString("post_id");
|
---|
| 113 | //......
|
---|
| 114 | }
|
---|
| 115 | */
|
---|
| 116 | //String pageName = json_obj.getJSONObject("pageInfo").getString("pageName");
|
---|
| 117 | /*
|
---|
| 118 | JSONArray arr = obj.getJSONArray("posts");
|
---|
| 119 | for (int i = 0; i < arr.length(); i++)
|
---|
| 120 | {
|
---|
| 121 | String post_id = arr.getJSONObject(i).getString("post_id");
|
---|
| 122 | ......
|
---|
| 123 | }
|
---|
| 124 | */
|
---|
| 125 |
|
---|
| 126 |
|
---|
| 127 | ids.add(id);
|
---|
| 128 |
|
---|
| 129 | return ids.iterator();
|
---|
| 130 | }
|
---|
| 131 | }
|
---|
| 132 |
|
---|