Access files through hadoopAPI
/ * *
* access via Hadoop api
* @ 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:
/ * *
* access via hadoop api
* @ 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 ()
}