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

Last change on this file since 30942 was 30942, checked in by davidb, 8 years ago

Improved output printing for slave node

  • Property svn:executable set to *
File size: 2.9 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 int _verbosity;
27
28 public PagedJSON(String input_dir, int verbosity)
29 {
30 _input_dir = input_dir;
31 _verbosity = verbosity;
32 }
33
34 protected JSONObject readJSONFile(String filename)
35 {
36 //Path path = Paths.get(filename);
37
38 StringBuilder sb = new StringBuilder();
39
40 try {
41
42 String str;
43 BufferedReader br = ClusterFileIO.getBufferedReaderForCompressedFile(_input_dir + "/" + filename);
44 while ((str = br.readLine()) != null) {
45 sb.append(str);
46 //System.out.println(str);
47 }
48
49 br.close();
50
51 //System.err.println("*****" + sb.toString());
52
53 /*
54 List<String> lines = Files.readAllLines(path,StandardCharsets.UTF_8);
55
56
57 for (String line : lines) {
58 sb.append(line);
59
60 }
61 */
62
63 }
64 catch (Exception e) {
65 e.printStackTrace();
66 }
67
68 JSONObject json_obj = new JSONObject(sb.toString());
69
70
71 return json_obj;
72
73 //return sb.toString();
74 }
75
76 public Iterator<String> call(String json_file_in)
77 {
78 JSONObject extracted_feature_record = readJSONFile(json_file_in);
79
80 // Check output directory for volume exists, and create it if not
81
82
83 String id = extracted_feature_record.getString("id");
84
85 JSONObject ef_metadata = extracted_feature_record.getJSONObject("metadata");
86 JSONObject ef_features = extracted_feature_record.getJSONObject("features");
87
88
89 int ef_page_count = ef_features.getInt("pageCount");
90
91 if (_verbosity >= 1) {
92 System.out.println("Processing: " + json_file_in);
93 System.out.println(" pageCount = " + ef_page_count);
94 }
95
96 JSONArray ef_pages = ef_features.getJSONArray("pages");
97 int ef_num_pages = ef_pages.length();
98
99 ArrayList<String> ids = new ArrayList<String>(ef_num_pages);
100 for (int i = 0; i < ef_page_count; i++) {
101 ids.add(id + "." + i);
102 }
103
104 /*
105 for (int i = 0; i < ef_num_pages; i++)
106 {
107 //String post_id = ef_pages.getJSONObject(i).getString("post_id");
108 //......
109 }
110 */
111 //String pageName = json_obj.getJSONObject("pageInfo").getString("pageName");
112/*
113 JSONArray arr = obj.getJSONArray("posts");
114 for (int i = 0; i < arr.length(); i++)
115 {
116 String post_id = arr.getJSONObject(i).getString("post_id");
117 ......
118 }
119*/
120
121
122 ids.add(id);
123
124 return ids.iterator();
125 }
126}
127
Note: See TracBrowser for help on using the repository browser.