HDFS 三 常用客戶端API及IO操作

2021-09-20 07:33:29 字數 3413 閱讀 1000

簡潔**的配置:

將core-site.xml複製到根目錄下,配置如下:

<?xml version="1.0" encoding="utf-8"?>

<?xml-stylesheet type="text/xsl" href="configuration.xsl"?>

>

>

>

fs.defaultfsname

>

>

hdfs://pdc:9000value

>

property

>

>

>

hadoop.tmp.dirname

>

>

/opt/module/hadoop-2.7.2/data/tmpvalue

>

property

>

configuration

>

**模板

@test

public

void

mkdirathdfs()

throws exception

filesystem fs = filesystem.

get(configuration)

;

// 2 建立要上傳檔案所在的本地路徑

path src =

newpath

("e:/pdc.txt");

// 建立要上傳到hdfs的目標路徑

path dst =

newpath

("hdfs://pdc:9000/user/pdc");

// 拷貝檔案,並刪除原始檔

fs.copyfromlocalfile

(true

,src, dst)

;

注:不用hadoop的api進行流操作是因為運算可能會基於spark,更快,用io就不用依賴於hadoop

//建立輸入流

fileinputstream instream =

newfileinputstream

(new

file

("e:/hello.txt"))

;//獲取輸出路徑

string putfilename =

"hdfs://pdc:9000/user/pdc/hello1.txt"

;path writepath =

newpath

(putfilename)

;//建立輸出流

fsdataoutputstream outstream = fs.

create

(writepath)

;// 5 流對接

trycatch

(exception e)

finally

fs.

copytolocalfile

(false

,new

path

("hdfs://pdc:9000/user/atguigu/hello.txt"),

newpath

("e:/hellocopy.txt"),

true

);

//獲取讀取檔案路徑

string filename =

"hdfs://pdc:9000/user/pdc/hello1.txt"

;//建立讀取path

path readpath =

newpath

(filename)

;// 4 建立輸入流

fsdatainputstream instream = fs.

open

(readpath)

;//流對接輸出到控制台

trycatch

(exception e)

finally

fs.

mkdirs

(new

path

("hdfs://pdc:9000/user/pdc/output"))

;

fs.

delete

(new

path

("hdfs://pdc:9000/user/root/output"),

true

);

fs.

rename

(new

path

("hdfs://pdc:9000/user/root/hello.txt"),

newpath

("hdfs://pdc:9000/user/root/hellonihao.txt"))

;

remoteiterator

listfiles = fs.

listfiles

(new

path

("/"),

true);

//注意返回的是迭代器

while

(listfiles.

hasnext()

)}}

//獲取查詢路徑下的檔案狀態資訊

filestatus[

] liststatus = fs.

liststatus

(new

path

("/"))

;//遍歷所有檔案狀態

for(filestatus status : liststatus)

else

}

@test

public

void

readfileseek1()

throws exception

//關閉流

ioutils.

closestream

(fis)

; ioutils.

closestream

(fos)

;}

@test

public

void

readfileseek2()

throws exception

合併檔案

cmd中:

type hadoop-2.7.2.tar.gz.part2 >> hadoop-2.7.2.tar.gz.part1

HDFS 客戶端入門 API

public class doitutils public class uploaddemo public class downdemo public class rename else 改名後的檔案 path path new path b.txt 將檔案移動到指定資料夾下 path path2 ...

hdfs客戶端操作

hdfs的客戶端有多種形式 網頁形式 命令列形式 客戶端在 執行,沒有約束,只要執行客戶端的機器能夠跟hdfs集群聯網 檔案的切塊大小和儲存的副本數量,都是由客戶端決定!所謂的由客戶端決定,是通過配置引數來定的 hdfs的客戶端會讀以下兩個引數,來決定切塊大小 副本數量 切塊大小的引數 dfs.bl...

hdfs客戶端操作

這裡hdfs客戶端指的是eclipse 一 獲取hadoop安裝包解壓後進入share資料夾裡面的所有jar包 二 這裡如果你客戶端 eclipse所在機器 是win7,需要編譯hadoop原始碼來適應你的機器。三 配置hadoop home環境變數和path 四 拷貝win7電腦上的檔案到虛擬機器...