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