版权说明:本文档由用户提供并上传,收益归属内容提供方,若内容存在侵权,请进行举报或认领
文档简介
文档来源为:从网络收集整理.word版本可编辑.欢迎下载支持.文档来源为:从网络收集整理.word版本可编辑.欢迎下载支持.文档收集于互联网,如有不妥请联系删除.PAGE文档收集于互联网,如有不妥请联系删除.文档来源为:从网络收集整理.word版本可编辑.欢迎下载支持.文档收集于互联网,如有不妥请联系删除.实验2熟悉常用的HDFS操作实验目的1. 理解HDFS在Hadoop体系结构中的角色;2. 熟练使用HDFS操作常用的Shell命令;3. 熟悉HDFS操作常用的JavaAPI。实验平台操作系统:LinuxHadoop版本:JDK版本:1.6或以上版本JavaIDE:Eclipse实验内容和要求编程实现以下指定功能,并利用Hadoop提供的Shell命令完成相同任务:提示:部分Shell命令的参数路径只能是本地路径或者HDFS路径。若Shell命令的参数既可以是本地路径,也可以是HDFS路径时,务必注意区分。为保证操作正确,可指定路径前缀或者注意区分相对路径与绝对路径具体命令的说明可参考教材或/stable/hadoop-project-dist/hadoop-common/FileSystemShell.html向HDFS中上传任意文本文件,如果指定的文件在HDFS中已经存在,由用户指定是追加到原有文件末尾还是覆盖原有的文件;Shell命令:检查文件是否存在:./hdfsdfs-test-etext.txt(执行完这一句不会输出结果,需要继续输入命令"echo$?")追加命令:./hdfsdfs-appendToFilelocal.txttext.txt覆盖命令1:./hdfsdfs-copyFromLocal-flocal.txttext.txt覆盖命令2:./hdfsdfs-cp-ftext.txt也可以使用如下命令实现:(如下代码可视为一行代码,在终端中输入第一行代码后,直到输入fi才会真正执行):if$(./hdfsdfs-test-etext.txt);then$(./hdfsdfs-appendToFilelocal.txttext.txt);else$(./hdfsdfs-copyFromLocal-flocal.txttext.txt);fiJava代码:import;import;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("",""); 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; +"已存在."); }else{ +"不存在."); } /*进行处理*/ if(!fileExists){//文件不存在,则上传 HDFSApi.copyFromLocalFile(conf,localFilePath,remoteFilePath); +"已上传至"+remoteFilePath); }elseif(choice.equals("overwrite")){//选择覆盖 HDFSApi.copyFromLocalFile(conf,localFilePath,remoteFilePath); +"已覆盖"+remoteFilePath); }elseif(choice.equals("append")){//选择追加 HDFSApi.appendToFile(conf,localFilePath,remoteFilePath); +"已追加至"+remoteFilePath); } }catch(Exceptione){ e.printStackTrace(); } }}从HDFS中下载指定文件,如果本地文件与要下载的文件名称相同,则自动对下载的文件重命名;Shell命令:if$(./hdfsdfs-test-e;then$(./hdfsdfs-copyToLocaltext.txt./text2.txt);else$(./hdfsdfs-copyToLocaltext.txt./text.txt);fiJava代码:import;import;importjava.io.*;publicclassHDFSApi{/***下载文件到本地*判断本地路径是否已存在,若已存在,则自动进行重命名*/publicstaticvoidcopyToLocal(Configurationconf,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.exists()){ localFilePath=localFilePath+"_"+i.toString(); break; } } "将重新命名为:"+localFilePath);}//下载文件到本地PathlocalPath=newPath(localFilePath);fs.copyToLocalFile(remotePath,localPath);fs.close();} /** *主函数 */ publicstaticvoidmain(String[]args){ Configurationconf=newConfiguration();conf.set("",""); StringlocalFilePath="/home/hadoop/text.txt";//本地路径 StringremoteFilePath="/user/hadoop/text.txt";//HDFS路径 try{ HDFSApi.copyToLocal(conf,remoteFilePath,localFilePath); "下载完成"); }catch(Exceptione){ e.printStackTrace(); } }}将HDFS中指定文件的内容输出到终端中;Shell命令:./hdfsdfs-cattext.txtJava代码:import;import;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){ ;}d.close();in.close();fs.close();} /** *主函数 */ publicstaticvoidmain(String[]args){ Configurationconf=newConfiguration();conf.set("",""); StringremoteFilePath="/user/hadoop/text.txt";//HDFS路径 try{ "读取文件:"+remoteFilePath); HDFSApi.cat(conf,remoteFilePath); "\n读取完成"); }catch(Exceptione){ e.printStackTrace(); } }}显示HDFS中指定的文件的读写权限、大小、创建时间、路径等信息;Shell命令:./hdfsdfs-ls-htext.txtJava代码:import;import;importjava.io.*;import;publicclassHDFSApi{/***显示指定文件的信息*/publicstaticvoidls(Configurationconf,StringremoteFilePath)throwsIOException{FileSystemfs=FileSystem.get(conf);PathremotePath=newPath(remoteFilePath);FileStatus[]fileStatuses=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=format.format(timeStamp); "时间:"+date);}fs.close();} /** *主函数 */ publicstaticvoidmain(String[]args){ Configurationconf=newConfiguration();conf.set("",""); StringremoteFilePath="/user/hadoop/text.txt";//HDFS路径 try{ "读取文件信息:"+remoteFilePath); HDFSApi.ls(conf,remoteFilePath); "\n读取完成"); }catch(Exceptione){ e.printStackTrace(); } }}给定HDFS中某一个目录,输出该目录下的所有文件的读写权限、大小、创建时间、路径等信息,如果该文件是目录,则递归输出该目录下所有文件相关信息;Shell命令:./hdfsdfs-ls-R-h/user/hadoopJava代码:import;import;importjava.io.*;import;publicclassHDFSApi{/***显示指定文件夹下所有文件的信息(递归)*/publicstaticvoidlsDir(Configurationconf,StringremoteDir)throwsIOException{FileSystemfs=FileSystem.get(conf);PathdirPath=newPath(remoteDir);/*递归获取目录下的所有文件*/RemoteIterator<LocatedFileStatus>remoteIterator=fs.listFiles(dirPath,true);/*输出每个文件的信息*/while(remoteIterator.hasNext()){ FileStatuss=remoteIterator.next();"路径:"+s.getPath().toString());"权限:"+s.getPermission().toString());"大小:"+s.getLen()); /*返回的是时间戳,转化为时间日期格式*/ LongtimeStamp=s.getModificationTime(); SimpleDateFormatformat=newSimpleDateFormat("yyyy-MM-ddHH:mm:ss"); Stringdate=format.format(timeStamp); "时间:"+date); ;}fs.close();} /** *主函数 */ publicstaticvoidmain(String[]args){ Configurationconf=newConfiguration();conf.set("",""); StringremoteDir="/user/hadoop";//HDFS路径 try{ "(递归)读取目录下所有文件的信息:"+remoteDir); HDFSApi.lsDir(conf,remoteDir); "读取完成"); }catch(Exceptione){ e.printStackTrace(); } }}提供一个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/filenameJava代码:import;import;importjava.io.*;publicclassHDFSApi{/***判断路径是否存在*/publicstaticbooleantest(Configurationconf,Stringpath)throwsIOException{FileSystemfs=FileSystem.get(conf);returnfs.exists(newPath(path));}/***创建目录*/publicstaticbooleanmkdir(Configurationconf,StringremoteDir)throwsIOException{FileSystemfs=FileSystem.get(conf);PathdirPath=newPath(remoteDir);booleanresult=fs.mkdirs(dirPath);fs.close();returnresult;}/***创建文件*/publicstaticvoidtouchz(Configurationconf,StringremoteFilePath)throwsIOException{FileSystemfs=FileSystem.get(conf);PathremotePath=newPath(remoteFilePath);FSDataOutputStreamoutputStream=fs.create(remotePath);outputStream.close();fs.close();}/***删除文件*/publicstaticbooleanrm(Configurationconf,StringremoteFilePath)throwsIOException{FileSystemfs=FileSystem.get(conf);PathremotePath=newPath(remoteFilePath);booleanresult=fs.delete(remotePath,false);fs.close();returnresult;} /** *主函数 */ publicstaticvoidmain(String[]args){ Configurationconf=newConfiguration();conf.set("",""); StringremoteFilePath="/user/hadoop/input/text.txt";//HDFS路径 StringremoteDir="/user/hadoop/input";//HDFS路径对应的目录 try{ /*判断路径是否存在,存在则删除,否则进行创建*/ 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.printStackTrace(); } }}提供一个HDFS的目录的路径,对该目录进行创建和删除操作。创建目录时,如果目录文件所在目录不存在则自动创建相应目录;删除目录时,由用户指定当该目录不为空时是否还删除该目录;Shell命令:创建目录:./hdfsdfs-mkdir-pdir1/dir2删除目录(如果目录非空则会提示notempty,不执行删除):./hdfsdfs-rmdirdir1/dir2强制删除目录:./hdfsdfs-rm-Rdir1/dir2Java代码:import;import;importjava.io.*;publicclassHDFSApi{/***判断路径是否存在*/publicstaticbooleantest(Configurationconf,Stringpath)throwsIOException{FileSystemfs=FileSystem.get(conf);returnfs.exists(newPath(path));}/***判断目录是否为空*true:空,false:非空*/publicstaticbooleanisDirEmpty(Configurationconf,StringremoteDir)throwsIOException{FileSystemfs=FileSystem.get(conf);PathdirPath=newPath(remoteDir);RemoteIterator<LocatedFileStatus>remoteIterator=fs.listFiles(dirPath,true);return!remoteIterator.hasNext();}/***创建目录*/publicstaticbooleanmkdir(Configurationconf,StringremoteDir)throwsIOException{FileSystemfs=FileSystem.get(conf);PathdirPath=newPath(remoteDir);booleanresult=fs.mkdirs(dirPath);fs.close();returnresult;}/***删除目录*/publicstaticbooleanrmDir(Configurationconf,StringremoteDir)throwsIOException{FileSystemfs=FileSystem.get(conf);PathdirPath=newPath(remoteDir);/*第二个参数表示是否递归删除所有文件*/booleanresult=fs.delete(dirPath,true);fs.close();returnresult;} /** *主函数 */ publicstaticvoidmain(String[]args){ 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.isDirEmpty(conf,remoteDir)||forceDelete){//目录为空或强制删除 HDFSApi.rmDir(conf,remoteDir); "删除目录:"+remoteDir); }else{//目录不为空 "目录不为空,不删除:"+remoteDir); } } }catch(Exceptione){ e.printStackTrace(); } }}向HDFS中指定的文件追加内容,由用户指定内容追加到原有文件的开头或结尾;Shell命令:追加到文件末尾:./hdfsdfs-appendToFilelocal.txttext.txt追加到文件开头:(由于没有直接的命令可以操作,方法之一是先移动到本地进行操作,再进行上传覆盖):./hdfsdfs-gettext.txtcattext.txt>>local.txt./hdfsdfs-copyFromLocal-ftext.txttext.txtJava代码:import;import;importjava.io.*;publicclassHDFSApi{/***判断路径是否存在*/publicstaticbooleantest(Configurationconf,Stringpath)throwsIOException{FileSystemfs=FileSystem.get(conf);returnfs.exists(newPath(path));}/***追加文本内容*/publicstaticvoidappendContentToFile(Configurationconf,Stringcontent,StringremoteFilePath)throwsIOException{FileSystemfs=FileSystem.get(conf);PathremotePath=newPath(remoteFilePath);/*创建一个文件输出流,输出的内容将追加到文件末尾*/FSDataOutputStreamout=fs.append(remotePath);out.write(content.getBytes());out.close();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();}/***移动文件到本地*移动后,删除源文件*/publicstaticvoidmoveToLocalFile(Configurationconf,StringremoteFilePath,StringlocalFilePath)throwsIOException{FileSystemfs=FileSystem.get(conf);PathremotePath=newPath(remoteFilePath);PathlocalPath=newPath(localFilePath);fs.moveToLocalFile(remotePath,localPath);}/***创建文件*/publicstaticvoidtouchz(Configurationconf,StringremoteFilePath)throwsIOException{FileSystemfs=FileSystem.get(conf);PathremotePath=newPath(remoteFilePath);FSDataOutputStreamoutputStream=fs.create(remotePath);outputStream.close();fs.close();} /** *主函数 */ publicstaticvoidmain(String[]args){ Configurationconf=newConfiguration();conf.set("",""); StringremoteFilePath="/user/hadoop/text.txt";//HDFS文件 Stringcontent="新追加的内容\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.equals("before")){//追加到文件开头 /*没有相应的api可以直接操作,因此先把文件移动到本地,创建一个新的HDFS,再按顺序追加内容*/ StringlocalTmpPath="/user/hadoop/tmp.txt"; HDFSApi.moveToLocalFile(conf,remoteFilePath,localTmpPath);//移动到本地 HDFSApi.touchz(conf,remoteFilePath);//创建一个新文件 HDFSApi.appendContentToFile(conf,content,remoteFilePath);//先写入新内容 HDFSApi.appendToFile(conf,localTmpPath,remoteFilePath);//再写入原来内容 "已追加内容到文件开头:"+remoteFilePath); } } }catch(Exceptione){ e.printStackTrace(); } }}删除HDFS中指定的文件;Shell命令:./hdfsdfs-rmtext.txtJava命令:import;import;importjava.io.*;publicclassHDFSApi{/***删除文件*/publicstaticbooleanrm(Configurationconf,StringremoteFilePath)throwsIOException{FileSystemfs=FileSystem.get(conf);PathremotePath=newPath(remoteFilePath);booleanresult=fs.delete(remotePath,false);fs.close();returnresult;} /** *主函数 */ publicstaticvoidmain(String[]args){ Configurationconf=newConfiguration();conf.set("",""); StringremoteFilePath="/user/hadoop/text.txt";//HDFS文件 try{ if(HDFSApi.rm(conf,remoteFilePath)){ "文件删除:"+remoteFilePath); }else{ "操作失败(文件不存在或删除失败)"); } }catch(Exceptione){ e.printStackTrace(); } }}删除HDFS中指定的目录,由用户指定目录中如果存在文件时是否删除目录;Shell命令:删除目录(如果目录非空则会提示notempty,不执行删除):./hdfsdfs-rmdirdir1/dir2强制删除目录:./hdfsdfs-rm-Rdir1/dir2Java代码:import;import;importjava.io.*;publicclassHDFSApi{/***判断目录是否为空*true:空,false:非空*/publicstaticbooleanisDirEmpty(Configurationconf,StringremoteDir)throwsIOException{FileSystemfs=FileSystem.get(conf);PathdirPath=newPath(remoteDir);RemoteIterator<LocatedFileStatus>remoteIterator=fs.listFiles(dirPath,true);return!remoteIterator.hasNext();}/***删除目录*/publicstaticbooleanrmDir(Configurationconf,StringremoteDir,booleanrecursive)throwsIOException{FileSystemfs=FileSystem.get(conf);PathdirPath=newPath(remoteDir);/*第二个参数表示是否递归删除所有文件*/booleanresult=fs.delete(dirPath,recursive);fs.close();returnresult;} /** *主函数 */ publicstaticvoidmain(String[]args){ 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){ e.printStackTrace(); } }}在HDFS中,将文件从源路径移动到目的路径。Shell命令:./hdfsdfs-mvtext.txttext2.txtJava代码:import;import;importjava.io.*;publicclassHDFSApi{/***移动文件*/publicstaticbooleanmv(Configurationconf,StringremoteFilePath,StringremoteToFilePath)throwsIOException{FileSystemfs=FileSystem.get(conf);PathsrcPath=newPath(remoteFilePath);PathdstPath=newPath(remoteToFilePath);booleanresult=fs.rename(srcPath,dstPath);fs.close();returnresult;} /** *主函数 */ publicstaticvoidmain(String[]args){ Configurationconf=newConfiguration();conf.set("",""); StringremoteFilePath="";//源文件HDFS路径 StringremoteToFilePath="";//目的HDFS路径 try{ if(HDFSApi.mv(conf,remoteFilePath,remoteToFilePath)){ "将文件"+remoteFilePath+"移动到"+remoteToFilePath); }else{ "操作失败(源文件不存在或移动失败)"); } }catch(Exceptione){ e.printStackTrace(); } }}编程实现一个类“MyFSDataInputStream”,该类继承“,要求如下:实现按行读取HDFS中指定文件的方法“
温馨提示
- 1. 本站所有资源如无特殊说明,都需要本地电脑安装OFFICE2007和PDF阅读器。图纸软件为CAD,CAXA,PROE,UG,SolidWorks等.压缩文件请下载最新的WinRAR软件解压。
- 2. 本站的文档不包含任何第三方提供的附件图纸等,如果需要附件,请联系上传者。文件的所有权益归上传用户所有。
- 3. 本站RAR压缩包中若带图纸,网页内容里面会有图纸预览,若没有图纸预览就没有图纸。
- 4. 未经权益所有人同意不得将文件中的内容挪作商业或盈利用途。
- 5. 人人文库网仅提供信息存储空间,仅对用户上传内容的表现方式做保护处理,对用户上传分享的文档内容本身不做任何修改或编辑,并不能对任何下载内容负责。
- 6. 下载文件中如有侵权或不适当内容,请与我们联系,我们立即纠正。
- 7. 本站不保证下载资源的准确性、安全性和完整性, 同时也不承担用户因使用这些下载资源对自己和他人造成任何形式的伤害或损失。
最新文档
- 2025 小学六年级语文下册 综合性学习 活动设计课件
- 【项目方案】233KWh 定制户外一体柜储能系统项目技术方案
- 注册会计师就业前景分析
- 跨境电商2025年海运整箱保险协议
- 2025 小学六年级语文上册综合性学习轻叩诗歌大门课件
- 科技研发终止协议2025年成果转化条款
- 2025 小学六年级语文上册借代修辞手法课件
- 浙江省丽水市2025年九年级上学期期末考试数学试卷附答案
- 股权架构方案(后附模板)
- 赣州医院面试题及答案
- 非开挖顶管合同范本
- 急性膀胱炎课件
- 专家讲座的协议书
- (新教材)2025年人教版三年级上册数学 数学广角:搭配问题 课件
- 【语文】小学一年级上册期末质量试卷
- 2026元旦班级联欢晚会活动主题班会:星光闪耀迎新夜 课件
- 人教版(2024)七年级上册生物期末复习全册知识点梳理讲义
- 2025年内蒙古行政执法人员资格认证考试题库真题库及答案
- 急性胰腺炎重症患者白蛋白输注方案
- 《产业经济学》课程论文选题、要求和评分标准
- 中国-东盟贸易投资合作进展报告2024-2025-深圳大学
评论
0/150
提交评论