You are viewing a plain text version of this content. The canonical link for it is here.
Posted to common-user@hadoop.apache.org by Cesar Delgado <cd...@bigred.unl.edu> on 2007/06/02 21:09:15 UTC
HDFS and Servlets
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
Re: HDFS and Servlets
Posted by Cesar Delgado <cd...@bigred.unl.edu>.
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
>
Re: HDFS and Servlets
Posted by Ion Badita <io...@mcr.ro>.
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