




版權(quán)說明:本文檔由用戶提供并上傳,收益歸屬內(nèi)容提供方,若內(nèi)容存在侵權(quán),請進(jìn)行舉報或認(rèn)領(lǐng)
文檔簡介
1、精選優(yōu)質(zhì)文檔-傾情為你奉上實驗2熟悉常用的HDFS操作1 實驗?zāi)康?.理解HDFS在Hadoop體系結(jié)構(gòu)中的角色;2.熟練使用HDFS操作常用的Shell命令;3.熟悉HDFS操作常用的Java API。2 實驗平臺操作系統(tǒng):LinuxHadoop版本:或以上版本JDK版本:或以上版本Java IDE:Eclipse3 實驗內(nèi)容和要求1. 編程實現(xiàn)以下指定功能,并利用Hadoop提供的Shell命令完成相同任務(wù):提示:1) 部分Shell命令的參數(shù)路徑只能是本地路徑或者HDFS路徑。2) 若Shell命令的參數(shù)既可以是本地路徑,也可以是HDFS路徑時,務(wù)必注意區(qū)分。為保證操作正確,可指定路徑前
2、綴 或者 注意區(qū)分相對路徑與絕對路徑3) 具體命令的說明可參考教材或 (1) 向HDFS中上傳任意文本文件,如果指定的文件在HDFS中已經(jīng)存在,由用戶指定是追加到原有文件末尾還是覆蓋原有的文件;Shell命令:檢查文件是否存在: ./hdfs dfs -test -e (執(zhí)行完這一句不會輸出結(jié)果,需要繼續(xù)輸入命令" echo $")追加命令: ./hdfs dfs -appendToFile 覆蓋命令1: ./hdfs dfs -copyFromLocal -f 覆蓋命令2: ./hdfs dfs -cp -f 也可以使用如下命令實現(xiàn):(如下代碼可視為一行代碼,在終端中輸入
3、第一行代碼后,直到輸入 fi 才會真正執(zhí)行):if $(./hdfs dfs -test -e ;then $(./hdfs dfs -appendToFile ;else $(./hdfs dfs -copyFromLocal -f ;fiJava代碼:import .*;public class HDFSApi /* * 判斷路徑是否存在 */ public static boolean test(Configuration conf, String path) throws IOException FileSystem fs = (conf); return (new Path(path
4、); /* * 復(fù)制文件到指定路徑 * 若路徑已存在,則進(jìn)行覆蓋 */ public static void copyFromLocalFile(Configuration conf, String localFilePath, String remoteFilePath) throws IOException FileSystem fs = (conf); Path localPath = new Path(localFilePath); Path remotePath = new Path(remoteFilePath); /* 第一個參數(shù)表示是否刪除源文件,第二個參數(shù)表示是否覆蓋 */
5、(false, true, localPath, remotePath); (); /* * 追加文件內(nèi)容 */ public static void appendToFile(Configuration conf, String localFilePath, String remoteFilePath) throws IOException FileSystem fs = (conf); Path remotePath = new Path(remoteFilePath); /* 創(chuàng)建一個文件讀入流 */ FileInputStream in = new FileInputStream(lo
6、calFilePath); /* 創(chuàng)建一個文件輸出流,輸出的內(nèi)容將追加到文件末尾 */ FSDataOutputStream out = (remotePath); /* 讀寫文件內(nèi)容 */ byte data = new byte1024; int read = -1; while ( (read = (data) > 0 ) (data, 0, read); (); (); (); /* * 主函數(shù) */public static void main(String args) Configuration conf = new Configuration(); (""
7、;,"");String localFilePath = "/home/hadoop/" ; else + " 不存在.");/* 進(jìn)行處理 */if ( !fileExists) (2)hdfs dfs -test -e ;then $(./hdfs dfs -copyToLocal ./; else $(./hdfs dfs -copyToLocal ./; fiJava代碼:import .*;public class HDFSApi /* * 下載文件到本地 * 判斷本地路徑是否已存在,若已存在,則自動進(jìn)行重命名 */ pub
8、lic static void copyToLocal(Configuration conf, String remoteFilePath, String localFilePath) throws IOException FileSystem fs = (conf); Path remotePath = new Path(remoteFilePath); File f = new File(localFilePath); /* 如果文件名存在,自動重命名(在文件名后面加上 _0, _1 .) */ if () + " 已存在."); Integer i = 0; whil
9、e (true) f = new File(localFilePath + "_" + (); if (!() localFilePath = localFilePath + "_" + (); break; "將重新命名為: " + localFilePath); (3)hdfs dfs -cat Java代碼:import .*;public class HDFSApi /* * 讀取文件內(nèi)容 */ public static void cat(Configuration conf, String remoteFilePath)
10、throws IOException FileSystem fs = (conf); Path remotePath = new Path(remoteFilePath); FSDataInputStream in = (remotePath); BufferedReader d = new BufferedReader(new InputStreamReader(in); String line = null; while ( (line = () != null ) (); (); (); /* * 主函數(shù) */public static void main(String args) Co
11、nfiguration conf = new Configuration(); ("","");String remoteFilePath = "/user/hadoop/" (4)hdfs dfs -ls -h Java代碼:import .*;import class HDFSApi /* * 顯示指定文件的信息 */ public static void ls(Configuration conf, String remoteFilePath) throws IOException FileSystem fs = (conf);
12、 Path remotePath = new Path(remoteFilePath); FileStatus fileStatuses = (remotePath); for (FileStatus s : fileStatuses) "路徑: " + ().toString(); "權(quán)限: " + ().toString(); "大小: " + (); /* 返回的是時間戳,轉(zhuǎn)化為時間日期格式 */ Long timeStamp = (); SimpleDateFormat format = new SimpleDateForma
13、t("yyyy-MM-dd HH:mm:ss"); String date = (timeStamp); "時間: " + date); (); /* * 主函數(shù) */public static void main(String args) Configuration conf = new Configuration(); ("","");String remoteFilePath = "/user/hadoop/" (5)hdfs dfs -ls -R -h /user/hadoopJava代
14、碼:import .*;import class HDFSApi /* * 顯示指定文件夾下所有文件的信息(遞歸) */ public static void lsDir(Configuration conf, String remoteDir) throws IOException FileSystem fs = (conf); Path dirPath = new Path(remoteDir); /* 遞歸獲取目錄下的所有文件 */ RemoteIterator<LocatedFileStatus> remoteIterator = (dirPath, true); /* 輸
15、出每個文件的信息 */ while () FileStatus s = (); "路徑: " + ().toString(); "權(quán)限: " + ().toString(); "大小: " + (); /* 返回的是時間戳,轉(zhuǎn)化為時間日期格式 */ Long timeStamp = (); SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss"); String date = (timeStamp); "時間: "
16、 + date); (); /* * 主函數(shù) */public static void main(String args) Configuration conf = new Configuration(); ("","");String remoteDir = "/user/hadoop" (6)hdfs dfs -test -d dir1/dir2);then $(./hdfs dfs -touchz dir1/dir2/filename); else $(./hdfs dfs -mkdir -p dir1/dir2 &&a
17、mp; hdfs dfs -touchz dir1/dir2/filename); fi刪除文件:./hdfs dfs -rm dir1/dir2/filenameJava代碼:import .*;public class HDFSApi /* * 判斷路徑是否存在 */ public static boolean test(Configuration conf, String path) throws IOException FileSystem fs = (conf); return (new Path(path); /* * 創(chuàng)建目錄 */ public static boolean m
18、kdir(Configuration conf, String remoteDir) throws IOException FileSystem fs = (conf); Path dirPath = new Path(remoteDir); boolean result = (dirPath); (); return result; /* * 創(chuàng)建文件 */ public static void touchz(Configuration conf, String remoteFilePath) throws IOException FileSystem fs = (conf); Path r
19、emotePath = new Path(remoteFilePath); FSDataOutputStream outputStream = (remotePath); (); (); /* * 刪除文件 */ public static boolean rm(Configuration conf, String remoteFilePath) throws IOException FileSystem fs = (conf); Path remotePath = new Path(remoteFilePath); boolean result = (remotePath, false);
20、(); return result; /* * 主函數(shù) */public static void main(String args) Configuration conf = new Configuration(); ("","");String remoteFilePath = "/user/hadoop/input/" (7)hdfs dfs -mkdir -p dir1/dir2刪除目錄(如果目錄非空則會提示not empty,不執(zhí)行刪除):./hdfs dfs -rmdir dir1/dir2強制刪除目錄:./hdfs dfs
21、 -rm -R dir1/dir2Java代碼:import .*;public class HDFSApi /* * 判斷路徑是否存在 */ public static boolean test(Configuration conf, String path) throws IOException FileSystem fs = (conf); return (new Path(path); /* * 判斷目錄是否為空 * true: 空,false: 非空 */ public static boolean isDirEmpty(Configuration conf, String remo
22、teDir) throws IOException FileSystem fs = (conf); Path dirPath = new Path(remoteDir); RemoteIterator<LocatedFileStatus> remoteIterator = (dirPath, true); return !(); /* * 創(chuàng)建目錄 */ public static boolean mkdir(Configuration conf, String remoteDir) throws IOException FileSystem fs = (conf); Path d
23、irPath = new Path(remoteDir); boolean result = (dirPath); (); return result; /* * 刪除目錄 */ public static boolean rmDir(Configuration conf, String remoteDir) throws IOException FileSystem fs = (conf); Path dirPath = new Path(remoteDir); /* 第二個參數(shù)表示是否遞歸刪除所有文件 */ boolean result = (dirPath, true); (); ret
24、urn result; /* * 主函數(shù) */public static void main(String args) Configuration conf = new Configuration(); ("","");String remoteDir = "/user/hadoop/input" (8)hdfs dfs -appendToFile 追加到文件開頭:(由于沒有直接的命令可以操作,方法之一是先移動到本地進(jìn)行操作,再進(jìn)行上傳覆蓋):./hdfs dfs -get cat >> ./hdfs dfs -copyF
25、romLocal -f Java代碼:import .*;public class HDFSApi /* * 判斷路徑是否存在 */ public static boolean test(Configuration conf, String path) throws IOException FileSystem fs = (conf); return (new Path(path); /* * 追加文本內(nèi)容 */ public static void appendContentToFile(Configuration conf, String content, String remoteFil
26、ePath) throws IOException FileSystem fs = (conf); Path remotePath = new Path(remoteFilePath); /* 創(chuàng)建一個文件輸出流,輸出的內(nèi)容將追加到文件末尾 */ FSDataOutputStream out = (remotePath); (); (); (); /* * 追加文件內(nèi)容 */ public static void appendToFile(Configuration conf, String localFilePath, String remoteFilePath) throws IOExce
27、ption FileSystem fs = (conf); Path remotePath = new Path(remoteFilePath); /* 創(chuàng)建一個文件讀入流 */ FileInputStream in = new FileInputStream(localFilePath); /* 創(chuàng)建一個文件輸出流,輸出的內(nèi)容將追加到文件末尾 */ FSDataOutputStream out = (remotePath); /* 讀寫文件內(nèi)容 */ byte data = new byte1024; int read = -1; while ( (read = (data) > 0
28、) (data, 0, read); (); (); (); /* * 移動文件到本地 * 移動后,刪除源文件 */ public static void moveToLocalFile(Configuration conf, String remoteFilePath, String localFilePath) throws IOException FileSystem fs = (conf); Path remotePath = new Path(remoteFilePath); Path localPath = new Path(localFilePath); (remotePath,
29、 localPath); /* * 創(chuàng)建文件 */ public static void touchz(Configuration conf, String remoteFilePath) throws IOException FileSystem fs = (conf); Path remotePath = new Path(remoteFilePath); FSDataOutputStream outputStream = (remotePath); (); (); /* * 主函數(shù) */public static void main(String args) Configuration
30、conf = new Configuration(); ("","");String remoteFilePath = "/user/hadoop/" (9)hdfs dfs -rm Java命令:import .*;public class HDFSApi /* * 刪除文件 */ public static boolean rm(Configuration conf, String remoteFilePath) throws IOException FileSystem fs = (conf); Path remotePath
31、= new Path(remoteFilePath); boolean result = (remotePath, false); (); return result; /* * 主函數(shù) */public static void main(String args) Configuration conf = new Configuration(); ("","");String remoteFilePath = "/user/hadoop/" (10)hdfs dfs -rmdir dir1/dir2強制刪除目錄:./hdfs dfs
32、-rm -R dir1/dir2Java代碼:import .*;public class HDFSApi /* * 判斷目錄是否為空 * true: 空,false: 非空 */ public static boolean isDirEmpty(Configuration conf, String remoteDir) throws IOException FileSystem fs = (conf); Path dirPath = new Path(remoteDir); RemoteIterator<LocatedFileStatus> remoteIterator = (d
33、irPath, true); return !(); /* * 刪除目錄 */ public static boolean rmDir(Configuration conf, String remoteDir, boolean recursive) throws IOException FileSystem fs = (conf); Path dirPath = new Path(remoteDir); /* 第二個參數(shù)表示是否遞歸刪除所有文件 */ boolean result = (dirPath, recursive); (); return result; /* * 主函數(shù) */pub
34、lic static void main(String args) Configuration conf = new Configuration(); ("","");String remoteDir = "/user/hadoop/input" (11)hdfs dfs -mv Java代碼:import .*;public class HDFSApi /* * 移動文件 */ public static boolean mv(Configuration conf, String remoteFilePath, String rem
35、oteToFilePath) throws IOException FileSystem fs = (conf); Path srcPath = new Path(remoteFilePath); Path dstPath = new Path(remoteToFilePath); boolean result = (srcPath, dstPath); (); return result; /* * 主函數(shù) */public static void main(String args) Configuration conf = new Configuration(); ("","");String remoteFilePath = "" 2.;public class MyFSDa
溫馨提示
- 1. 本站所有資源如無特殊說明,都需要本地電腦安裝OFFICE2007和PDF閱讀器。圖紙軟件為CAD,CAXA,PROE,UG,SolidWorks等.壓縮文件請下載最新的WinRAR軟件解壓。
- 2. 本站的文檔不包含任何第三方提供的附件圖紙等,如果需要附件,請聯(lián)系上傳者。文件的所有權(quán)益歸上傳用戶所有。
- 3. 本站RAR壓縮包中若帶圖紙,網(wǎng)頁內(nèi)容里面會有圖紙預(yù)覽,若沒有圖紙預(yù)覽就沒有圖紙。
- 4. 未經(jīng)權(quán)益所有人同意不得將文件中的內(nèi)容挪作商業(yè)或盈利用途。
- 5. 人人文庫網(wǎng)僅提供信息存儲空間,僅對用戶上傳內(nèi)容的表現(xiàn)方式做保護(hù)處理,對用戶上傳分享的文檔內(nèi)容本身不做任何修改或編輯,并不能對任何下載內(nèi)容負(fù)責(zé)。
- 6. 下載文件中如有侵權(quán)或不適當(dāng)內(nèi)容,請與我們聯(lián)系,我們立即糾正。
- 7. 本站不保證下載資源的準(zhǔn)確性、安全性和完整性, 同時也不承擔(dān)用戶因使用這些下載資源對自己和他人造成任何形式的傷害或損失。
最新文檔
- 航天信息財務(wù)培訓(xùn)
- 綜合實踐課:水與健康
- 舞蹈培訓(xùn)匯報演出
- TTT培訓(xùn)師成長特訓(xùn)營
- 腫瘤放化療科出科培訓(xùn)大綱
- 客車操作培訓(xùn)課件
- 女士正裝培訓(xùn)
- 培訓(xùn)銷售流程
- 腫瘤患者飲食營養(yǎng)護(hù)理
- 酒店前廳服務(wù)流程標(biāo)準(zhǔn)化管理
- 高考英語讀后續(xù)寫:三大主題語境結(jié)尾金句
- 直飲水施工合同協(xié)議
- 老年護(hù)理技能和知識培訓(xùn)
- 供應(yīng)商現(xiàn)場審核計劃管理制度
- 售電業(yè)務(wù)知識培訓(xùn)課件
- 預(yù)防病人走失
- 2025-2030中國抗體藥物結(jié)合物(ADC)行業(yè)市場發(fā)展趨勢與前景展望戰(zhàn)略研究報告
- 2025-2030中國安全儀表系統(tǒng)行業(yè)市場發(fā)展趨勢與前景展望戰(zhàn)略研究報告
- 2025年非高危行業(yè)生產(chǎn)經(jīng)營單位主要負(fù)責(zé)人安全培訓(xùn)(復(fù)訓(xùn))考試題庫-下(判斷題)
- 2025年江蘇防雷考試試題及答案
- T-SBIAORG 001-2023 間充質(zhì)干細(xì)胞外泌體質(zhì)量控制標(biāo)準(zhǔn)
評論
0/150
提交評論