1 | package org.hathitrust.extractedfeatures;
|
---|
2 |
|
---|
3 | import java.io.BufferedInputStream;
|
---|
4 | import java.io.BufferedOutputStream;
|
---|
5 | import java.io.BufferedReader;
|
---|
6 | import java.io.BufferedWriter;
|
---|
7 | import java.io.FileOutputStream;
|
---|
8 | import java.io.IOException;
|
---|
9 | import java.io.InputStreamReader;
|
---|
10 | import java.io.OutputStreamWriter;
|
---|
11 | import java.net.URI;
|
---|
12 | import java.net.URISyntaxException;
|
---|
13 |
|
---|
14 | import org.apache.commons.compress.compressors.CompressorException;
|
---|
15 | import org.apache.commons.compress.compressors.CompressorInputStream;
|
---|
16 | import org.apache.commons.compress.compressors.CompressorOutputStream;
|
---|
17 | import org.apache.commons.compress.compressors.CompressorStreamFactory;
|
---|
18 | import org.apache.hadoop.conf.Configuration;
|
---|
19 | import org.apache.hadoop.fs.FSDataInputStream;
|
---|
20 | import org.apache.hadoop.fs.FSDataOutputStream;
|
---|
21 | import org.apache.hadoop.fs.FileSystem;
|
---|
22 | import org.apache.hadoop.fs.Path;
|
---|
23 | import org.json.JSONObject;
|
---|
24 |
|
---|
25 | public class ClusterFileIO {
|
---|
26 |
|
---|
27 |
|
---|
28 | public static void memory_usage(String prefix)
|
---|
29 | {
|
---|
30 | Runtime runtime = Runtime.getRuntime();
|
---|
31 |
|
---|
32 | java.text.NumberFormat format = java.text.NumberFormat.getInstance();
|
---|
33 |
|
---|
34 | StringBuilder sb = new StringBuilder();
|
---|
35 | long maxMemory = runtime.maxMemory();
|
---|
36 | long allocatedMemory = runtime.totalMemory();
|
---|
37 | long freeMemory = runtime.freeMemory();
|
---|
38 |
|
---|
39 | sb.append(prefix+" free memory: " + format.format(freeMemory / 1024) + "\n");
|
---|
40 | sb.append(prefix+" allocated memory: " + format.format(allocatedMemory / 1024) + "\n");
|
---|
41 | sb.append(prefix+" max memory: " + format.format(maxMemory / 1024) + "\n");
|
---|
42 | sb.append(prefix+" total free memory: " + format.format((freeMemory + (maxMemory - allocatedMemory)) / 1024) + "\n");
|
---|
43 |
|
---|
44 | System.out.print(sb.toString());
|
---|
45 | }
|
---|
46 |
|
---|
47 |
|
---|
48 | protected static FileSystem getFileSystemInstance(String input_file_or_dir)
|
---|
49 | {
|
---|
50 | FileSystem fs = null;
|
---|
51 |
|
---|
52 | try {
|
---|
53 | Configuration conf = new Configuration();
|
---|
54 | URI uri = new URI(input_file_or_dir);
|
---|
55 | fs = FileSystem.newInstance(uri,conf);
|
---|
56 | }
|
---|
57 | catch (URISyntaxException e) {
|
---|
58 | e.printStackTrace();
|
---|
59 | }
|
---|
60 | catch (IOException e) {
|
---|
61 | e.printStackTrace();
|
---|
62 | }
|
---|
63 |
|
---|
64 | return fs;
|
---|
65 | }
|
---|
66 |
|
---|
67 | public static boolean isHDFS(String fileIn)
|
---|
68 | {
|
---|
69 | return fileIn.startsWith("hdfs://");
|
---|
70 | }
|
---|
71 |
|
---|
72 | public static boolean exists(String file)
|
---|
73 | {
|
---|
74 | FileSystem fs = getFileSystemInstance(file);
|
---|
75 |
|
---|
76 | boolean exists = false;
|
---|
77 |
|
---|
78 | try {
|
---|
79 | Path path = new Path(file);
|
---|
80 | exists = fs.exists(path);
|
---|
81 | } catch (IllegalArgumentException e) {
|
---|
82 | exists = false;
|
---|
83 | } catch (IOException e) {
|
---|
84 | exists = false;
|
---|
85 | }
|
---|
86 |
|
---|
87 | return exists;
|
---|
88 | }
|
---|
89 |
|
---|
90 | public static String removeSuffix(String file,String suffix)
|
---|
91 | {
|
---|
92 | return file.substring(0,file.length() - suffix.length());
|
---|
93 | }
|
---|
94 |
|
---|
95 | public static boolean createDirectoryAll(String dir)
|
---|
96 | {
|
---|
97 | FileSystem fs = getFileSystemInstance(dir);
|
---|
98 | boolean created_dir = false;
|
---|
99 |
|
---|
100 | if (!exists(dir)) {
|
---|
101 | try {
|
---|
102 | URI uri = new URI(dir);
|
---|
103 | Path path = new Path(uri);
|
---|
104 | fs.mkdirs(path);
|
---|
105 | created_dir = true;
|
---|
106 | } catch (URISyntaxException e) {
|
---|
107 | e.printStackTrace();
|
---|
108 | } catch (IOException e) {
|
---|
109 | e.printStackTrace();
|
---|
110 | }
|
---|
111 | }
|
---|
112 |
|
---|
113 | return created_dir;
|
---|
114 | }
|
---|
115 |
|
---|
116 | public static BufferedInputStream getBufferedInputStream(String fileIn)
|
---|
117 | throws IOException
|
---|
118 | {
|
---|
119 | FileSystem fs = getFileSystemInstance(fileIn);
|
---|
120 |
|
---|
121 | BufferedInputStream bis = null;
|
---|
122 |
|
---|
123 | if (isHDFS(fileIn)) {
|
---|
124 | URI uri = URI.create (fileIn);
|
---|
125 | //Configuration conf = new Configuration();
|
---|
126 | //FileSystem file = FileSystem.get(uri, conf);
|
---|
127 | //FSDataInputStream fin = file.open(new Path(uri));
|
---|
128 |
|
---|
129 | //FSDataInputStream fin = _fs.open(new Path(fileIn));
|
---|
130 |
|
---|
131 | Path path = new Path(uri);
|
---|
132 | FSDataInputStream fin = fs.open(path);
|
---|
133 |
|
---|
134 | bis = new BufferedInputStream(fin);
|
---|
135 | }
|
---|
136 | else {
|
---|
137 | /*
|
---|
138 | // Trim 'file://' off the front
|
---|
139 | String local_file_in = fileIn;
|
---|
140 | if (local_file_in.startsWith("file://")) {
|
---|
141 | local_file_in = fileIn.substring("file://".length());
|
---|
142 | }
|
---|
143 | FileInputStream fin = new FileInputStream(local_file_in);
|
---|
144 | bis = new BufferedInputStream(fin);
|
---|
145 | */
|
---|
146 | URI uri = URI.create (fileIn);
|
---|
147 | Path path = new Path(uri);
|
---|
148 |
|
---|
149 | FSDataInputStream fin = fs.open(path);
|
---|
150 |
|
---|
151 | bis = new BufferedInputStream(fin);
|
---|
152 | }
|
---|
153 |
|
---|
154 | return bis;
|
---|
155 | }
|
---|
156 |
|
---|
157 | protected static String readTextFile(String filename)
|
---|
158 | {
|
---|
159 | StringBuilder sb = new StringBuilder();
|
---|
160 |
|
---|
161 | try {
|
---|
162 | BufferedReader br = ClusterFileIO.getBufferedReaderForCompressedFile(filename);
|
---|
163 |
|
---|
164 | int cp;
|
---|
165 | while ((cp = br.read()) != -1) {
|
---|
166 | sb.append((char) cp);
|
---|
167 | }
|
---|
168 |
|
---|
169 | br.close();
|
---|
170 | }
|
---|
171 | catch (Exception e) {
|
---|
172 | e.printStackTrace();
|
---|
173 | }
|
---|
174 |
|
---|
175 | return sb.toString();
|
---|
176 | }
|
---|
177 |
|
---|
178 | public static BufferedOutputStream getBufferedOutputStream(String fileOut)
|
---|
179 | throws IOException
|
---|
180 | {
|
---|
181 | BufferedOutputStream bos = null;
|
---|
182 |
|
---|
183 | if (fileOut.startsWith("hdfs://")) {
|
---|
184 | URI uri = URI.create (fileOut);
|
---|
185 | Configuration conf = new Configuration();
|
---|
186 | FileSystem file = FileSystem.get(uri, conf);
|
---|
187 | FSDataOutputStream fout = file.create(new Path(uri));
|
---|
188 |
|
---|
189 | bos = new BufferedOutputStream(fout);
|
---|
190 | }
|
---|
191 | else {
|
---|
192 | // Trim 'file://' off the front
|
---|
193 | String local_file_out = fileOut;
|
---|
194 | if (local_file_out.startsWith("file://")) {
|
---|
195 | local_file_out = fileOut.substring("file://".length());
|
---|
196 | }
|
---|
197 | FileOutputStream fout = new FileOutputStream(local_file_out);
|
---|
198 | bos = new BufferedOutputStream(fout);
|
---|
199 | }
|
---|
200 |
|
---|
201 | return bos;
|
---|
202 | }
|
---|
203 |
|
---|
204 | public static BufferedReader getBufferedReaderForCompressedFile(String fileIn)
|
---|
205 | throws IOException, CompressorException
|
---|
206 | {
|
---|
207 | BufferedInputStream bis = getBufferedInputStream(fileIn);
|
---|
208 | CompressorInputStream cis = new CompressorStreamFactory().createCompressorInputStream(bis);
|
---|
209 | BufferedReader br = new BufferedReader(new InputStreamReader(cis,"UTF8"));
|
---|
210 | return br;
|
---|
211 | }
|
---|
212 |
|
---|
213 | public static BufferedWriter getBufferedWriterForCompressedFile(String fileOut)
|
---|
214 | throws IOException, CompressorException
|
---|
215 | {
|
---|
216 | BufferedOutputStream bos = getBufferedOutputStream(fileOut);
|
---|
217 | CompressorStreamFactory csf = new CompressorStreamFactory();
|
---|
218 | CompressorOutputStream cos = csf.createCompressorOutputStream(CompressorStreamFactory.BZIP2,bos);
|
---|
219 | BufferedWriter bw = new BufferedWriter(new OutputStreamWriter(cos,"UTF8"));
|
---|
220 | return bw;
|
---|
221 | }
|
---|
222 |
|
---|
223 | }
|
---|