实验3熟悉常用的HDFS操作答案Word文档下载推荐.docx

上传人:b****5 文档编号:15844711 上传时间:2022-11-16 格式:DOCX 页数:22 大小:18.79KB
下载 相关 举报
实验3熟悉常用的HDFS操作答案Word文档下载推荐.docx_第1页
第1页 / 共22页
实验3熟悉常用的HDFS操作答案Word文档下载推荐.docx_第2页
第2页 / 共22页
实验3熟悉常用的HDFS操作答案Word文档下载推荐.docx_第3页
第3页 / 共22页
实验3熟悉常用的HDFS操作答案Word文档下载推荐.docx_第4页
第4页 / 共22页
实验3熟悉常用的HDFS操作答案Word文档下载推荐.docx_第5页
第5页 / 共22页
点击查看更多>>
下载资源
资源描述

实验3熟悉常用的HDFS操作答案Word文档下载推荐.docx

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

实验3熟悉常用的HDFS操作答案Word文档下载推荐.docx

提示:

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

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

为保证操作正确,可指定路径前缀或者注意区分相对路径与绝对路径

3)具体命令的说明可参考教材或

(1)向HDFS中上传任意文本文件,如果指定的文件在HDFS中已经存在,由用户指定是追加到原有文件末尾还是覆盖原有的文件;

Shell命令:

检查文件是否存在:

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

"

echo$"

追加命令:

./hdfsdfs-appendToFile

覆盖命令1:

./hdfsdfs-copyFromLocal-f

覆盖命令2:

./hdfsdfs-cp-f

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

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

if$(./hdfsdfs-test-e;

then$(./hdfsdfs-appendToFile;

else$(./hdfsdfs-copyFromLocal-f;

fi

Java代码:

import.*;

publicclassHDFSApi{

/**

*判断路径是否存在

*/

publicstaticbooleantest(Configurationconf,Stringpath)throwsIOException{

FileSystemfs=(conf);

return(newPath(path));

}

*复制文件到指定路径

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

publicstaticvoidcopyFromLocalFile(Configurationconf,StringlocalFilePath,StringremoteFilePath)throwsIOException{

PathlocalPath=newPath(localFilePath);

PathremotePath=newPath(remoteFilePath);

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

(false,true,localPath,remotePath);

();

*追加文件内容

publicstaticvoidappendToFile(Configurationconf,StringlocalFilePath,StringremoteFilePath)throwsIOException{

/*创建一个文件读入流*/

FileInputStreamin=newFileInputStream(localFilePath);

/*创建一个文件输出流,输出的内容将追加到文件末尾*/

FSDataOutputStreamout=(remotePath);

/*读写文件内容*/

byte[]data=newbyte[1024];

intread=-1;

while((read=(data))>

0){

(data,0,read);

*主函数

publicstaticvoidmain(String[]args){

Configurationconf=newConfiguration();

("

"

);

StringlocalFilePath="

/home/hadoop/"

;

;

}else{

+"

不存在."

}

/*进行处理*/

if(!

fileExists){

(2)

hdfsdfs-test-e;

then$(./hdfsdfs-copyToLocal./;

else$(./hdfsdfs-copyToLocal./;

*下载文件到本地

*判断本地路径是否已存在,若已存在,则自动进行重命名

publicstaticvoidcopyToLocal(Configurationconf,StringremoteFilePath,StringlocalFilePath)throwsIOException{

Filef=newFile(localFilePath);

/*如果文件名存在,自动重命名(在文件名后面加上_0,_1...)*/

if()){

+"

已存在."

Integeri=0;

while(true){

f=newFile(localFilePath+"

_"

+());

if(!

()){

localFilePath=localFilePath+"

+();

break;

}

"

将重新命名为:

"

+localFilePath);

(3)

hdfsdfs-cat

*读取文件内容

publicstaticvoidcat(Configurationconf,StringremoteFilePath)throwsIOException{

FSDataInputStreamin=(remotePath);

BufferedReaderd=newBufferedReader(newInputStreamReader(in));

Stringline=null;

while((line=())!

=null){

StringremoteFilePath="

/user/hadoop/"

(4)

hdfsdfs-ls-h

importclassHDFSApi{

*显示指定文件的信息

publicstaticvoidls(Configurationconf,StringremoteFilePath)throwsIOException{

FileStatus[]fileStatuses=(remotePath);

for(FileStatuss:

fileStatuses){

路径:

+().toString());

权限:

大小:

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

LongtimeStamp=();

SimpleDateFormatformat=newSimpleDateFormat("

yyyy-MM-ddHH:

mm:

ss"

Stringdate=(timeStamp);

时间:

+date);

(5)

hdfsdfs-ls-R-h/user/hadoop

*显示指定文件夹下所有文件的信息(递归)

publicstaticvoidlsDir(Configurationconf,StringremoteDir)throwsIOException{

PathdirPath=newPath(remoteDir);

/*递归获取目录下的所有文件*/

RemoteIterator<

LocatedFileStatus>

remoteIterator=(dirPath,true);

/*输出每个文件的信息*/

while()){

FileStatuss=();

}

StringremoteD

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

当前位置:首页 > 初中教育 > 学科竞赛

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

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