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

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

Scripts for downloading sample JSON data from public domain extracted feature set, and some initial processing using Apache Spark

  • Property svn:executable set to *
File size: 3.6 KB
Line 
1package org.hathitrust;
2
3import java.io.BufferedInputStream;
4import java.io.BufferedReader;
5import java.io.FileInputStream;
6import java.io.FileNotFoundException;
7import java.io.IOException;
8import java.io.InputStreamReader;
9import java.io.UnsupportedEncodingException;
10import java.nio.charset.StandardCharsets;
11import java.nio.file.Files;
12import java.nio.file.Path;
13import java.nio.file.Paths;
14import java.util.ArrayList;
15import java.util.Arrays;
16import java.util.Iterator;
17import java.util.List;
18
19import org.apache.commons.compress.compressors.CompressorException;
20import org.apache.commons.compress.compressors.CompressorInputStream;
21import org.apache.commons.compress.compressors.CompressorStreamFactory;
22import org.apache.spark.api.java.function.FlatMapFunction;
23import org.json.JSONArray;
24import org.json.JSONObject;
25
26/*
27class 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
35class 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
Note: See TracBrowser for help on using the repository browser.