實(shí)驗(yàn)3熟悉常用的HDFS操作答案_第1頁(yè)
實(shí)驗(yàn)3熟悉常用的HDFS操作答案_第2頁(yè)
實(shí)驗(yàn)3熟悉常用的HDFS操作答案_第3頁(yè)
實(shí)驗(yàn)3熟悉常用的HDFS操作答案_第4頁(yè)
實(shí)驗(yàn)3熟悉常用的HDFS操作答案_第5頁(yè)
已閱讀5頁(yè),還剩14頁(yè)未讀, 繼續(xù)免費(fèi)閱讀

下載本文檔

版權(quán)說(shuō)明:本文檔由用戶提供并上傳,收益歸屬內(nèi)容提供方,若內(nèi)容存在侵權(quán),請(qǐng)進(jìn)行舉報(bào)或認(rèn)領(lǐng)

文檔簡(jiǎn)介

文檔來(lái)源為:從網(wǎng)絡(luò)收集整理.word版本可編輯.歡迎下載支持.文檔來(lái)源為:從網(wǎng)絡(luò)收集整理.word版本可編輯.歡迎下載支持.文檔收集于互聯(lián)網(wǎng),如有不妥請(qǐng)聯(lián)系刪除.PAGE文檔收集于互聯(lián)網(wǎng),如有不妥請(qǐng)聯(lián)系刪除.文檔來(lái)源為:從網(wǎng)絡(luò)收集整理.word版本可編輯.歡迎下載支持.文檔收集于互聯(lián)網(wǎng),如有不妥請(qǐng)聯(lián)系刪除.實(shí)驗(yàn)2熟悉常用的HDFS操作實(shí)驗(yàn)?zāi)康?. 理解HDFS在Hadoop體系結(jié)構(gòu)中的角色;2. 熟練使用HDFS操作常用的Shell命令;3. 熟悉HDFS操作常用的JavaAPI。實(shí)驗(yàn)平臺(tái)操作系統(tǒng):LinuxHadoop版本:JDK版本:1.6或以上版本JavaIDE:Eclipse實(shí)驗(yàn)內(nèi)容和要求編程實(shí)現(xiàn)以下指定功能,并利用Hadoop提供的Shell命令完成相同任務(wù):提示:部分Shell命令的參數(shù)路徑只能是本地路徑或者HDFS路徑。若Shell命令的參數(shù)既可以是本地路徑,也可以是HDFS路徑時(shí),務(wù)必注意區(qū)分。為保證操作正確,可指定路徑前綴或者注意區(qū)分相對(duì)路徑與絕對(duì)路徑具體命令的說(shuō)明可參考教材或/stable/hadoop-project-dist/hadoop-common/FileSystemShell.html向HDFS中上傳任意文本文件,如果指定的文件在HDFS中已經(jīng)存在,由用戶指定是追加到原有文件末尾還是覆蓋原有的文件;Shell命令:檢查文件是否存在:./hdfsdfs-test-etext.txt(執(zhí)行完這一句不會(huì)輸出結(jié)果,需要繼續(xù)輸入命令"echo$?")追加命令:./hdfsdfs-appendToFilelocal.txttext.txt覆蓋命令1:./hdfsdfs-copyFromLocal-flocal.txttext.txt覆蓋命令2:./hdfsdfs-cp-ftext.txt也可以使用如下命令實(shí)現(xiàn):(如下代碼可視為一行代碼,在終端中輸入第一行代碼后,直到輸入fi才會(huì)真正執(zhí)行):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));}/***復(fù)制文件到指定路徑*若路徑已存在,則進(jìn)行覆蓋*/publicstaticvoidcopyFromLocalFile(Configurationconf,StringlocalFilePath,StringremoteFilePath)throwsIOException{FileSystemfs=FileSystem.get(conf);PathlocalPath=newPath(localFilePath);PathremotePath=newPath(remoteFilePath);/*fs.copyFromLocalFile第一個(gè)參數(shù)表示是否刪除源文件,第二個(gè)參數(shù)表示是否覆蓋*/fs.copyFromLocalFile(false,true,localPath,remotePath);fs.close();}/***追加文件內(nèi)容*/publicstaticvoidappendToFile(Configurationconf,StringlocalFilePath,StringremoteFilePath)throwsIOException{FileSystemfs=FileSystem.get(conf);PathremotePath=newPath(remoteFilePath);/*創(chuàng)建一個(gè)文件讀入流*/FileInputStreamin=newFileInputStream(localFilePath);/*創(chuàng)建一個(gè)文件輸出流,輸出的內(nèi)容將追加到文件末尾*/FSDataOutputStreamout=fs.append(remotePath);/*讀寫文件內(nèi)容*/byte[]data=newbyte[1024];intread=-1;while((read=in.read(data))>0){ out.write(data,0,read);}out.close();in.close();fs.close();} /** *主函數(shù) */ 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{ +"不存在."); } /*進(jìn)行處理*/ 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中下載指定文件,如果本地文件與要下載的文件名稱相同,則自動(dòng)對(duì)下載的文件重命名;Shell命令:if$(./hdfsdfs-test-e;then$(./hdfsdfs-copyToLocaltext.txt./text2.txt);else$(./hdfsdfs-copyToLocaltext.txt./text.txt);fiJava代碼:import;import;importjava.io.*;publicclassHDFSApi{/***下載文件到本地*判斷本地路徑是否已存在,若已存在,則自動(dòng)進(jìn)行重命名*/publicstaticvoidcopyToLocal(Configurationconf,StringremoteFilePath,StringlocalFilePath)throwsIOException{FileSystemfs=FileSystem.get(conf);PathremotePath=newPath(remoteFilePath);Filef=newFile(localFilePath);/*如果文件名存在,自動(dòng)重命名(在文件名后面加上_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();} /** *主函數(shù) */ 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中指定文件的內(nèi)容輸出到終端中;Shell命令:./hdfsdfs-cattext.txtJava代碼:import;import;importjava.io.*;publicclassHDFSApi{/***讀取文件內(nèi)容*/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();} /** *主函數(shù) */ 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中指定的文件的讀寫權(quán)限、大小、創(chuàng)建時(shí)間、路徑等信息;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()); "權(quán)限:"+s.getPermission().toString()); "大小:"+s.getLen()); /*返回的是時(shí)間戳,轉(zhuǎn)化為時(shí)間日期格式*/ LongtimeStamp=s.getModificationTime(); SimpleDateFormatformat=newSimpleDateFormat("yyyy-MM-ddHH:mm:ss"); Stringdate=format.format(timeStamp); "時(shí)間:"+date);}fs.close();} /** *主函數(shù) */ 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中某一個(gè)目錄,輸出該目錄下的所有文件的讀寫權(quán)限、大小、創(chuàng)建時(shí)間、路徑等信息,如果該文件是目錄,則遞歸輸出該目錄下所有文件相關(guān)信息;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);/*輸出每個(gè)文件的信息*/while(remoteIterator.hasNext()){ FileStatuss=remoteIterator.next();"路徑:"+s.getPath().toString());"權(quán)限:"+s.getPermission().toString());"大小:"+s.getLen()); /*返回的是時(shí)間戳,轉(zhuǎn)化為時(shí)間日期格式*/ LongtimeStamp=s.getModificationTime(); SimpleDateFormatformat=newSimpleDateFormat("yyyy-MM-ddHH:mm:ss"); Stringdate=format.format(timeStamp); "時(shí)間:"+date); ;}fs.close();} /** *主函數(shù) */ publicstaticvoidmain(String[]args){ Configurationconf=newConfiguration();conf.set("",""); StringremoteDir="/user/hadoop";//HDFS路徑 try{ "(遞歸)讀取目錄下所有文件的信息:"+remoteDir); HDFSApi.lsDir(conf,remoteDir); "讀取完成"); }catch(Exceptione){ e.printStackTrace(); } }}提供一個(gè)HDFS內(nèi)的文件的路徑,對(duì)該文件進(jìn)行創(chuàng)建和刪除操作。如果文件所在目錄不存在,則自動(dòng)創(chuàng)建目錄;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));}/***創(chuàng)建目錄*/publicstaticbooleanmkdir(Configurationconf,StringremoteDir)throwsIOException{FileSystemfs=FileSystem.get(conf);PathdirPath=newPath(remoteDir);booleanresult=fs.mkdirs(dirPath);fs.close();returnresult;}/***創(chuàng)建文件*/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;} /** *主函數(shù) */ publicstaticvoidmain(String[]args){ Configurationconf=newConfiguration();conf.set("",""); StringremoteFilePath="/user/hadoop/input/text.txt";//HDFS路徑 StringremoteDir="/user/hadoop/input";//HDFS路徑對(duì)應(yīng)的目錄 try{ /*判斷路徑是否存在,存在則刪除,否則進(jìn)行創(chuàng)建*/ if(HDFSApi.test(conf,remoteFilePath)){ HDFSApi.rm(conf,remoteFilePath);//刪除 "刪除路徑:"+remoteFilePath); }else{ if(!HDFSApi.test(conf,remoteDir)){//若目錄不存在,則進(jìn)行創(chuàng)建 HDFSApi.mkdir(conf,remoteDir); "創(chuàng)建文件夾:"+remoteDir); } HDFSApi.touchz(conf,remoteFilePath); "創(chuàng)建路徑:"+remoteFilePath); } }catch(Exceptione){ e.printStackTrace(); } }}提供一個(gè)HDFS的目錄的路徑,對(duì)該目錄進(jìn)行創(chuàng)建和刪除操作。創(chuàng)建目錄時(shí),如果目錄文件所在目錄不存在則自動(dòng)創(chuàng)建相應(yīng)目錄;刪除目錄時(shí),由用戶指定當(dāng)該目錄不為空時(shí)是否還刪除該目錄;Shell命令:創(chuàng)建目錄:./hdfsdfs-mkdir-pdir1/dir2刪除目錄(如果目錄非空則會(huì)提示notempty,不執(zhí)行刪除):./hdfsdfs-rmdirdir1/dir2強(qiáng)制刪除目錄:./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();}/***創(chuàng)建目錄*/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);/*第二個(gè)參數(shù)表示是否遞歸刪除所有文件*/booleanresult=fs.delete(dirPath,true);fs.close();returnresult;} /** *主函數(shù) */ publicstaticvoidmain(String[]args){ Configurationconf=newConfiguration();conf.set("",""); StringremoteDir="/user/hadoop/input";//HDFS目錄 BooleanforceDelete=false;//是否強(qiáng)制刪除 try{ /*判斷目錄是否存在,不存在則創(chuàng)建,存在則刪除*/ if(!HDFSApi.test(conf,remoteDir)){ HDFSApi.mkdir(conf,remoteDir);//創(chuàng)建目錄 "創(chuàng)建目錄:"+remoteDir); }else{ if(HDFSApi.isDirEmpty(conf,remoteDir)||forceDelete){//目錄為空或強(qiáng)制刪除 HDFSApi.rmDir(conf,remoteDir); "刪除目錄:"+remoteDir); }else{//目錄不為空 "目錄不為空,不刪除:"+remoteDir); } } }catch(Exceptione){ e.printStackTrace(); } }}向HDFS中指定的文件追加內(nèi)容,由用戶指定內(nèi)容追加到原有文件的開頭或結(jié)尾;Shell命令:追加到文件末尾:./hdfsdfs-appendToFilelocal.txttext.txt追加到文件開頭:(由于沒有直接的命令可以操作,方法之一是先移動(dòng)到本地進(jìn)行操作,再進(jìn)行上傳覆蓋):./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));}/***追加文本內(nèi)容*/publicstaticvoidappendContentToFile(Configurationconf,Stringcontent,StringremoteFilePath)throwsIOException{FileSystemfs=FileSystem.get(conf);PathremotePath=newPath(remoteFilePath);/*創(chuàng)建一個(gè)文件輸出流,輸出的內(nèi)容將追加到文件末尾*/FSDataOutputStreamout=fs.append(remotePath);out.write(content.getBytes());out.close();fs.close();}/***追加文件內(nèi)容*/publicstaticvoidappendToFile(Configurationconf,StringlocalFilePath,StringremoteFilePath)throwsIOException{FileSystemfs=FileSystem.get(conf);PathremotePath=newPath(remoteFilePath);/*創(chuàng)建一個(gè)文件讀入流*/FileInputStreamin=newFileInputStream(localFilePath);/*創(chuàng)建一個(gè)文件輸出流,輸出的內(nèi)容將追加到文件末尾*/FSDataOutputStreamout=fs.append(remotePath);/*讀寫文件內(nèi)容*/byte[]data=newbyte[1024];intread=-1;while((read=in.read(data))>0){ out.write(data,0,read);}out.close();in.close();fs.close();}/***移動(dòng)文件到本地*移動(dòng)后,刪除源文件*/publicstaticvoidmoveToLocalFile(Configurationconf,StringremoteFilePath,StringlocalFilePath)throwsIOException{FileSystemfs=FileSystem.get(conf);PathremotePath=newPath(remoteFilePath);PathlocalPath=newPath(localFilePath);fs.moveToLocalFile(remotePath,localPath);}/***創(chuàng)建文件*/publicstaticvoidtouchz(Configurationconf,StringremoteFilePath)throwsIOException{FileSystemfs=FileSystem.get(conf);PathremotePath=newPath(remoteFilePath);FSDataOutputStreamoutputStream=fs.create(remotePath);outputStream.close();fs.close();} /** *主函數(shù) */ publicstaticvoidmain(String[]args){ Configurationconf=newConfiguration();conf.set("",""); StringremoteFilePath="/user/hadoop/text.txt";//HDFS文件 Stringcontent="新追加的內(nèi)容\n"; Stringchoice="after"; //追加到文件末尾// Stringchoice="before";//追加到文件開頭 try{ /*判斷文件是否存在*/ if(!HDFSApi.test(conf,remoteFilePath)){ "文件不存在:"+remoteFilePath); }else{ if(choice.equals("after")){//追加在文件末尾 HDFSApi.appendContentToFile(conf,content,remoteFilePath); "已追加內(nèi)容到文件末尾"+remoteFilePath); }elseif(choice.equals("before")){//追加到文件開頭 /*沒有相應(yīng)的api可以直接操作,因此先把文件移動(dòng)到本地,創(chuàng)建一個(gè)新的HDFS,再按順序追加內(nèi)容*/ StringlocalTmpPath="/user/hadoop/tmp.txt"; HDFSApi.moveToLocalFile(conf,remoteFilePath,localTmpPath);//移動(dòng)到本地 HDFSApi.touchz(conf,remoteFilePath);//創(chuàng)建一個(gè)新文件 HDFSApi.appendContentToFile(conf,content,remoteFilePath);//先寫入新內(nèi)容 HDFSApi.appendToFile(conf,localTmpPath,remoteFilePath);//再寫入原來(lái)內(nèi)容 "已追加內(nèi)容到文件開頭:"+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;} /** *主函數(shù) */ publicstaticvoidmain(String[]args){ Configurationconf=newConfiguration();conf.set("",""); StringremoteFilePath="/user/hadoop/text.txt";//HDFS文件 try{ if(HDFSApi.rm(conf,remoteFilePath)){ "文件刪除:"+remoteFilePath); }else{ "操作失?。ㄎ募淮嬖诨騽h除失?。?); } }catch(Exceptione){ e.printStackTrace(); } }}刪除HDFS中指定的目錄,由用戶指定目錄中如果存在文件時(shí)是否刪除目錄;Shell命令:刪除目錄(如果目錄非空則會(huì)提示notempty,不執(zhí)行刪除):./hdfsdfs-rmdirdir1/dir2強(qiáng)制刪除目錄:./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);/*第二個(gè)參數(shù)表示是否遞歸刪除所有文件*/booleanresult=fs.delete(dirPath,recursive);fs.close();returnresult;} /** *主函數(shù) */ publicstaticvoidmain(String[]args){ Configurationconf=newConfiguration();conf.set("",""); StringremoteDir="/user/hadoop/input";//HDFS目錄 BooleanforceDelete=false;//是否強(qiáng)制刪除 try{ if(!HDFSApi.isDirEmpty(conf,remoteDir)&&!forceDelete){ "目錄不為空,不刪除"); }else{ if(HDFSApi.rmDir(conf,remoteDir,forceDelete)){ "目錄已刪除:"+remoteDir); }else{ "操作失敗"); } } }catch(Exceptione){ e.printStackTrace(); } }}在HDFS中,將文件從源路徑移動(dòng)到目的路徑。Shell命令:./hdfsdfs-mvtext.txttext2.txtJava代碼:import;import;importjava.io.*;publicclassHDFSApi{/***移動(dòng)文件*/publicstaticbooleanmv(Configurationconf,StringremoteFilePath,StringremoteToFilePath)throwsIOException{FileSystemfs=FileSystem.get(conf);PathsrcPath=newPath(remoteFilePath);PathdstPath=newPath(remoteToFilePath);booleanresult=fs.rename(srcPath,dstPath);fs.close();returnresult;} /** *主函數(shù) */ publicstaticvoidmain(String[]args){ Configurationconf=newConfiguration();conf.set("",""); StringremoteFilePath="";//源文件HDFS路徑 StringremoteToFilePath="";//目的HDFS路徑 try{ if(HDFSApi.mv(conf,remoteFilePath,remoteToFilePath)){ "將文件"+remoteFilePath+"移動(dòng)到"+remoteToFilePath); }else{ "操作失敗(源文件不存在或移動(dòng)失敗)"); } }catch(Exceptione){ e.printStackTrace(); } }}編程實(shí)現(xiàn)一個(gè)類“MyFSDataInputStream”,該類繼承“,要求如下:實(shí)現(xiàn)按行讀取HDFS中指定文件的方法“

溫馨提示

  • 1. 本站所有資源如無(wú)特殊說(shuō)明,都需要本地電腦安裝OFFICE2007和PDF閱讀器。圖紙軟件為CAD,CAXA,PROE,UG,SolidWorks等.壓縮文件請(qǐng)下載最新的WinRAR軟件解壓。
  • 2. 本站的文檔不包含任何第三方提供的附件圖紙等,如果需要附件,請(qǐng)聯(lián)系上傳者。文件的所有權(quán)益歸上傳用戶所有。
  • 3. 本站RAR壓縮包中若帶圖紙,網(wǎng)頁(yè)內(nèi)容里面會(huì)有圖紙預(yù)覽,若沒有圖紙預(yù)覽就沒有圖紙。
  • 4. 未經(jīng)權(quán)益所有人同意不得將文件中的內(nèi)容挪作商業(yè)或盈利用途。
  • 5. 人人文庫(kù)網(wǎng)僅提供信息存儲(chǔ)空間,僅對(duì)用戶上傳內(nèi)容的表現(xiàn)方式做保護(hù)處理,對(duì)用戶上傳分享的文檔內(nèi)容本身不做任何修改或編輯,并不能對(duì)任何下載內(nèi)容負(fù)責(zé)。
  • 6. 下載文件中如有侵權(quán)或不適當(dāng)內(nèi)容,請(qǐng)與我們聯(lián)系,我們立即糾正。
  • 7. 本站不保證下載資源的準(zhǔn)確性、安全性和完整性, 同時(shí)也不承擔(dān)用戶因使用這些下載資源對(duì)自己和他人造成任何形式的傷害或損失。

最新文檔

評(píng)論

0/150

提交評(píng)論