实验3熟悉常用的HDFS操纵答案解析.docx

上传人:b****4 文档编号:4918308 上传时间:2023-05-07 格式:DOCX 页数:37 大小:3.38MB
下载 相关 举报
实验3熟悉常用的HDFS操纵答案解析.docx_第1页
第1页 / 共37页
实验3熟悉常用的HDFS操纵答案解析.docx_第2页
第2页 / 共37页
实验3熟悉常用的HDFS操纵答案解析.docx_第3页
第3页 / 共37页
实验3熟悉常用的HDFS操纵答案解析.docx_第4页
第4页 / 共37页
实验3熟悉常用的HDFS操纵答案解析.docx_第5页
第5页 / 共37页
实验3熟悉常用的HDFS操纵答案解析.docx_第6页
第6页 / 共37页
实验3熟悉常用的HDFS操纵答案解析.docx_第7页
第7页 / 共37页
实验3熟悉常用的HDFS操纵答案解析.docx_第8页
第8页 / 共37页
实验3熟悉常用的HDFS操纵答案解析.docx_第9页
第9页 / 共37页
实验3熟悉常用的HDFS操纵答案解析.docx_第10页
第10页 / 共37页
实验3熟悉常用的HDFS操纵答案解析.docx_第11页
第11页 / 共37页
实验3熟悉常用的HDFS操纵答案解析.docx_第12页
第12页 / 共37页
实验3熟悉常用的HDFS操纵答案解析.docx_第13页
第13页 / 共37页
实验3熟悉常用的HDFS操纵答案解析.docx_第14页
第14页 / 共37页
实验3熟悉常用的HDFS操纵答案解析.docx_第15页
第15页 / 共37页
实验3熟悉常用的HDFS操纵答案解析.docx_第16页
第16页 / 共37页
实验3熟悉常用的HDFS操纵答案解析.docx_第17页
第17页 / 共37页
实验3熟悉常用的HDFS操纵答案解析.docx_第18页
第18页 / 共37页
实验3熟悉常用的HDFS操纵答案解析.docx_第19页
第19页 / 共37页
实验3熟悉常用的HDFS操纵答案解析.docx_第20页
第20页 / 共37页
亲,该文档总共37页,到这儿已超出免费预览范围,如果喜欢就下载吧!
下载资源
资源描述

实验3熟悉常用的HDFS操纵答案解析.docx

《实验3熟悉常用的HDFS操纵答案解析.docx》由会员分享,可在线阅读,更多相关《实验3熟悉常用的HDFS操纵答案解析.docx(37页珍藏版)》请在冰点文库上搜索。

实验3熟悉常用的HDFS操纵答案解析.docx

实验3熟悉常用的HDFS操纵答案解析

实验2熟悉常用的HDFS操作

1实验目的

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

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

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

2实验平台

操作系统:

Linux

Hadoop版本:

2.6.0或以上版本

JDK版本:

1.6或以上版本

JavaIDE:

Eclipse

3实验内容和要求

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

提示:

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

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

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

///或者file:

///

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

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

//hadoop.apache.org/docs/stable/hadoop-project-dist/hadoop-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.*;

publicclassHDFSApi{

/**

*判断路径是否存在

*/

publicstaticbooleantest(Configurationconf,Stringpath)throwsIOException{

FileSystemfs=FileSystem.get(conf);

returnfs.exists(newPath(path));

}

/**

*复制文件到指定路径

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

*/

publicstaticvoidcopyFromLocalFile(Configurationconf,StringlocalFilePath,StringremoteFilePath)throwsIOException{

FileSystemfs=FileSystem.get(conf);

PathlocalPath=newPath(localFilePath);

PathremotePath=newPath(remoteFilePath);

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

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

fs.close();

}

/**

*追加文件内容

*/

publicstaticvoidappendToFile(Configurationconf,StringlocalFilePath,StringremoteFilePath)throwsIOException{

FileSystemfs=FileSystem.get(conf);

PathremotePath=newPath(remoteFilePath);

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

FileInputStreamin=newFileInputStream(localFilePath);

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

FSDataOutputStreamout=fs.append(remotePath);

/*读写文件内容*/

byte[]data=newbyte[1024];

intread=-1;

while((read=in.read(data))>0){

out.write(data,0,read);

}

out.close();

in.close();

fs.close();

}

/**

*主函数

*/

publicstaticvoidmain(String[]args){

Configurationconf=newConfiguration();

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

//localhost:

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,localFilePath,remoteFilePath);

System.out.println(localFilePath+"已上传至"+remoteFilePath);

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

HDFSApi.copyFromLocalFile(conf,localFilePath,remoteFilePath);

System.out.println(localFilePath+"已覆盖"+remoteFilePath);

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

HDFSApi.appendToFile(conf,localFilePath,remoteFilePath);

System.out.println(localFilePath+"已追加至"+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

Java代码:

importorg.apache.hadoop.conf.Configuration;

importorg.apache.hadoop.fs.*;

importjava.io.*;

publicclassHDFSApi{

/**

*下载文件到本地

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

*/

publicstaticvoidcopyToLocal(Configurationconf,StringremoteFilePath,StringlocalFilePath)throwsIOException{

FileSystemfs=FileSystem.get(conf);

PathremotePath=newPath(remoteFilePath);

Filef=newFile(localFilePath);

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

if(f.exists()){

System.out.println(localFilePath+"已存在.");

Integeri=0;

while(true){

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

if(!

f.exists()){

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

break;

}

}

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

"+localFilePath);

}

//下载文件到本地

PathlocalPath=newPath(localFilePath);

fs.copyToLocalFile(remotePath,localPath);

fs.close();

}

/**

*主函数

*/

publicstaticvoidmain(String[]args){

Configurationconf=newConfiguration();

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

//localhost:

9000");

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

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

try{

HDFSApi.copyToLocal(conf,remoteFilePath,localFilePath);

System.out.println("下载完成");

}catch(Exceptione){

e.printStackTrace();

}

}

}

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

Shell命令:

./hdfsdfs-cattext.txt

Java代码:

importorg.apache.hadoop.conf.Configuration;

importorg.apache.hadoop.fs.*;

importjava.io.*;

publicclassHDFSApi{

/**

*读取文件内容

*/

publicstaticvoidcat(Configurationconf,StringremoteFilePath)throwsIOException{

FileSystemfs=FileSystem.get(conf);

PathremotePath=newPath(remoteFilePath);

FSDataInputStreamin=fs.open(remotePath);

BufferedReaderd=newBufferedReader(newInputStreamReader(in));

Stringline=null;

while((line=d.readLine())!

=null){

System.out.println(line);

}

d.close();

in.close();

fs.close();

}

/**

*主函数

*/

publicstaticvoidmain(String[]args){

Configurationconf=newConfiguration();

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

//localhost:

9000");

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

try{

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

"+remoteFilePath);

HDFSApi.cat(conf,remoteFilePath);

System.out.println("\n读取完成");

}catch(Exceptione){

e.printStackTrace();

}

}

}

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

Shell命令:

./hdfsdfs-ls-htext.txt

Java代码:

importorg.apache.hadoop.conf.Configuration;

importorg.apache.hadoop.fs.*;

importjava.io.*;

importjava.text.SimpleDateFormat;

publicclassHDFSApi{

/**

*显示指定文件的信息

*/

publicstaticvoidls(Configurationconf,StringremoteFilePath)throwsIOException{

FileSystemfs=FileSystem.get(conf);

PathremotePath=newPath(remoteFilePath);

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

for(FileStatuss:

fileStatuses){

System.out.println("路径:

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

System.out.println("权限:

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

System.out.println("大小:

"+s.getLen());

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

LongtimeStamp=s.getModificationTime();

SimpleDateFormatformat=newSimpleDateFormat("yyyy-MM-ddHH:

mm:

ss");

Stringdate=format.format(timeStamp);

System.out.println("时间:

"+date);

}

fs.close();

}

/**

*主函数

*/

publicstaticvoidmain(String[]args){

Configurationconf=newConfiguration();

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

//localhost:

9000");

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

try{

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

"+remoteFilePath);

HDFSApi.ls(conf,remoteFilePath);

System.out.println("\n读取完成");

}catch(Exceptione){

e.printStackTrace();

}

}

}

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

Shell命令:

./hdfsdfs-ls-R-h/user/hadoop

Java代码:

importorg.apache.hadoop.conf.Configuration;

importorg.apache.hadoop.fs.*;

importjava.io.*;

importjava.text.SimpleDateFormat;

publicclassHDFSApi{

/**

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

*/

publicstaticvoidlsDir(Configurationconf,StringremoteDir)throwsIOException{

FileSystemfs=FileSystem.get(conf);

PathdirPath=newPath(remoteDir);

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

RemoteIteratorremoteIterator=fs.listFiles(dirPath,true);

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

while(remoteIterator.hasNext()){

FileStatuss=remoteIterator.next();

System.out.println("路径:

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

System.out.println("权限:

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

System.out.println("大小:

"+s.getLen());

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

LongtimeStamp=s.getModificationTime();

SimpleDateFormatformat=newSimpleDateFormat("yyyy-MM-ddHH:

mm:

ss");

Stringdate=format.format(timeStamp);

System.out.println("时间:

"+date);

System.out.println();

}

fs.close();

}

/**

*主函数

*/

publicstaticvoidmain(String[]args){

Configurationconf=newConfiguration();

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

//localhost:

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

Java代码:

importorg.apache.hadoop.conf.Configuration;

importorg.apache.hadoop.fs.*;

importjava.io.*;

publicclassHDFSApi{

/**

*判断路径是否存在

*/

publicstaticbooleantest(Configurationconf,Stringpath)throwsIOException{

FileSystemfs=FileSystem.get(conf);

returnfs.exists(newPath(path));

}

/**

*创建目录

*/

publicstaticbooleanmkdir(Configurationconf,StringremoteDir)throwsIOException{

FileSystemfs=FileSystem.get(conf);

PathdirPath=new

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

当前位置:首页 > 表格模板

copyright@ 2008-2023 冰点文库 网站版权所有

经营许可证编号:鄂ICP备19020893号-2