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

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

Save a JSONObject as a file in the output directory

  • Property svn:executable set to *
File size: 3.9 KB
Line 
1package org.hathitrust;
2
3import java.io.BufferedReader;
4import java.io.BufferedWriter;
5import java.io.IOException;
6import java.util.ArrayList;
7import java.util.Iterator;
8
9import org.apache.commons.compress.compressors.CompressorException;
10import org.apache.spark.api.java.function.FlatMapFunction;
11import org.json.JSONArray;
12import org.json.JSONObject;
13
14/*
15class PagedJSON implements Function<String, Boolean> {
16
17 private static final long serialVersionUID = 1L;
18
19 public Boolean call(String s) { return s.contains("a"); }
20}
21 */
22
23
24class PagedJSON implements FlatMapFunction<String, String>
25{
26 private static final long serialVersionUID = 1L;
27
28 protected String _input_dir;
29 protected String _output_dir;
30 protected int _verbosity;
31
32 public PagedJSON(String input_dir, String output_dir, int verbosity)
33 {
34 _input_dir = input_dir;
35 _output_dir = output_dir;
36 _verbosity = verbosity;
37 }
38
39 protected JSONObject readJSONFile(String filename)
40 {
41 //Path path = Paths.get(filename);
42
43 StringBuilder sb = new StringBuilder();
44
45 try {
46
47 String str;
48 BufferedReader br = ClusterFileIO.getBufferedReaderForCompressedFile(_input_dir + "/" + filename);
49 while ((str = br.readLine()) != null) {
50 sb.append(str);
51 }
52
53 br.close();
54 }
55 catch (Exception e) {
56 e.printStackTrace();
57 }
58
59 JSONObject json_obj = new JSONObject(sb.toString());
60
61 return json_obj;
62 }
63
64 public Iterator<String> call(String json_file_in)
65 {
66 JSONObject extracted_feature_record = readJSONFile(json_file_in);
67
68 // Check output directory for volume exists, and create it if not
69
70
71 String id = extracted_feature_record.getString("id");
72
73 //JSONObject ef_metadata = extracted_feature_record.getJSONObject("metadata");
74 JSONObject ef_features = extracted_feature_record.getJSONObject("features");
75
76
77 int ef_page_count = ef_features.getInt("pageCount");
78
79 if (_verbosity >= 1) {
80 System.out.println("Processing: " + json_file_in);
81 System.out.println(" pageCount = " + ef_page_count);
82 }
83
84 JSONArray ef_pages = ef_features.getJSONArray("pages");
85 int ef_num_pages = ef_pages.length();
86
87 // Make directory for page-level JSON output
88 String json_dir = ClusterFileIO.removeSuffix(json_file_in,".json.bz2");
89 String page_json_dir = json_dir + "/pages";
90 ClusterFileIO.createDirectoryAll(_output_dir + "/" + page_json_dir);
91 //System.out.println("mkdir: " + _output_dir + "/" + page_json_dir);
92
93 ArrayList<String> ids = new ArrayList<String>(ef_num_pages);
94 for (int i = 0; i < ef_page_count; i++) {
95 String formatted_i = String.format("page-%06d", i);
96 String page_id = id + "." + formatted_i;
97
98 if (_verbosity >= 2) {
99 System.out.println(" Page: " + page_id);
100 }
101
102
103
104 String output_json_bz2 = page_json_dir +"/" + formatted_i + ".json.bz2";
105 ids.add(output_json_bz2);
106
107 if (i==0) {
108 System.out.println("Sample output JSON page file: " + output_json_bz2);
109 }
110
111 // create JSON obj of just the page (for now), and write it out
112 JSONObject ef_page = ef_pages.getJSONObject(i);
113 try {
114 BufferedWriter bw = ClusterFileIO.getBufferedWriterForCompressedFile(output_json_bz2);
115 bw.write(ef_page.toString());
116 bw.close();
117 } catch (IOException e) {
118 e.printStackTrace();
119 } catch (CompressorException e) {
120 e.printStackTrace();
121 }
122
123
124 }
125
126 /*
127 for (int i = 0; i < ef_num_pages; i++)
128 {
129 //String post_id = ef_pages.getJSONObject(i).getString("post_id");
130 //......
131 }
132 */
133 //String pageName = json_obj.getJSONObject("pageInfo").getString("pageName");
134/*
135 JSONArray arr = obj.getJSONArray("posts");
136 for (int i = 0; i < arr.length(); i++)
137 {
138 String post_id = arr.getJSONObject(i).getString("post_id");
139 ......
140 }
141*/
142
143
144 ids.add(id);
145
146 return ids.iterator();
147 }
148}
149
Note: See TracBrowser for help on using the repository browser.