




版權(quán)說明:本文檔由用戶提供并上傳,收益歸屬內(nèi)容提供方,若內(nèi)容存在侵權(quán),請(qǐng)進(jìn)行舉報(bào)或認(rèn)領(lǐng)
文檔簡(jiǎn)介
1、實(shí)驗(yàn)2熟悉常用的hdfs操作1 實(shí)驗(yàn)?zāi)康?.理解hdfs在hadoop體系結(jié)構(gòu)中的角色;2.熟練使用hdfs操作常用的shell命令;3.熟悉hdfs操作常用的java api。2 實(shí)驗(yàn)平臺(tái)操作系統(tǒng):linuxhadoop版本:2.6.0或以上版本jdk版本:1.6或以上版本java ide:eclipse3 實(shí)驗(yàn)內(nèi)容和要求1. 編程實(shí)現(xiàn)以下指定功能,并利用hadoop提供的shell命令完成相同任務(wù):提示:1) 部分shell命令的參數(shù)路徑只能是本地路徑或者h(yuǎn)dfs路徑。2) 若shell命令的參數(shù)既可以是本地路徑,也可以是hdfs路徑時(shí),務(wù)必注意區(qū)分。為保證操作正確,可指定路徑前綴 hdf
2、s:/ 或者 file:/3) 注意區(qū)分相對(duì)路徑與絕對(duì)路徑4) 具體命令的說明可參考教材或 /docs/stable/hadoop-project-dist/hadoop-common/filesystemshell.html(1) 向hdfs中上傳任意文本文件,如果指定的文件在hdfs中已經(jīng)存在,由用戶指定是追加到原有文件末尾還是覆蓋原有的文件;shell命令:檢查文件是否存在: ./hdfs dfs -test -e text.txt(執(zhí)行完這一句不會(huì)輸出結(jié)果,需要繼續(xù)輸入命令 echo $?)追加命令: ./hdfs dfs -appendt
3、ofile local.txt text.txt覆蓋命令1: ./hdfs dfs -copyfromlocal -f local.txt text.txt覆蓋命令2: ./hdfs dfs -cp -f file:/home/hadoop/local.txt text.txt也可以使用如下命令實(shí)現(xiàn):(如下代碼可視為一行代碼,在終端中輸入第一行代碼后,直到輸入 fi 才會(huì)真正執(zhí)行):if $(./hdfs dfs -test -e text.txt);then $(./hdfs dfs -appendtofile local.txt text.txt);else $(./hdfs dfs -c
4、opyfromlocal -f local.txt text.txt);fijava代碼:import org.apache.hadoop.conf.configuration;import org.apache.hadoop.fs.*;import java.io.*;public class hdfsapi /* * 判斷路徑是否存在 */ public static boolean test(configuration conf, string path) throws ioexception filesystem fs = filesystem.get(conf); return fs
5、.exists(new path(path); /* * 復(fù)制文件到指定路徑 * 若路徑已存在,則進(jìn)行覆蓋 */ public static void copyfromlocalfile(configuration conf, string localfilepath, string remotefilepath) throws ioexception filesystem fs = filesystem.get(conf); path localpath = new path(localfilepath); path remotepath = new path(remotefilepath)
6、; /* fs.copyfromlocalfile 第一個(gè)參數(shù)表示是否刪除源文件,第二個(gè)參數(shù)表示是否覆蓋 */ fs.copyfromlocalfile(false, true, localpath, remotepath); fs.close(); /* * 追加文件內(nèi)容 */ public static void appendtofile(configuration conf, string localfilepath, string remotefilepath) throws ioexception filesystem fs = filesystem.get(conf); path
7、remotepath = new path(remotefilepath); /* 創(chuàng)建一個(gè)文件讀入流 */ fileinputstream in = new fileinputstream(localfilepath); /* 創(chuàng)建一個(gè)文件輸出流,輸出的內(nèi)容將追加到文件末尾 */ fsdataoutputstream out = fs.append(remotepath); /* 讀寫文件內(nèi)容 */ byte data = new byte1024; int read = -1; while ( (read = in.read(data) 0 ) out.write(data, 0, rea
8、d); out.close(); in.close(); fs.close(); /* * 主函數(shù) */public static void main(string args) configuration conf = new configuration(); conf.set(,hdfs:/localhost:9000);string localfilepath = /home/hadoop/text.txt; / 本地路徑string remotefilepath = /user/hadoop/text.txt; / hdfs路徑string choice =
9、 append; / 若文件存在則追加到文件末尾/string choice = overwrite; / 若文件存在則覆蓋try /* 判斷文件是否存在 */boolean fileexists = false;if (hdfsapi.test(conf, remotefilepath) fileexists = true;system.out.println(remotefilepath + 已存在.); else system.out.println(remotefilepath + 不存在.);/* 進(jìn)行處理 */if ( !fileexists) / 文件不存在,則上傳hdfsapi
10、.copyfromlocalfile(conf, localfilepath, remotefilepath);system.out.println(localfilepath + 已上傳至 + remotefilepath); else if ( choice.equals(overwrite) ) / 選擇覆蓋hdfsapi.copyfromlocalfile(conf, localfilepath, remotefilepath);system.out.println(localfilepath + 已覆蓋 + remotefilepath); else if ( choice.equa
11、ls(append) ) / 選擇追加hdfsapi.appendtofile(conf, localfilepath, remotefilepath);system.out.println(localfilepath + 已追加至 + remotefilepath); catch (exception e) e.printstacktrace();(2) 從hdfs中下載指定文件,如果本地文件與要下載的文件名稱相同,則自動(dòng)對(duì)下載的文件重命名;shell命令:if $(./hdfs dfs -test -e file:/home/hadoop/text.txt);then $(./hdfs d
12、fs -copytolocal text.txt ./text2.txt); else $(./hdfs dfs -copytolocal text.txt ./text.txt); fijava代碼:import org.apache.hadoop.conf.configuration;import org.apache.hadoop.fs.*;import java.io.*;public class hdfsapi /* * 下載文件到本地 * 判斷本地路徑是否已存在,若已存在,則自動(dòng)進(jìn)行重命名 */ public static void copytolocal(configuratio
13、n conf, string remotefilepath, string localfilepath) throws ioexception filesystem fs = filesystem.get(conf); path remotepath = new path(remotefilepath); file f = new file(localfilepath); /* 如果文件名存在,自動(dòng)重命名(在文件名后面加上 _0, _1 .) */ if (f.exists() system.out.println(localfilepath + 已存在.); integer i = 0; w
14、hile (true) f = new file(localfilepath + _ + i.tostring(); if (!f.exists() localfilepath = localfilepath + _ + i.tostring(); break; system.out.println(將重新命名為: + localfilepath); / 下載文件到本地 path localpath = new path(localfilepath); fs.copytolocalfile(remotepath, localpath); fs.close(); /* * 主函數(shù) */publi
15、c static void main(string args) configuration conf = new configuration(); conf.set(,hdfs:/localhost:9000);string localfilepath = /home/hadoop/text.txt; / 本地路徑string remotefilepath = /user/hadoop/text.txt; / hdfs路徑try hdfsapi.copytolocal(conf, remotefilepath, localfilepath);system.out.
16、println(下載完成); catch (exception e) e.printstacktrace();(3) 將hdfs中指定文件的內(nèi)容輸出到終端中;shell命令:./hdfs dfs -cat text.txtjava代碼:import org.apache.hadoop.conf.configuration;import org.apache.hadoop.fs.*;import java.io.*;public class hdfsapi /* * 讀取文件內(nèi)容 */ public static void cat(configuration conf, string remot
17、efilepath) throws ioexception filesystem fs = filesystem.get(conf); path remotepath = new path(remotefilepath); fsdatainputstream in = fs.open(remotepath); bufferedreader d = new bufferedreader(new inputstreamreader(in); string line = null; while ( (line = d.readline() != null ) system.out.println(l
18、ine); d.close(); in.close(); fs.close(); /* * 主函數(shù) */public static void main(string args) configuration conf = new configuration(); conf.set(,hdfs:/localhost:9000);string remotefilepath = /user/hadoop/text.txt; / hdfs路徑try system.out.println(讀取文件: + remotefilepath);hdfsapi.cat(conf, re
19、motefilepath);system.out.println(n讀取完成); catch (exception e) e.printstacktrace();(4) 顯示hdfs中指定的文件的讀寫權(quán)限、大小、創(chuàng)建時(shí)間、路徑等信息;shell命令:./hdfs dfs -ls -h text.txtjava代碼:import org.apache.hadoop.conf.configuration;import org.apache.hadoop.fs.*;import java.io.*;import java.text.simpledateformat;public class hdfs
20、api /* * 顯示指定文件的信息 */ public static void ls(configuration conf, string remotefilepath) throws ioexception filesystem fs = filesystem.get(conf); path remotepath = new path(remotefilepath); filestatus filestatuses = fs.liststatus(remotepath); for (filestatus s : filestatuses) system.out.println(路徑: +
21、s.getpath().tostring(); system.out.println(權(quán)限: + s.getpermission().tostring(); system.out.println(大小: + s.getlen(); /* 返回的是時(shí)間戳,轉(zhuǎn)化為時(shí)間日期格式 */ long timestamp = s.getmodificationtime(); simpledateformat format = new simpledateformat(yyyy-mm-dd hh:mm:ss); string date = format.format(timestamp); system.ou
22、t.println(時(shí)間: + date); fs.close(); /* * 主函數(shù) */public static void main(string args) configuration conf = new configuration(); conf.set(,hdfs:/localhost:9000);string remotefilepath = /user/hadoop/text.txt; / hdfs路徑try system.out.println(讀取文件信息: + remotefilepath);hdfsapi.ls(conf, remotef
23、ilepath);system.out.println(n讀取完成); catch (exception e) e.printstacktrace();(5) 給定hdfs中某一個(gè)目錄,輸出該目錄下的所有文件的讀寫權(quán)限、大小、創(chuàng)建時(shí)間、路徑等信息,如果該文件是目錄,則遞歸輸出該目錄下所有文件相關(guān)信息;shell命令:./hdfs dfs -ls -r -h /user/hadoopjava代碼:import org.apache.hadoop.conf.configuration;import org.apache.hadoop.fs.*;import java.io.*;import jav
24、a.text.simpledateformat;public class hdfsapi /* * 顯示指定文件夾下所有文件的信息(遞歸) */ public static void lsdir(configuration conf, string remotedir) throws ioexception filesystem fs = filesystem.get(conf); path dirpath = new path(remotedir); /* 遞歸獲取目錄下的所有文件 */ remoteiterator remoteiterator = fs.listfiles(dirpath
25、, true); /* 輸出每個(gè)文件的信息 */ while (remoteiterator.hasnext() filestatus s = remoteiterator.next(); system.out.println(路徑: + s.getpath().tostring(); system.out.println(權(quán)限: + s.getpermission().tostring(); system.out.println(大小: + s.getlen(); /* 返回的是時(shí)間戳,轉(zhuǎn)化為時(shí)間日期格式 */ long timestamp = s.getmodificationtime()
26、; simpledateformat format = new simpledateformat(yyyy-mm-dd hh:mm:ss); string date = format.format(timestamp); system.out.println(時(shí)間: + date); system.out.println(); fs.close(); /* * 主函數(shù) */public static void main(string args) configuration conf = new configuration(); conf.set(,hdfs:/lo
27、calhost:9000);string remotedir = /user/hadoop; / hdfs路徑try system.out.println(遞歸)讀取目錄下所有文件的信息: + remotedir);hdfsapi.lsdir(conf, remotedir);system.out.println(讀取完成); catch (exception e) e.printstacktrace();(6) 提供一個(gè)hdfs內(nèi)的文件的路徑,對(duì)該文件進(jìn)行創(chuàng)建和刪除操作。如果文件所在目錄不存在,則自動(dòng)創(chuàng)建目錄;shell命令:if $(./hdfs dfs -test -d dir1/dir
28、2);then $(./hdfs dfs -touchz dir1/dir2/filename); else $(./hdfs dfs -mkdir -p dir1/dir2 & hdfs dfs -touchz dir1/dir2/filename); fi刪除文件:./hdfs dfs -rm dir1/dir2/filenamejava代碼:import org.apache.hadoop.conf.configuration;import org.apache.hadoop.fs.*;import java.io.*;public class hdfsapi /* * 判斷路徑是否存在
29、 */ public static boolean test(configuration conf, string path) throws ioexception filesystem fs = filesystem.get(conf); return fs.exists(new path(path); /* * 創(chuàng)建目錄 */ public static boolean mkdir(configuration conf, string remotedir) throws ioexception filesystem fs = filesystem.get(conf); path dirpa
30、th = new path(remotedir); boolean result = fs.mkdirs(dirpath); fs.close(); return result; /* * 創(chuàng)建文件 */ public static void touchz(configuration conf, string remotefilepath) throws ioexception filesystem fs = filesystem.get(conf); path remotepath = new path(remotefilepath); fsdataoutputstream outputst
31、ream = fs.create(remotepath); outputstream.close(); fs.close(); /* * 刪除文件 */ public static boolean rm(configuration conf, string remotefilepath) throws ioexception filesystem fs = filesystem.get(conf); path remotepath = new path(remotefilepath); boolean result = fs.delete(remotepath, false); fs.clos
32、e(); return result; /* * 主函數(shù) */public static void main(string args) configuration conf = new configuration(); conf.set(,hdfs:/localhost:9000);string remotefilepath = /user/hadoop/input/text.txt; / hdfs路徑string remotedir = /user/hadoop/input; / hdfs路徑對(duì)應(yīng)的目錄try /* 判斷路徑是否存在,存在則刪除,否則進(jìn)行創(chuàng)建 *
33、/if ( hdfsapi.test(conf, remotefilepath) ) hdfsapi.rm(conf, remotefilepath); / 刪除system.out.println(刪除路徑: + remotefilepath); else if ( !hdfsapi.test(conf, remotedir) ) / 若目錄不存在,則進(jìn)行創(chuàng)建hdfsapi.mkdir(conf, remotedir);system.out.println(創(chuàng)建文件夾: + remotedir);hdfsapi.touchz(conf, remotefilepath);system.out.
34、println(創(chuàng)建路徑: + remotefilepath); catch (exception e) e.printstacktrace();(7) 提供一個(gè)hdfs的目錄的路徑,對(duì)該目錄進(jìn)行創(chuàng)建和刪除操作。創(chuàng)建目錄時(shí),如果目錄文件所在目錄不存在則自動(dòng)創(chuàng)建相應(yīng)目錄;刪除目錄時(shí),由用戶指定當(dāng)該目錄不為空時(shí)是否還刪除該目錄;shell命令:創(chuàng)建目錄:./hdfs dfs -mkdir -p dir1/dir2刪除目錄(如果目錄非空則會(huì)提示not empty,不執(zhí)行刪除):./hdfs dfs -rmdir dir1/dir2強(qiáng)制刪除目錄:./hdfs dfs -rm -r dir1/dir2j
35、ava代碼:import org.apache.hadoop.conf.configuration;import org.apache.hadoop.fs.*;import java.io.*;public class hdfsapi /* * 判斷路徑是否存在 */ public static boolean test(configuration conf, string path) throws ioexception filesystem fs = filesystem.get(conf); return fs.exists(new path(path); /* * 判斷目錄是否為空 *
36、 true: 空,false: 非空 */ public static boolean isdirempty(configuration conf, string remotedir) throws ioexception filesystem fs = filesystem.get(conf); path dirpath = new path(remotedir); remoteiterator remoteiterator = fs.listfiles(dirpath, true); return !remoteiterator.hasnext(); /* * 創(chuàng)建目錄 */ public
37、 static boolean mkdir(configuration conf, string remotedir) throws ioexception filesystem fs = filesystem.get(conf); path dirpath = new path(remotedir); boolean result = fs.mkdirs(dirpath); fs.close(); return result; /* * 刪除目錄 */ public static boolean rmdir(configuration conf, string remotedir) thro
38、ws ioexception filesystem fs = filesystem.get(conf); path dirpath = new path(remotedir); /* 第二個(gè)參數(shù)表示是否遞歸刪除所有文件 */ boolean result = fs.delete(dirpath, true); fs.close(); return result; /* * 主函數(shù) */public static void main(string args) configuration conf = new configuration(); conf.set(,hd
39、fs:/localhost:9000);string remotedir = /user/hadoop/input; / hdfs目錄boolean forcedelete = false; / 是否強(qiáng)制刪除try /* 判斷目錄是否存在,不存在則創(chuàng)建,存在則刪除 */if ( !hdfsapi.test(conf, remotedir) ) hdfsapi.mkdir(conf, remotedir); / 創(chuàng)建目錄system.out.println(創(chuàng)建目錄: + remotedir); else if ( hdfsapi.isdirempty(conf, remotedir) | fo
40、rcedelete ) / 目錄為空或強(qiáng)制刪除hdfsapi.rmdir(conf, remotedir);system.out.println(刪除目錄: + remotedir); else / 目錄不為空system.out.println(目錄不為空,不刪除: + remotedir); catch (exception e) e.printstacktrace();(8) 向hdfs中指定的文件追加內(nèi)容,由用戶指定內(nèi)容追加到原有文件的開頭或結(jié)尾;shell命令:追加到文件末尾:./hdfs dfs -appendtofile local.txt text.txt追加到文件開頭:(由于
41、沒有直接的命令可以操作,方法之一是先移動(dòng)到本地進(jìn)行操作,再進(jìn)行上傳覆蓋):./hdfs dfs -get text.txtcat text.txt local.txt./hdfs dfs -copyfromlocal -f text.txt text.txtjava代碼:import org.apache.hadoop.conf.configuration;import org.apache.hadoop.fs.*;import java.io.*;public class hdfsapi /* * 判斷路徑是否存在 */ public static boolean test(configur
42、ation conf, string path) throws ioexception filesystem fs = filesystem.get(conf); return fs.exists(new path(path); /* * 追加文本內(nèi)容 */ public static void appendcontenttofile(configuration conf, string content, string remotefilepath) throws ioexception filesystem fs = filesystem.get(conf); path remotepath
43、 = new path(remotefilepath); /* 創(chuàng)建一個(gè)文件輸出流,輸出的內(nèi)容將追加到文件末尾 */ fsdataoutputstream out = fs.append(remotepath); out.write(content.getbytes(); out.close(); fs.close(); /* * 追加文件內(nèi)容 */ public static void appendtofile(configuration conf, string localfilepath, string remotefilepath) throws ioexception filesys
44、tem fs = filesystem.get(conf); path remotepath = new path(remotefilepath); /* 創(chuàng)建一個(gè)文件讀入流 */ fileinputstream in = new fileinputstream(localfilepath); /* 創(chuàng)建一個(gè)文件輸出流,輸出的內(nèi)容將追加到文件末尾 */ fsdataoutputstream out = fs.append(remotepath); /* 讀寫文件內(nèi)容 */ byte data = new byte1024; int read = -1; while ( (read = in.r
45、ead(data) 0 ) out.write(data, 0, read); out.close(); in.close(); fs.close(); /* * 移動(dòng)文件到本地 * 移動(dòng)后,刪除源文件 */ public static void movetolocalfile(configuration conf, string remotefilepath, string localfilepath) throws ioexception filesystem fs = filesystem.get(conf); path remotepath = new path(remotefilep
46、ath); path localpath = new path(localfilepath); fs.movetolocalfile(remotepath, localpath); /* * 創(chuàng)建文件 */ public static void touchz(configuration conf, string remotefilepath) throws ioexception filesystem fs = filesystem.get(conf); path remotepath = new path(remotefilepath); fsdataoutputstream outputs
47、tream = fs.create(remotepath); outputstream.close(); fs.close(); /* * 主函數(shù) */public static void main(string args) configuration conf = new configuration(); conf.set(,hdfs:/localhost:9000);string remotefilepath = /user/hadoop/text.txt; / hdfs文件string content = 新追加的內(nèi)容n;string choice = after; /追加到文件末尾/string choice = before; / 追加到文件開頭try /* 判斷文件是否存在 */if ( !hdfsapi.test(conf, remotefilepath) ) system.out.println(文件不存在: + remotefilepath); else if ( choice.equals(after) ) / 追加在文件末尾hdfsapi.appendcontentto
溫馨提示
- 1. 本站所有資源如無特殊說明,都需要本地電腦安裝OFFICE2007和PDF閱讀器。圖紙軟件為CAD,CAXA,PROE,UG,SolidWorks等.壓縮文件請(qǐng)下載最新的WinRAR軟件解壓。
- 2. 本站的文檔不包含任何第三方提供的附件圖紙等,如果需要附件,請(qǐng)聯(lián)系上傳者。文件的所有權(quán)益歸上傳用戶所有。
- 3. 本站RAR壓縮包中若帶圖紙,網(wǎng)頁內(nèi)容里面會(huì)有圖紙預(yù)覽,若沒有圖紙預(yù)覽就沒有圖紙。
- 4. 未經(jīng)權(quán)益所有人同意不得將文件中的內(nèi)容挪作商業(yè)或盈利用途。
- 5. 人人文庫網(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ì)自己和他人造成任何形式的傷害或損失。
最新文檔
- 廣東舞蹈戲劇職業(yè)學(xué)院《臨床藥理學(xué)B》2023-2024學(xué)年第二學(xué)期期末試卷
- 內(nèi)蒙古能源職業(yè)學(xué)院《軟件工程專業(yè)實(shí)訓(xùn)》2023-2024學(xué)年第二學(xué)期期末試卷
- 安徽信息工程學(xué)院《氣象與生活》2023-2024學(xué)年第一學(xué)期期末試卷
- 湖北中醫(yī)藥高等??茖W(xué)?!缎旅襟w產(chǎn)品設(shè)計(jì)與制作實(shí)訓(xùn)》2023-2024學(xué)年第二學(xué)期期末試卷
- 河南省豫東豫北十所名校2025屆高三第一次月考物理試題文試題含解析
- 常熟中學(xué)2025屆高三下第二次質(zhì)量檢查物理試題含解析
- 江西農(nóng)業(yè)大學(xué)《工程力學(xué)Ⅱ》2023-2024學(xué)年第一學(xué)期期末試卷
- 濰坊職業(yè)學(xué)院《高分子科學(xué)前沿與進(jìn)展》2023-2024學(xué)年第二學(xué)期期末試卷
- 貴州省南白中學(xué)2025屆高三下-第一次強(qiáng)化訓(xùn)練英語試題試卷含解析
- 供應(yīng)鏈管理與采購制度
- 2025年上半年宜賓江安縣人社局招考易考易錯(cuò)模擬試題(共500題)試卷后附參考答案
- 2025年安徽工業(yè)職業(yè)技術(shù)學(xué)院?jiǎn)握新殬I(yè)技能測(cè)試題庫完整版
- 《C#程序設(shè)計(jì)基礎(chǔ)》課件
- 2024年第五屆美麗中國(guó)全國(guó)國(guó)家版圖知識(shí)競(jìng)賽題庫及答案(中小學(xué)組)
- 2025年江蘇航空職業(yè)技術(shù)學(xué)院高職單招職業(yè)適應(yīng)性測(cè)試近5年??及鎱⒖碱}庫含答案解析
- 2023年湖北省技能高考計(jì)算機(jī)類備考題庫(萬維題庫)-中部分(800題)
- S145水表井標(biāo)準(zhǔn)圖集
- 2024年天翼云認(rèn)證運(yùn)維工程師考試復(fù)習(xí)題庫(含答案)
- 2025年云南曲靖市事業(yè)單位定向招聘駐曲部隊(duì)未就業(yè)隨軍家屬10人歷年管理單位筆試遴選500模擬題附帶答案詳解
- 第4章水彈性力學(xué)-流體與剛體、彈性體相互耦合運(yùn)動(dòng)理論
- 2024年10月自考13003數(shù)據(jù)結(jié)構(gòu)與算法試題及答案
評(píng)論
0/150
提交評(píng)論