Get the App
SLTechnology News&Howtos  ›  Internet Technology  › 

Access files through hadoopAPI

Shulou Source: shulou.com Published: 2022-06-03 05:37:13 11月24日 Update

/ * *

* 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 ()

}

Tags: Mode file Apple Docker Huawei Linux macOS MariaDB Microsoft MySQL NVidia OPPO Reno Redmi vpn Linux MariaDB Microsoft