大資料入門(4)hdfs的shell語法
1、測試hdfs檔案上傳和下載(HDFS shell)
1.0檢視幫助
hadoop fs -help <cmd>
1.1上傳
hadoop fs -put <linux上檔案> <hdfs上的路徑>
hadoop fs -put jdk-7u71-linux-x64.tar.gz hdfs://192.168.21.115:9000/
1.2檢視檔案內容
hadoop fs -cat <hdfs上的路徑>
1.3檢視檔案列表
hadoop fs -ls /
1.4下載檔案
hadoop fs -get <hdfs上的路徑> <linux上檔案>
hadoop fs -get hdfs://192.168.1.115:9000/jdk-7u71-linux-x64.tar.gz
1.5新建檔案(一層一層的建)
hadoop fs -mkdir /aa
hadoop fs -mkdir /aa/bb
1.6刪除資料夾
hadoop fs -rm -r /aa/bb
1.7 統計檔案大小
hadoop fs -du -s -h hdfs://192.168.1.115:9000/
1.8、copy 檔案到指定虛擬機器目錄
scp 檔案 192.168.1.116:/home/admin
2、測試mapreduce
使用shell自帶的程式測試(app/hadoop-2.4.1/share/hadoop/mapreduce)
hadoop jar hadoop-mapreduce-examples-2.4.1.jar pi 5 5
新建txt檔案: vi test.txt,寫入hello word
新建input資料夾,將檔案放到input資料夾中
hadoop fs -mkdir /wordcount
hadoop fs -mkdir /wordcount/input
hadoop fs -put test.txt /wordcount/input
執行計算,test.txt 中的文字個數,輸入結果放到output資料夾中
hadoop jar hadoop-mapreduce-examples-2.4.1.jar wordcount /wordcount/input /wordcount/output
檢視檔案列表:hadoop fs -ls /wordcount/output
檢視輸入檔案:hadoop fs -cat /wordcount/output/part-r-00000
2.使用java介面操作HDFS
見eclipse工程下的demo
本機修改ip對映:C:\Windows\System32\drivers\etc下的host檔案,配置對映
3.hadoop通訊機制
不同程序之間的方法進行呼叫
4.HDFS原始碼分析
FileSystem.get --> 通過反射例項化了一個DistributedFileSystem --> new DFSCilent()把他作為自己的成員變數
在DFSClient構造方法裡面,呼叫了createNamenode,使用了RPC機制,得到了一個NameNode的代理物件,就可以和NameNode進行通訊了
FileSystem --> DistributedFileSystem --> DFSClient --> NameNode的代理