source: other-projects/hathitrust/solr-extracted-features/trunk/src/main/java/org/hathitrust/PagedJSON.java@ 30946

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

Correction to output JSON.bz2 name generated

  • Property svn:executable set to *
File size: 3.4 KB
Line 
1package org.hathitrust;
2
3import java.io.BufferedReader;
4import java.util.ArrayList;
5import java.util.Iterator;
6
7import org.apache.spark.api.java.function.FlatMapFunction;
8import org.json.JSONArray;
9import org.json.JSONObject;
10
11/*
12class 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
21class PagedJSON implements FlatMapFunction<String, String>
22{
23 private static final long serialVersionUID = 1L;
24
25 protected String _input_dir;
26 protected String _output_dir;
27 protected int _verbosity;
28
29 public PagedJSON(String input_dir, String output_dir, int verbosity)
30 {
31 _input_dir = input_dir;
32 _output_dir = output_dir;
33 _verbosity = verbosity;
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;
45 BufferedReader br = ClusterFileIO.getBufferedReaderForCompressedFile(_input_dir + "/" + filename);
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
61 public Iterator<String> call(String json_file_in)
62 {
63 JSONObject extracted_feature_record = readJSONFile(json_file_in);
64
65 // Check output directory for volume exists, and create it if not
66
67
68 String id = extracted_feature_record.getString("id");
69
70 //JSONObject ef_metadata = extracted_feature_record.getJSONObject("metadata");
71 JSONObject ef_features = extracted_feature_record.getJSONObject("features");
72
73
74 int ef_page_count = ef_features.getInt("pageCount");
75
76 if (_verbosity >= 1) {
77 System.out.println("Processing: " + json_file_in);
78 System.out.println(" pageCount = " + ef_page_count);
79 }
80
81 JSONArray ef_pages = ef_features.getJSONArray("pages");
82 int ef_num_pages = ef_pages.length();
83
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
90 ArrayList<String> ids = new ArrayList<String>(ef_num_pages);
91 for (int i = 0; i < ef_page_count; i++) {
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 String output_json_bz2 = page_json_dir +"/" + "pages/page-" + formatted_i + ".json.bz2";
103
104 ids.add(output_json_bz2);
105
106 if (i==0) {
107 System.out.println("Sample output JSON page file: " + output_json_bz2);
108 }
109 }
110
111 /*
112 for (int i = 0; i < ef_num_pages; i++)
113 {
114 //String post_id = ef_pages.getJSONObject(i).getString("post_id");
115 //......
116 }
117 */
118 //String pageName = json_obj.getJSONObject("pageInfo").getString("pageName");
119/*
120 JSONArray arr = obj.getJSONArray("posts");
121 for (int i = 0; i < arr.length(); i++)
122 {
123 String post_id = arr.getJSONObject(i).getString("post_id");
124 ......
125 }
126*/
127
128
129 ids.add(id);
130
131 return ids.iterator();
132 }
133}
134
Note: See TracBrowser for help on using the repository browser.