试验3熟悉常用的HDFS操作答案_第1页
试验3熟悉常用的HDFS操作答案_第2页
试验3熟悉常用的HDFS操作答案_第3页
试验3熟悉常用的HDFS操作答案_第4页
试验3熟悉常用的HDFS操作答案_第5页
免费预览已结束,剩余13页可下载查看

下载本文档

版权说明:本文档由用户提供并上传,收益归属内容提供方,若内容存在侵权,请进行举报或认领

文档简介

1、实验2熟悉常用的HDFS操作 1实验目的 1. 理解HDFS在Hadoop体系结构中的角色; 2. 熟练使用HDFS操作常用的Shell命令; 3. 熟悉HDFS操作常用的JavaAPI. 2实验平台 操作系统:Linux Hadoop版本: JDK版本:1.6或以上版本 JavaIDE:Eclipse 3实验内容和要求 (1) 一文本文件,如果指定的文件在HDFS中已经存在,由用户 指定是追加到原有文件末尾还是覆盖原有的文件; Shell命令: 检查文件是否存在:./hdfsdfs-test-etext.txt(执行完这一句不会输出结果,需要继续输入命令 echo$?) 追加命令:./hdf

2、sdfs-appendToFilelocal.txttext.txt 覆盖命令1:./hdfsdfs-copyFromLocal-flocal.txttext.txt 覆盖命令2:./hdfsdfs-cp-ftext.txt 也可以使用如下命令实现: (如下代码可视为一行代码,在终端中输入第一行代码后,直到输入 if$(./hdfsdfs-test-etext.txt); then$(./hdfsdfs-appendToFilelocal.txttext.txt); else$(./hdfsdfs-copyFromLocal-flocal.txttext.txt);fi Java代码:impo

3、rt;import;importjava.io.*;publicclassHDFSApi/* *判断路径是否存在1) 2) 或者 3) 4) /stable/hadoop-project-dist/hadoop-common/FileSystemShel fi才会真正执行): 1.编程实现以下指定功能,并利用Hadoop提供的Shell命令完成相同任务: 提示: 局部Shell命令的参数路径只能是本地路径或者HDFS路径. 假设Shell命令的参数既可以是本地路径,也可以是HDFS路径时,务必注意区分.为保 证操作正确,可指定路径前缀注意区分相对路径与绝对路径具体命令的说明可参考教材或l.ht

4、ml */ publicstaticbooleantest(Configurationconf,Stringpath)throwslOException( FileSystemfs=FileSystem.get(conf); returnfs.exists(newPath(path); /* *复制文件到指定路径 *假设路径已存在,那么进行覆盖 */ publicstaticvoidcopyFromLocalFile(Configurationconf,StringlocalFilePath,StringremoteFilePath)throwsIOException( FileSystemf

5、s=FileSystem.get(conf); PathlocalPath=newPath(localFilePath); PathremotePath=newPath(remoteFilePath); /*fs.copyFromLocalFile第一个参数表示是否删除源文件,第二个参数表示是否覆 盖*/ fs.copyFromLocalFile(false,true,localPath,remotePath);fs.close(); /* 追加文件内容 */ publicstaticvoidappendToFile(Configurationconf,StringlocalFilePath,

6、StringremoteFilePath)throwsIOException( FileSystemfs=FileSystem.get(conf); PathremotePath=newPath(remoteFilePath); /*创立一个文件读入流*/ FileInputStreamin=newFileInputStream(localFilePath); /*创立一个文件输出流,输出的内容将追加到文件末尾 FSDataOutputStreamout=fs.append(remotePath); /*读写文件内容*/ bytedata=newbyte1024; intread=-1; wh

7、ile(read=in.read(data)0)( out.write(data,0,read); out.close(); in.close(); fs.close(); /* *主函数 */ publicstaticvoidmain(Stringargs)(*/ Configurationconf=newConfiguration.; conf.set(,); StringlocalFilePath=/home/hadoop/text.txt; StringremoteFilePath=/user/hadoop/text.txt; Stringchoice=append; /Stringc

8、hoice=overwrite; try( /*判断文件是否存在*/ BooleanfileExists=false; if(HDFSApi.test(conf,remoteFilePath)( fileExists=true; +已存在.); else( +不存在.); /*进行处理*/ if(!fileExists)(/文件不存在,那么上传 HDFSApi.copyFromLocalFile(conf,localFilePath,remoteFilePath); +已上传至+remoteFilePath); elseif(choice.equals(overwrite)(/选择覆盖 HDF

9、SApi.copyFromLocalFile(conf,localFilePath,remoteFilePath); +已覆盖+remoteFilePath); elseif(choice.equals(append)(/选择追加 HDFSApi.appendToFile(conf,localFilePath,remoteFilePath); +已追加至+remoteFilePath); catch(Exceptione)( e.printStackTrace(); (1)从HDFS中下载指定文件,如果本地文件与要下载的文件名称相同,那么自动对下载的文件重命名; Shell命令: if$(./

10、hdfsdfs-test-e; then$(./hdfsdfs-copyToLocaltext.txt./text2.txt); else$(./hdfsdfs-copyToLocaltext.txt./text.txt); fi Java代码: import; import; importjava.io.*; publicclassHDFSApi(/* *下载文件到本地 *判断本地路径是否已存在,假设已存在,那么自动进行重命名 */ /本地路径 /HDFS路径 /假设文件存在那么追加到文件末尾 /假设文件存在那么覆盖 publicstaticvoidcopyToLocal(Configura

11、tionconf,StringremoteFilePath,StringlocalFilePath)throwsIOException FileSystemfs=FileSystem.get(conf); PathremotePath=newPath(remoteFilePath); Filef=newFile(localFilePath); /*如果文件名存在,自动重命名(在文件名后面加上_0,_1.)*/ if(f.exists() +已存在.); Integeri=0; while(true) f=newFile(localFilePath+_+i.toString(); if(!f.e

12、xists() localFilePath=localFilePath+_+i.toString(); break; 将重新命名为:+localFilePath); /下载文件到本地 PathlocalPath=newPath(localFilePath); fs.copyToLocalFile(remotePath,localPath); fs.close(); /* *主函数 */ publicstaticvoidmain(Stringargs) Configurationconf=newConfiguration(); conf.set(,); StringlocalFilePath=/

13、home/hadoop/text.txt;/本地路径 StringremoteFilePath=/user/hadoop/text.txt;/HDFS路径 try HDFSApi.copyToLocal(conf,remoteFilePath,localFilePath); 下载完成); catch(Exceptione) e.printStackTrace(); (2) 将HDFS中指定文件的内容输出到终端中; Shell命令: ./hdfsdfs-cattext.txt Java代码:import; import; importjava.io.*; publicclassHDFSApi /

14、* *读取文件内容 */ publicstaticvoidcat(Configurationconf,StringremoteFilePath)throwsIOExceptionFileSystemfs=FileSystem.get(conf); PathremotePath=newPath(remoteFilePath); FSDataInputStreamin=fs.open(remotePath); BufferedReaderd=newBufferedReader(newInputStreamReader(in); Stringline=null; while(line=d.readL

15、ine()!=null) 5 d.close(); in.close(); fs.close(); /* *主函数 */ publicstaticvoidmain(Stringargs) Configurationconf=newConfiguration(); conf.set(,); StringremoteFilePath=/user/hadoop/text.txt;/HDFS路径try 读取文件:+remoteFilePath); HDFSApi.cat(conf,remoteFilePath); n读取完成); catch(Exceptione) e.printStackTrace(

16、); (3) 显示HDFS中指定的文件的读写权限、大小、创立时间、路径等信息; Shell命令: ./hdfsdfs-ls-htext.txt Java代码: import; import; importjava.io.*; import; publicclassHDFSApi( /* *显不指定文件的信息 */ publicstaticvoidls(Configurationconf,StringremoteFilePath)throwsIOException(FileSystemfs=FileSystem.get(conf); PathremotePath=newPath(remoteFi

17、lePath); FileStatusfileStatuses=fs.listStatus(remotePath);for(FileStatuss:fileStatuses)( 路径:+s.getPath().toString(); 权限:+s.getPermission().toString(); 大小:+s.getLen(); /*返回的是时间戳,转化为时间日期格式 LongtimeStamp=s.getModificationTime(); SimpleDateFormatformat=newSimpleDateFormat(yyyy-MM-ddHH:mm:ss);Stringdate=

18、format.format(timeStamp); 时间:+date); fs.close(); /* *主函数 */ publicstaticvoidmain(Stringargs)( Configurationconf=newConfiguration(); conf.set(,); StringremoteFilePath=/user/hadoop/text.txt; try( 读取文件信息:+remoteFilePath); HDFSApi.ls(conf,remoteFilePath); n读取完成); catch(Exceptione)(e.printStackTrace(); (

19、4) 给定HDFS中某一个目录,输出该目录下的所有文件的读写权限、大小、创立 时间、路径等信息,如果该文件是目录,那么递归输出该目录下所有文件相关信息; Shell命令: ./hdfsdfs-ls-R-h/user/hadoop Java代码:*/ /HDFS路径 import; import; importjava.io.*; import; publicclassHDFSApi( /* *显示指定文件夹下所有文件的信息(递归) */ publicstaticvoidlsDir(Configurationconf,StringremoteDir)throwsIOException( File

20、Systemfs=FileSystem.get(conf); PathdirPath=newPath(remoteDir); /*递归获取目录下的所有文件*/ RemoteIteratorremoteIterator=fs.listFiles(dirPath,true); /*输出每个文件的信息*/ while(remoteIterator.hasNext()( FileStatuss=remoteIterator.next(); 路径:+s.getPath().toString(); 权限:+s.getPermission().toString(); 大小:+s.getLen(); /*返回

21、的是时间戳,转化为时间日期格式 LongtimeStamp=s.getModificationTime(); SimpleDateFormatformat=newSimpleDateFormat(yyyy-MM-ddHH:mm:ss); Stringdate=format.format(timeStamp); 时间:+date); 5 fs.close(); /* *主函数 */ publicstaticvoidmain(Stringargs)( Configurationconf=newConfiguration(); conf.set(,); StringremoteDir=/user/h

22、adoop;/HDFS路径try( (递归)读取目录下所有文件的信息:+remoteDir); HDFSApi.lsDir(conf,remoteDir); 读取完成); catch(Exceptione)( e.printStackTrace(); */ (5) 提供一个HDFS内的文件的路径,对该文件进行创立和删除操作.如果文件所 在目录不存在,那么自动创立目录; Shell命令: if$(./hdfsdfs-test-ddir1/dir2); then$(./hdfsdfs-touchzdir1/dir2/filename); else$(./hdfsdfs-mkdir-pdir1/di

23、r2&hdfsdfs-touchzdir1/dir2/filename); fi 删除文件:./hdfsdfs-rmdir1/dir2/filename Java代码: import; import; importjava.io.*; publicclassHDFSApi /* *判断路径是否存在 */ publicstaticbooleantest(Configurationconf,Stringpath)throwsIOExceptionFileSystemfs=FileSystem.get(conf); returnfs.exists(newPath(path); /* * */ pub

24、licstaticbooleanmkdir(Configurationconf,StringremoteDir)throwsIOException FileSystemfs=FileSystem.get(conf); PathdirPath=newPath(remoteDir); booleanresult=fs.mkdirs(dirPath); fs.close(); returnresult; /* 创立文件 */ publicstaticvoidtouchz(Configurationconf,StringremoteFilePath)throwsIOException FileSyst

25、emfs=FileSystem.get(conf); PathremotePath=newPath(remoteFilePath); FSDataOutputStreamoutputStream=fs.create(remotePath); outputStream.close(); fs.close(); /* 删除文件 */ publicstaticbooleanrm(Configurationconf,StringremoteFilePath)throwslOException FileSystemfs=FileSystem.get(conf); PathremotePath=newPa

26、th(remoteFilePath); booleanresult=fs.delete(remotePath,false); 创立目录 fs.close(); returnresult; /* *主函数 */ publicstaticvoidmain(Stringargs) Configurationconf=newConfiguration(); conf.set(,); StringremoteFilePath=/user/hadoop/input/text.txt;/HDFS路径 StringremoteDir=/user/hadoop/input;/HDFS路径对应的目录 try /*

27、判断路径是否存在,存在那么删除,否那么进行创立*/ if(HDFSApi.test(conf,remoteFilePath)HDFSApi.rm(conf,remoteFilePath);/删除删除路径:+remoteFilePath); else if(!HDFSApi.test(conf,remoteDir)/假设目录不存在,那么进行创立HDFSApi.mkdir(conf,remoteDir); 创立文件夹:+remoteDir); HDFSApi.touchz(conf,remoteFilePath); 创立路径:+remoteFilePath); catch(Exceptione)e

28、.printStackTrace(); (6) 提供一个HDFS的目录的路径,对该目录进行创立和删除操作.创立目录时, 如果目录文件所在目录不存在那么自动创立相应目录;删除目录时,由用户指定当该目录不为空时是否还删除该目录; Shell命令: 创立目录:./hdfsdfs-mkdir-pdir1/dir2 删除目录(如果目录非空那么会提示notempty,不执行删除):./hdfsdfs-rmdirdir1/dir2 强制删除目录:./hdfsdfs-rm-Rdir1/dir2 Java代码: import; import; importjava.io.*; publicclassHDFSAp

29、i *判断路径是否存在 */ publicstaticbooleantest(Configurationconf,Stringpath)throwslOException(FileSystemfs=FileSystem.get(conf); returnfs.exists(newPath(path); 判断目录是否为空 *true:空,false:非空 */ publicstaticbooleanisDirEmpty(Configurationconf,StringremoteDir)throwsIOException( FileSystemfs=FileSystem.get(conf); P

30、athdirPath=newPath(remoteDir); RemoteIteratorremoteIterator=fs.listFiles(dirPath,true); return!remoteIterator.hasNext(); /* *创立目录 */ publicstaticbooleanmkdir(Configurationconf,StringremoteDir)throwsIOException( FileSystemfs=FileSystem.get(conf); PathdirPath=newPath(remoteDir); booleanresult=fs.mkdir

31、s(dirPath); fs.close(); returnresult; /* *删除目录 */ publicstaticbooleanrmDir(Configurationconf,StringremoteDir)throwsIOException(FileSystemfs=FileSystem.get(conf); PathdirPath=newPath(remoteDir); /*第二个参数表示是否递归删除所有文件*/ booleanresult=fs.delete(dirPath,true); fs.close(); returnresult; /* *主函数 */ publicst

32、aticvoidmain(Stringargs)( Configurationconf=newConfiguration(); conf.set(,); StringremoteDir=/user/hadoop/input;/HDFS目录 BooleanforceDelete=false;/是否强制删除 try /* /*判断目录是否存在,不存在那么创立,存在那么删除*/ if(!HDFSApi.test(conf,remoteDir) HDFSApi.mkdir(conf,remoteDir);/创立目录 创立目录:+remoteDir); else if(HDFSApi.isDirEmpt

33、y(conf,remoteDir)|forceDelete)/目录为空或强制删除HDFSApi.rmDir(conf,remoteDir); 删除目录:+remoteDir); else/目录不为空 目录不为空,不删除:+remoteDir); catch(Exceptione) e.printStackTrace(); (7) 向HDFS中指定的文件追加内容,由用户指定内容追加到原有文件的开头或结 尾; Shell命令: 追加到文件末尾:./hdfsdfs-appendToFilelocal.txttext.txt 追加到文件开头: (由于没有直接的命令可以操作,方法之一是先移动到本地进行操

34、作,再进行上传覆盖): ./hdfsdfs-gettext.txt cattext.txtlocal.txt ./hdfsdfs-copyFromLocal-ftext.txttext.txt Java代码: import; import; importjava.io.*; publicclassHDFSApi /* *判断路径是否存在 */ publicstaticbooleantest(Configurationconf,Stringpath)throwsIOExceptionFileSystemfs=FileSystem.get(conf); returnfs.exists(newPat

35、h(path); /* 追加文本内容 */ publicstaticvoidappendContentToFile(ConfigurationremoteFilePath)throwsIOException FileSystemfs=FileSystem.get(conf); PathremotePath=newPath(remoteFilePath); /*创立一个文件输出流,输出的内容将追加到文件末尾 FSDataOutputStreamout=fs.append(remotePath); out.write(content.getBytes(); out.close(); fs.clos

36、e(); conf,Stringcontent, */ String /* 追加文件内容 */ publicstaticvoidappendToFile(Configurationconf,remoteFilePath)throwsIOException FileSystemfs=FileSystem.get(conf); PathremotePath=newPath(remoteFilePath); /*创立一个文件读入流*/ FileInputStreamin=newFileInputStream(localFilePath); /*创立一个文件输出流,输出的内容将追加到文件末尾 FSDa

37、taOutputStreamout=fs.append(remotePath); /*读写文件内容*/ bytedata=newbyte1024; intread=-1; while(read=in.read(data)0) out.write(data,0,read); out.close(); in.close(); fs.close(); StringlocalFilePath, String */ /* 移动文件到本地 移动后,删除源文件 */ publicstaticvoidmoveToLocalFile(Configurationconf,localFilePath)throwsI

38、OException FileSystemfs=FileSystem.get(conf); PathremotePath=newPath(remoteFilePath); PathlocalPath=newPath(localFilePath); fs.moveToLocalFile(remotePath,localPath); /* StringremoteFilePath, String *创立文件 */ publicstaticvoidtouchz(Configurationconf,StringremoteFilePath)throwslOException( FileSystemfs

39、=FileSystem.get(conf); PathremotePath=newPath(remoteFilePath); FSDataOutputStreamoutputStream=fs.create(remotePath); outputStream.close(); fs.close(); /* *主函数 */ publicstaticvoidmain(Stringargs)( Configurationconf=newConfiguration(); conf.set(,); StringremoteFilePath=/user/hadoop/text.txt;/HDFS文件 St

40、ringcontent=新追加的内容n; Stringchoice=after;追加到文件末尾 /Stringchoice=before;/追加到文件开头try( /*判断文件是否存在*/ if(!HDFSApi.test(conf,remoteFilePath)( 文件不存在:+remoteFilePath); else( if(choice.equals(after)(/追加在文件末尾 HDFSApi.appendContentToFile(conf,content,remoteFilePath); 已追加内容到文件末尾+remoteFilePath); elseif(choice.equ

41、als(before)(/追加到文件开头 /*没有相应的api可以直接操作,因此先把文件移动到本地,创立一个新的 HDFS,再按顺序追加内容*/ StringlocalTmpPath=/user/hadoop/tmp.txt; HDFSApi.moveToLocalFile(conf,remoteFilePath,localTmpPath);/移动到本地 HDFSApi.touchz(conf,remoteFilePath);/创立一个新文件HDFSApi.appendContentToFile(conf,content,remoteFilePath);/先写入新内容 HDFSApi.appe

42、ndToFile(conf,localTmpPath,remoteFilePath);/再写入原来内 已追加内容到文件开头:+remoteFilePath); catch(Exceptione)( e.printStackTrace(); (8) 删除HDFS中指定的文件; Shell命令: ./hdfsdfs-rmtext.txt Java命令: import; import; importjava.io.*; publicclassHDFSApi /* *删除文件 */ publicstaticbooleanrm(Configurationconf,StringremoteFilePath

43、)throwsIOException FileSystemfs=FileSystem.get(conf); PathremotePath=newPath(remoteFilePath); booleanresult=fs.delete(remotePath,false); fs.close(); returnresult; /* *主函数 */ publicstaticvoidmain(Stringargs) Configurationconf=newConfiguration(); conf.set(,); StringremoteFilePath=/user/hadoop/text.txt

44、; try if(HDFSApi.rm(conf,remoteFilePath) 文件删除:+remoteFilePath); else 操作失败(文件不存在或删除失败); catch(Exceptione) e.printStackTrace(); (9) 删除HDFS中指定的目录,由用户指定目录中如果存在文件时是否删除目录; Shell命令: 删除目录(如果目录非空那么会提示notempty,不执行删除):./hdfsdfs-rmdirdir1/dir2 强制删除目录:./hdfsdfs-rm-Rdir1/dir2 Java代码: import; import; importjava.io

45、.*;/HDFS文件 publicclassHDFSApi /* *判断目录是否为空 *true:空,false:非空 */ publicstaticbooleanisDirEmpty(Configurationconf,StringremoteDir)throwsIOException FileSystemfs=FileSystem.get(conf); PathdirPath=newPath(remoteDir); RemoteIteratorremoteIterator=fs.listFiles(dirPath,true); return!remoteIterator.hasNext()

46、; /* 删除目录 */ publicstaticbooleanrmDir(Configurationconf,StringremoteDir,booleanrecursive)throwsIOException FileSystemfs=FileSystem.get(conf); PathdirPath=newPath(remoteDir); /*第二个参数表示是否递归删除所有文件 booleanresult=fs.delete(dirPath,recursive); fs.close(); returnresult; /* *主函数 */ publicstaticvoidmain(Stri

47、ngargs) Configurationconf=newConfiguration(); conf.set(,); StringremoteDir=/user/hadoop/input;/HDFS目录 BooleanforceDelete=false;/是否强制删除 try if(!HDFSApi.isDirEmpty(conf,remoteDir)&forceDelete) 目录不为空,不删除); else if(HDFSApi.rmDir(conf,remoteDir,forceDelete) 目录已删除:+remoteDir); else 操作失败); catch(Exceptione

48、) e.printStackTrace(); */ (10)在HDFS中,将文件从源路径移动到目的路径. Shell命令: ./hdfsdfs-mvtext.txttext2.txt Java代码: import; import; importjava.io.*; publicclassHDFSApi /* *移动文件 */ publicstaticbooleanmv(Configurationconf,StringremoteFilePath,StringremoteToFilePath)throwsIOException FileSystemfs=FileSystem.get(conf);

49、 PathsrcPath=newPath(remoteFilePath); PathdstPath=newPath(remoteToFilePath); booleanresult=fs.rename(srcPath,dstPath); fs.close(); returnresult; /* *主函数 */ publicstaticvoidmain(Stringargs) Configurationconf=newConfiguration(); conf.set(,); StringremoteFilePath=;/源文件HDFS路径 StringremoteToFilePath=;/目的HDFS路径 try if(HDFSApi.mv(conf,remoteFilePath,remoteToFilePath) 将文件+remoteFilePath+移动到+remoteToFilePath); else 操作失败(源文件不存在或移动失败广); catch(Exceptione) e.printStackTrace(); 2.编程实现一个类“MyFSDataInputStream,该类继承“,要求如下:实现按行读取HD FS中指定文件的方法“readLine(),如果读到文件末尾

温馨提示

  • 1. 本站所有资源如无特殊说明,都需要本地电脑安装OFFICE2007和PDF阅读器。图纸软件为CAD,CAXA,PROE,UG,SolidWorks等.压缩文件请下载最新的WinRAR软件解压。
  • 2. 本站的文档不包含任何第三方提供的附件图纸等,如果需要附件,请联系上传者。文件的所有权益归上传用户所有。
  • 3. 本站RAR压缩包中若带图纸,网页内容里面会有图纸预览,若没有图纸预览就没有图纸。
  • 4. 未经权益所有人同意不得将文件中的内容挪作商业或盈利用途。
  • 5. 人人文库网仅提供信息存储空间,仅对用户上传内容的表现方式做保护处理,对用户上传分享的文档内容本身不做任何修改或编辑,并不能对任何下载内容负责。
  • 6. 下载文件中如有侵权或不适当内容,请与我们联系,我们立即纠正。
  • 7. 本站不保证下载资源的准确性、安全性和完整性, 同时也不承担用户因使用这些下载资源对自己和他人造成任何形式的伤害或损失。

评论

0/150

提交评论