/**
* through Hadoop API Access
* @throws IOException
*/
@Test
Public void Readfilebyapi () throws ioexception{
Configuration conf = new configuration ();
Conf.set ("Fs.defaultfs", "hdfs://192.168.75.201:8020/");
FileSystem fs = FileSystem. Get (conf);
Path PATH = new path ("/user/index.html");
Fsdatainputstream fis =fs.open (path);
byte [] bytes = new byte[1024];
int len =-1;
Bytearrayoutputstream BAOs = new bytearrayoutputstream ();
while (len = fis.read (bytes))!=-1) {
Baos.write (bytes, 0, Len);
}
System. out. println (new String (Baos.tobytearray ()));
Fis.close ();
Baos.close ();
}
The second way:
/**
* through Hadoop API Access
* @throws IOException
*/
@Test
Public void ReadFileByAPI2 () throws ioexception{
Configuration conf = new configuration ();
Conf.set ("Fs.defaultfs", "hdfs://192.168.75.201:8020/");
FileSystem fs = FileSystem. Get (conf);
Path PATH = new path ("/user/index.html");
Fsdatainputstream fis =fs.open (path);
bytearrayoutputstream BAOs = new bytearrayoutputstream ();
Ioutils. copybytes (FIS, BAOs, 1024);
System. out. println (new String (Baos.tobytearray ()));
Fis.close ();
Baos.close ();
}
accessing files via Hadoopapi