实验3熟悉常用的HDFS操作答案.docx

上传人:b****8 文档编号:27711063 上传时间:2023-07-04 格式:DOCX 页数:36 大小:713.12KB
下载 相关 举报
实验3熟悉常用的HDFS操作答案.docx_第1页
第1页 / 共36页
实验3熟悉常用的HDFS操作答案.docx_第2页
第2页 / 共36页
实验3熟悉常用的HDFS操作答案.docx_第3页
第3页 / 共36页
实验3熟悉常用的HDFS操作答案.docx_第4页
第4页 / 共36页
实验3熟悉常用的HDFS操作答案.docx_第5页
第5页 / 共36页
点击查看更多>>
下载资源
资源描述

实验3熟悉常用的HDFS操作答案.docx

《实验3熟悉常用的HDFS操作答案.docx》由会员分享,可在线阅读,更多相关《实验3熟悉常用的HDFS操作答案.docx(36页珍藏版)》请在冰豆网上搜索。

实验3熟悉常用的HDFS操作答案.docx

实验3熟悉常用的HDFS操作答案

实验2熟悉常用的HDFS操作

1实验目的

1.理解HDFS在HadooP体系结构中的角色;

2.熟练使用HDFS操作常用的Shell命令;

3.熟悉HDFS操作常用的JaVaAPl。

2实验平台

操作系统:

LinUX

Hadoop版本:

2.6.0或以上版本

JDK版本:

1.6或以上版本

JaVaIDE:

ECIiPSe

3实验内容和要求

1.编程实现以下指定功能,并利用HadooP提供的Shell命令完成相同任务:

提示:

1)部分Shell命令的参数路径只能是本地路径或者HDFS路径。

2)若Shell命令的参数既可以是本地路径,也可以是HDFS路径时,务必注意区分。

为保

证操作正确,可指定路径前缀hdfs:

///或者file:

///

3)注意区分相对路径与绝对路径

4)具体命令的说明可参考教材或http:

//hadoop.apache.org/docs/stable/hadoop-project-dist/had

oop-common/FileSystemShell.html

(1)向HDFS中上传任意文本文件,如果指定的文件在HDFS中已经存在,由用户

指定是追加到原有文件末尾还是覆盖原有的文件;

Shell命令:

检查文件是否存在:

./hdfsdfs-test-etext.txt(执行完这一句不会输出结果,需要继续输入命令

"echo$?

")

追加命令:

./hdfsdfs-appendToFilelocal.txttext.txt

覆盖命令1:

./hdfsdfs-COPyFromLocal-flocal.txttext.txt

覆盖命令2:

./hdfsdfs-CP-ffile:

///home/hadoop/local.txttext.txt

也可以使用如下命令实现:

(如下代码可视为一行代码,在终端中输入第一行代码后,直到输入fi才会真正执行):

if$(./hdfsdfs-test-etext.txt);

then$(./hdfsdfs-appendToFilelocal.txttext.txt);

else$(./hdfsdfs-COPyFromLocal-flocal.txttext.txt);

fi

JaVa代码:

importorg.apache.hadoop.conf.Configuration;

importorg.apache.hadoop.fs.;

importjava.io.*;

PUbIiCclassHDFSAPi{

/

*判断路径是否存在

*/

PUbIiCStatiCbooleantest(COnfiguratiOnconf,StringPath)throwsIOEXCePtiOn{FiIeSyStemfs=FiIeSyStem.get(conf);

returnfs.exists(newPath(Path));

}

/**

复制文件到指定路径

若路径已存在,则进行覆盖

*/

PUbIiCStatiCvoidCOPyFromLocalFile(COnfiguratiOnconf,StringlocalFilePath,String

remoteFilePath)throwsIOEXCePtiOn{

FiIeSyStemfs=FiIeSyStem.get(conf);

PathlocalPath=newPath(IOcalFilePath);

PathremotePath=newPath(remoteFilePath);

/*fs.copyFromLocalFile第一个参数表示是否删除源文件,第二个参数表示是否覆

盖*/

fs.copyFromLocalFile(false,true,localPath,remotePath);

fs.close();

}

out.close();

in.close();

fs.close();

}

*主函数

*/

PUbIiCStatiCvoidmain(String[]args){

ConfiguratiOnConf=newConfiguration();

Conf.set("fs.default.name","hdfs:

//IoCaIhoSt:

9000");

StringlocalFilePath="/home/hadoop/text.txt";//本地路径

StringremoteFilePath="∕user∕hadoop∕text.txt";//HDFS路径

StringChOiCe="append";//若文件存在则追加到文件末尾

//StringChOiCe="overwrite";//若文件存在则覆盖

try{

/*判断文件是否存在*/

BooleanfileExists=false;

if(HDFSAPi.test(conf,remoteFilePath)){

fileExists=true;

SyStem.out.println(remoteFilePath+"已存在.");

}else{

SyStem.out.println(remoteFilePath+"不存在.");

}

/*进行处理*/

if(!

fileExists){//文件不存在,则上传

HDFSAPi.copyFromLocalFile(conf,IocaIFiIePath,remoteFilePath);

SyStem.out.println(IoCaIFilePath+"已上传至"+remoteFilePath);

}elseif(ChOiCe.equals("overwrite")){//选择覆盖

HDFSAPi.copyFromLocaIFiIe(conf,IocaIFiIePath,remoteFilePath);

SyStem.out.println(IoCaIFilePath+"已覆盖"+remoteFilePath);

}elseif(ChOiCe.equals("append")){//选择追加

HDFSAPi.appendToFiIe(conf,IocaIFiIePath,remoteFilePath);

SyStem.out.println(IoCaIFilePath+"已追加至"+remoteFilePath);

}

}CatCh(EXCePtiOne){

e.printStackTrace();

}

}

}

(2)从HDFS中下载指定文件,如果本地文件与要下载的文件名称相同,则自动对

下载的文件重命名;

Shell命令:

if$(./hdfsdfs-test-efile:

///home/hadoop/text.txt);

then$(./hdfsdfs-COPyToLocaltext.txt./text2.txt);

else$(./hdfsdfs-COPyToLocaltext.txt./text.txt);

fi

Integeri=0;

while(true){

f=newFile(IoCalFiIePath+"_"+i.toString());

if(!

f.exists()){

localFilePath=localFilePath+"_"+i.toString();break;

}

}

SyStem.out.println("将重新命名为:

"+localFilePath);

}

//下载文件到本地

PathlocalPath=newPath(IocalFilePath);fs.copyToLocalFile(remotePath,IocaIPath);fs.close();

}

*主函数

*/

PUbIiCStatiCvoidmain(String[]args){

ConfiguratiOnConf=newConfiguration();

Conf.set("fs.default.name","hdfs:

//IoCaIhoSt:

9000");

StringIocaIFiIePath="/home/hadoop/text.txt";//本地路径

StringremoteFilePath="/user/hadoop/text.txt";//HDFS路径

try{

HDFSAPi.copyToLocaI(conf,remoteFilePath,IocaIFiIePath);SyStem.out.println("下载完成");

}CatCh(EXCePtiOne){

e.printStackTrace();

}

}

}

(3)将HDFS中指定文件的内容输出到终端中;

Shell命令:

.∕hdfsdfs-Cattext.txt

*主函数

*/

PUbIiCStatiCvoidmain(String[]args){

ConfiguratiOnConf=newConfiguration();

Conf.set("fs.default.name","hdfs:

//IoCaIhoSt:

9000");

StringremoteFilePath="/user/hadoop/text.txt";//HDFS路径try{

SyStem.out.println("读取文件:

"+remoteFilePath);

HDFSAPi.cat(conf,remoteFilePath);

SyStem.out.println("∖n读取完成");

}CatCh(EXCePtiOne){

e.printStackTrace();

}

}

}

EW⅛⅛⅞j⅛⅛:

l⅛f⅛a⅛fa⅜∙⅝⅞⅛l⅛∙S⅛ril⅛M⅜⅛α祕⅛⅛1∣⅝d⅛h⅜⅞

3iMSF⅛ψ∣μ41⅛f⅛pi

刁raι5l.

4■M*F4>E⅞kvcLLH.-fl!

μ=-ι

Rffly

iM⅛lfcEhinΦΓU⅛>⅞I

(4)显示HDFS中指定的文件的读写权限、大小、创建时间、路径等信息;

Shell命令:

.∕hdfsdfs-IS-htext.txt

importjava.text.SimpleDateFormat;

PUbIiCclassHDFSAPi{

/**

*显示指定文件的信息

*/

PUbIiCStatiCvoidls(COnfiguratiOnconf,StringremoteFilePath)throwsIOEXCePtion{

FiIeSyStemfs=FiIeSyStem.get(conf);

PathremotePath=newPath(remoteFilePath);

FiIeStatus[]fileStatuses=fs.listStatus(remotePath);

for(FiIeStatUSS:

fileStatuses){

SyStem.out.println("路径:

"+s.getPath().toString());

SyStem.out.println("权限:

"+s.getPermission().toString());

SyStem.out.println("大小:

"+s.getLen());

/*返回的是时间戳,转化为时间日期格式*/

LongtimeStamp=s.getModificatiOnTime();

SimPIeDateFOrmatformat=newSimPIeDateFormat("yyyy-MM-ddHH:

mm:

ss");

Stringdate=format.format(timeStamp);

SyStem.out.println("时间:

"+date);

}

fs.close();

}

*主函数

*/

PUbIiCStatiCvoidmain(String[]args){

ConfiguratiOnConf=newConfiguration();

Conf.set("fs.default.name","hdfs:

//IoCaIhoSt:

9000");

StringremoteFilePath="/user/hadoop/text.txt";//HDFS路径

try{

SyStem.out.println("读取文件信息:

"+remoteFilePath);

HDFSAPi.ls(conf,remoteFilePath);

SyStem.out.println("∖n读取完成");

}CatCh(EXCePtiOne){

e.printStackTrace();

}

}

}

卜gM⅝3rπp43L2.M.>iIIlim∣∣∣∣∣gIlrtIVl

■-昌jπvv-uL9-Jm平ητ-r-

FC≠≡π⅛aα>Hfl:

IaQlMlIOM:

*审

卜EIlirMy-^>M-1⅞∣M.J

MP⅛wJ⅛∙,j∙κi∣κ∣∣JIrB<

"⅛⅜⅝pιl⅛j⅝e-ιOJii-彳

(5)给定HDFS中某一个目录,输出该目录下的所有文件的读写权限、大小、创建时间、路径等信息,如果该文件是目录,则递归输出该目录下所有文件相关信息;

Shell命令:

.∕hdfsdfs-IS-R-h∕user∕hadoop

SyStem.out.println("大小:

"+s.getLen());

/*返回的是时间戳,转化为时间日期格式*/

LongtimeStamp=s.getModificatiOnTime();

SimPIeDateFOrmatformat=newSimPIeDateFormat("yyyy-MM-ddHH:

mm:

ss");

Stringdate=format.format(timeStamp);

SyStem.out.println("时间:

"+date);

SyStem.out.println();

}

fs.close();

}

/**

*主函数

*/

PUbIiCStatiCvoidmain(String[]args){

ConfiguratiOnConf=newConfiguration();

Conf.set("fs.default.name","hdfs:

//IoCaIhoSt:

9000");

StringremoteDir="/user/hadoop";//HDFS路径

try{

SyStem.out.println("(递归)读取目录下所有文件的信息:

"+remoteDir);

HDFSAPi.lSDir(COnf,remoteDir);

SyStem.out.println("读取完成");

}CatCh(EXCePtiOne){

e.printStackTrace();

}

}

}

(6)提供一个HDFS内的文件的路径,对该文件进行创建和删除操作。

如果文件所

在目录不存在,则自动创建目录;

Shell命令:

if$(./hdfsdfs-test-ddir1∕dir2);

then$(./hdfsdfs-touchzdir1∕dir2∕filename);

else$(./hdfsdfs-mkdir-Pdir1∕dir2&&hdfsdfs-touchzdir1∕dir2∕filename);

fi删除文件:

./hdfsdfs-rmdir1∕dir2∕filename

PUbliCStatiCbooleanmkdir(COnfiguratiOnconf,StringremoteDir)throwsIoEXCePtiOn{FileSyStemfs=FiIeSyStem.get(conf);

PathdirPath=newPath(remoteDir);

booleanresult=fs.mkdirs(dirPath);

fs.close();

returnresult;

*/

创建文件

PUbIiCStatiCvoidtouchz(Configurationconf,StringremoteFilePath)throwsIOEXCePtion{

FiIeSyStemfs=FiIeSyStem.get(conf);

PathremotePath=newPath(remoteFilePath);

FSDataOUtPUtStreamOUtPUtStream=fs.create(remotePath);

OUtPUtStream.close();

fs.close();

}

*删除文件

*/

PUbIiCStatiCbooleanrm(COnfiguratiOnconf,StringremoteFilePath)throwsIOEXCePtiOn{FiIeSyStemfs=FiIeSyStem.get(conf);

PathremotePath=newPath(remoteFiIePath);booleanresult=fs.deIete(remotePath,false);fs.close();

returnresult;

}

*主函数

*/

PUbIiCStatiCvoidmain(String[]args){

ConfiguratiOnConf=newConfiguration();

Conf.set("fs.default.name","hdfs:

//IoCaIhoSt:

9000");

StringremoteFilePath="/user/hadoop/input/text.txt";//HDFS路径

StringremoteDir="/user/hadoop/input";//HDFS路径对应的目录

try{

/*判断路径是否存在,存在则删除,否则进行创建*/

if(HDFSAPi.test(conf,remoteFilePath)){

HDFSAPi.rm(conf,remoteFilePath);//删除

SyStem.out.println("删除路径:

"+remoteFilePath);

}else{

if(!

HDFSApi.test(conf,remoteDir)){//若目录不存在,则进行创建

HDFSAPi.mkdir(conf,remoteDir);

SyStem.out.println("创建文件夹:

"+remoteDir);

}

HDFSAPi.touchz(conf,remoteFilePath);

SyStem.out.println("创建路径:

"+remoteFilePath);

}

}CatCh(EXCePtiOne){

e.printStackTrace();

}

}

}

(7)提供一个HDFS的目录的路径,对该目录进行创建和删除操作。

创建目录时,

如果目录文件所在目录不存在则自动创建相应目录;删除目录时,由用户指定当该目录不为空时是否还删除该目录;

Shell命令:

创建目录:

.∕hdfsdfs-mkdir-Pdir1∕dir2

删除目录(如果目录非空则会提示notempty,不执行删除):

./hdfsdfs-rmdirdir1∕dir2强制删除目录:

./hdfsdfs-rm-Rdir1∕dir2

JaVa代码:

importorg.apache.hadoop.conf.Configuration;

importorg.apache.hadoop.fs.*;

importjava.io.*;

PUbIiCclassHDFSAPi{

/**

*判断路径是否存在

*/

PUbIiCStatiCbooleantest(COnfiguratiOnconf,StringPath)throwsIOEXCePtiOn{FiIeSyStemfs=FiIeSyStem.get(conf);

returnfs.exists(newPath(Path));

}

*判断目录是否为空

*true:

空,false:

非空

*/

PUbIiCStatiCbooleanisDirEmpty(COnfiguratiOnConf,StringremoteDir)throwsIOEXCePtiOn{

FiIeSyStemfs=FiIeSyStem.get(conf);

PathdirPath=newPath(remoteDir);

RemOteIteratorremoteIterator=fs.IistFiIes(dirPath,true);

return!

remotelterator.hasNext();

*/

创建目录

PUbIiCStatiCbooleanmkdir(COnfiguratiOnconf,StringremoteDir)throwsIOEXCePtiOn{FiIeSyStemfs=FiIeSyStem.get(conf);

PathdirPath=newPath(remoteDir);

booleanresult=fs.mkdirs(dirPath);

fs.close();

returnresult;

}

/**

*删除目录

*/

PUbIiCStatiCbooleanrmDir(COnfiguratiOnconf,StringremoteDir)throwsIOEXCePtiOn{FiIeSyStemfs=FiIeSyStem.get(conf);

PathdirPath=newPath(remoteDir);

/*第二个参数表示是否递归删除所有文件*/

booleanresult=fs.deIete(dirPath,true);

fs.close();

retur

展开阅读全文
相关资源
猜你喜欢
相关搜索

当前位置:首页 > 总结汇报 > 其它

copyright@ 2008-2022 冰豆网网站版权所有

经营许可证编号:鄂ICP备2022015515号-1