Get the App
SLTechnology News&Howtos  ›  Internet Technology  › 

Hadoop Learning-uploading Files via API-day04

Shulou Source: shulou.com Published: 2022-06-03 05:36:58 10月16日 Update

Import java.io.BufferedInputStream

Import java.io.BufferedOutputStream

Import java.io.ByteArrayOutputStream

Import java.io.FileInputStream

Import java.io.InputStream

Import java.net.URL

Import org.apache.hadoop.conf.Configuration

Import org.apache.hadoop.fs.FSDataInputStream

Import org.apache.hadoop.fs.FSDataOutputStream

Import org.apache.hadoop.fs.FileSystem

Import org.apache.hadoop.fs.FsUrlStreamHandlerFactory

Import org.apache.hadoop.fs.Path

Import org.apache.hadoop.io.IOUtils

Import org.junit.Test

/ * *

* upload files through API

* @ author Administrator

*

, /

Public class putfile {

/ * *

* upload data via API

*

* @ author Administrator

*

* [hadoop@hadoop01 ~] $hdfs dfs-chmod-R aquiw / user/hadoop

* [hadoop@hadoop01 ~] $hdfs dfs-ls-R /

*

*

, /

@ Test

Public void putFile () throws Exception {

/ / create configuration objects, which have a default loading order, first from core-default.xml to the files in the src directory, here

/ / We have given

Configuration conf = new Configuration ()

/ / the distributed file system fs is created through the configuration object of conf. By default, it is the local file system if no file is specified.

FileSystem fs = FileSystem.get (conf)

/ / define a URL string and the directory where the file is uploaded

String file = "hdfs://hadoop01:9000/user/hadoop/data2/shengfenzhao.jpg"

/ / construct a path object from a string of URL

Path path = new Path (file)

FSDataOutputStream out = fs.create (path)

IOUtils.copyBytes (new FileInputStream ("E:/zhaopian.jpg"), out, 1024)

/ / other methods one

/ / byte [] bys = new byte [1024]

/ / int len = 0

/ / while ((len = (new FileInputStream ("E:/zhaopian.jpg")) .read (bys))! =-1) {

/ / out.write (bys, 0, len)

/ /}

/ / other methods two

/ / BufferedInputStream bis = new BufferedInputStream (new FileInputStream ("E:/zhaopian.jpg"))

/ / BufferedOutputStream bos = new BufferedOutputStream (out)

/ / int len = 0

/ / while ((len=bis.read ())! =-1) {

/ / bos.write (len)

/ /}

}

}

Tags: Files objects characters strings methods directories systems distributed data order learning Apple Docker Huawei Linux macOS MariaDB Microsoft MySQL NVidia OPPO Reno Microsoft Redmi Shulou Information Docker Shulou Technology