1 | package org.hathitrust;
|
---|
2 |
|
---|
3 | import java.io.BufferedInputStream;
|
---|
4 | import java.io.BufferedReader;
|
---|
5 | import java.io.FileInputStream;
|
---|
6 | import java.io.IOException;
|
---|
7 | import java.io.InputStreamReader;
|
---|
8 | import java.net.URI;
|
---|
9 | import java.util.ArrayList;
|
---|
10 | import java.util.Iterator;
|
---|
11 |
|
---|
12 | import org.apache.commons.compress.compressors.CompressorException;
|
---|
13 | import org.apache.commons.compress.compressors.CompressorInputStream;
|
---|
14 | import org.apache.commons.compress.compressors.CompressorStreamFactory;
|
---|
15 | import org.apache.hadoop.conf.Configuration;
|
---|
16 | import org.apache.hadoop.fs.FSDataInputStream;
|
---|
17 | import org.apache.hadoop.fs.FileSystem;
|
---|
18 | import org.apache.hadoop.fs.Path;
|
---|
19 | import org.apache.spark.api.java.function.FlatMapFunction;
|
---|
20 | import org.json.JSONArray;
|
---|
21 | import org.json.JSONObject;
|
---|
22 |
|
---|
23 | /*
|
---|
24 | class PagedJSON implements Function<String, Boolean> {
|
---|
25 |
|
---|
26 | private static final long serialVersionUID = 1L;
|
---|
27 |
|
---|
28 | public Boolean call(String s) { return s.contains("a"); }
|
---|
29 | }
|
---|
30 | */
|
---|
31 |
|
---|
32 |
|
---|
33 | class PagedJSON implements FlatMapFunction<String, String>
|
---|
34 | {
|
---|
35 | private static final long serialVersionUID = 1L;
|
---|
36 |
|
---|
37 | protected String _input_dir;
|
---|
38 |
|
---|
39 | public PagedJSON(String input_dir)
|
---|
40 | {
|
---|
41 | _input_dir = input_dir;
|
---|
42 | }
|
---|
43 |
|
---|
44 | protected static BufferedInputStream getBufferedInputStream(String fileIn)
|
---|
45 | throws IOException
|
---|
46 | {
|
---|
47 | BufferedInputStream bis = null;
|
---|
48 |
|
---|
49 | if (fileIn.startsWith("hdfs://")) {
|
---|
50 | URI uri = URI.create (fileIn);
|
---|
51 | Configuration conf = new Configuration();
|
---|
52 | FileSystem file = FileSystem.get(uri, conf);
|
---|
53 | FSDataInputStream fin = file.open(new Path(uri));
|
---|
54 |
|
---|
55 | bis = new BufferedInputStream(fin);
|
---|
56 | }
|
---|
57 | else {
|
---|
58 | // Trim 'file://' off the front
|
---|
59 |
|
---|
60 | String local_file_in = fileIn;
|
---|
61 | if (local_file_in.startsWith("file://")) {
|
---|
62 | local_file_in = fileIn.substring("file://".length());
|
---|
63 | }
|
---|
64 | FileInputStream fin = new FileInputStream(local_file_in);
|
---|
65 | bis = new BufferedInputStream(fin);
|
---|
66 | }
|
---|
67 |
|
---|
68 | return bis;
|
---|
69 |
|
---|
70 | }
|
---|
71 | protected static BufferedReader getBufferedReaderForCompressedFile(String fileIn)
|
---|
72 | throws IOException, CompressorException
|
---|
73 | {
|
---|
74 | BufferedInputStream bis = getBufferedInputStream(fileIn);
|
---|
75 | CompressorInputStream comp_input = new CompressorStreamFactory().createCompressorInputStream(bis);
|
---|
76 | BufferedReader br = new BufferedReader(new InputStreamReader(comp_input,"UTF8"));
|
---|
77 | return br;
|
---|
78 | }
|
---|
79 |
|
---|
80 | protected JSONObject readJSONFile(String filename)
|
---|
81 | {
|
---|
82 | //Path path = Paths.get(filename);
|
---|
83 |
|
---|
84 | StringBuilder sb = new StringBuilder();
|
---|
85 |
|
---|
86 | try {
|
---|
87 |
|
---|
88 | String str;
|
---|
89 | BufferedReader br = getBufferedReaderForCompressedFile(_input_dir + "/" + filename);
|
---|
90 | while ((str = br.readLine()) != null) {
|
---|
91 | sb.append(str);
|
---|
92 | //System.out.println(str);
|
---|
93 | }
|
---|
94 |
|
---|
95 | br.close();
|
---|
96 |
|
---|
97 | //System.err.println("*****" + sb.toString());
|
---|
98 |
|
---|
99 | /*
|
---|
100 | List<String> lines = Files.readAllLines(path,StandardCharsets.UTF_8);
|
---|
101 |
|
---|
102 |
|
---|
103 | for (String line : lines) {
|
---|
104 | sb.append(line);
|
---|
105 |
|
---|
106 | }
|
---|
107 | */
|
---|
108 |
|
---|
109 | }
|
---|
110 | catch (Exception e) {
|
---|
111 | e.printStackTrace();
|
---|
112 | }
|
---|
113 |
|
---|
114 | JSONObject json_obj = new JSONObject(sb.toString());
|
---|
115 |
|
---|
116 |
|
---|
117 | return json_obj;
|
---|
118 |
|
---|
119 | //return sb.toString();
|
---|
120 | }
|
---|
121 |
|
---|
122 | public Iterator<String> call(String s)
|
---|
123 | {
|
---|
124 | JSONObject extracted_feature_record = readJSONFile(s);
|
---|
125 |
|
---|
126 | String id = extracted_feature_record.getString("id");
|
---|
127 |
|
---|
128 | JSONObject ef_metadata = extracted_feature_record.getJSONObject("metadata");
|
---|
129 | JSONObject ef_features = extracted_feature_record.getJSONObject("features");
|
---|
130 |
|
---|
131 |
|
---|
132 | int ef_page_count = ef_features.getInt("pageCount");
|
---|
133 |
|
---|
134 | JSONArray ef_pages = ef_features.getJSONArray("pages");
|
---|
135 | int ef_num_pages = ef_pages.length();
|
---|
136 |
|
---|
137 | ArrayList<String> ids = new ArrayList<String>(ef_num_pages);
|
---|
138 | for (int i = 0; i < ef_page_count; i++) {
|
---|
139 | ids.add(id + "." + i);
|
---|
140 | }
|
---|
141 |
|
---|
142 | /*
|
---|
143 | for (int i = 0; i < ef_num_pages; i++)
|
---|
144 | {
|
---|
145 | //String post_id = ef_pages.getJSONObject(i).getString("post_id");
|
---|
146 | //......
|
---|
147 | }
|
---|
148 | */
|
---|
149 | //String pageName = json_obj.getJSONObject("pageInfo").getString("pageName");
|
---|
150 | /*
|
---|
151 | JSONArray arr = obj.getJSONArray("posts");
|
---|
152 | for (int i = 0; i < arr.length(); i++)
|
---|
153 | {
|
---|
154 | String post_id = arr.getJSONObject(i).getString("post_id");
|
---|
155 | ......
|
---|
156 | }
|
---|
157 | */
|
---|
158 |
|
---|
159 |
|
---|
160 | ids.add(id);
|
---|
161 |
|
---|
162 | return ids.iterator();
|
---|
163 | }
|
---|
164 | }
|
---|
165 |
|
---|