Skip to content

Commit 37d02a8

Browse files
authored
Merge pull request #2 from gewanbo/master
Add load data from HDFS
2 parents 99fdc39 + 1ff188e commit 37d02a8

File tree

1 file changed

+25
-0
lines changed

1 file changed

+25
-0
lines changed

src/main/java/qiniu/ip17mon/Locator.java

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -154,6 +154,31 @@ public static Locator loadFromLocal(String filePath) throws IOException {
154154

155155
return loadBinary(b);
156156
}
157+
158+
public static Locator loadFromHDFS(String dfsPath) throws Exception {
159+
Configuration conf = new Configuration();
160+
FileSystem fs = FileSystem.get(conf);
161+
162+
ByteArrayOutputStream byteArrayOutputStream = new ByteArrayOutputStream();
163+
164+
Path filePath = new Path(dfsPath);
165+
166+
if(fs.exists(filePath)){
167+
FSDataInputStream in = fs.open(filePath);
168+
byte[] buffer = new byte[1];
169+
170+
while (in.read(buffer) != -1){
171+
byteArrayOutputStream.write(buffer);
172+
}
173+
} else {
174+
throw new Exception("File does not exists!");
175+
}
176+
177+
// Using in Spark, don't close it.
178+
//fs.close();
179+
180+
return loadBinary(byteArrayOutputStream.toByteArray());
181+
}
157182

158183
public static Locator loadBinary(byte[] ipdb) {
159184
return new Locator(ipdb);

0 commit comments

Comments
 (0)