Ion,
Thanks! I'll give it a shot in the next couple of days!
-Cesar
Ion Badita wrote:
Cesar Delgado wrote:
Does anyone have an example of how I can read a file that lives in
HDFS from a Servlet?
Thank you for your time,
-Cesar
I would use something like this:
byte[] buffer = new byte[8192];
FileSystem hdfs = FileSystem.get(new Configuration());
Path path = new Path("your/path");
FSDataInputStream in = hdfs.open(path);
ServletOutputStream out = servletResponse.getOutputStream();
/
// you could set the content-type and the length of the file, to
help the browser
// servletResponse.setContentType("file/contentType");
// servletResponse.setContentLength((int)hdfs.getLength(path));/
int readed;
while((readed = in.read(buffer)) != -1) {
out.write(buffer, 0, readed);
}
in.close();
out.flush();
out.close();
You have to make sure that the HDFS is initialized.
The servletResponse is taken from servlet service or get method.
I hope this will help you!
Ion