初識Hadoop二,文件操作


1、使用hadoop命令查看hdfs下文件

[root@localhost hadoop-2.7.3]# hadoop fs -ls hdfs://192.168.36.134:9000/

開始在secureCRT上執行這條命令失敗,使用netstat -nltp命令查看監聽的9000端口,是127.0.0.1:9000,沒有找到辦法更改這個監聽的IP和端口

后來就把etc/hadoop/core-site.xml配置下的localhost改為192.168.36.134,保存配置重啟HDFS,使用上面命令還是不行,重啟系統后再啟動hdfs和yarn,再用上面命令就不報錯了,命令執行后沒有任何反應,那是因為沒有在HDFS系統上存文件

2、上傳文件到HDFS系統,例子中上傳/home/jdk/jdk-8u73-linux-x64.tar.gz文件

[root@localhost jdk]# hadoop fs -put jdk-8u73-linux-x64.tar.gz hdfs://192.168.36.134:9000/

上傳完成后再查看hdfs下文件hadoop fs -ls hdfs://192.168.36.134:9000/或hadoop fs -ls /

[root@localhost sbin]# hadoop fs -ls /
Found 1 items
-rw-r--r-- 1 root supergroup 181310701 2016-10-06 15:35 /jdk-8u73-linux-x64.tar.gz
[root@localhost sbin]#

這個上傳的文件就被切割成塊分別存放在datanode節點上了,由於都在同一台服務器上,那么文件被分的塊在路徑/home/hadoop/hadoop-2.7.3/tmp/dfs/data/current/BP-944456004-127.0.0.1-1475724779784/current/finalized/subdir0/subdir0下,分成兩塊大小分別為134217728和47092973

[root@localhost subdir0]# ll
total 178452
-rw-r--r--. 1 root root 134217728 Oct 6 15:35 blk_1073741825
-rw-r--r--. 1 root root 1048583 Oct 6 15:35 blk_1073741825_1001.meta
-rw-r--r--. 1 root root 47092973 Oct 6 15:35 blk_1073741826
-rw-r--r--. 1 root root 367923 Oct 6 15:35 blk_1073741826_1002.meta
[root@localhost subdir0]# pwd
/home/hadoop/hadoop-2.7.3/tmp/dfs/data/current/BP-944456004-127.0.0.1-1475724779784/current/finalized/subdir0/subdir0
[root@localhost subdir0]#

3、下載HDFS系統文件

hadoop fs -get hdfs://192.168.36.134:9000/jdk-8u73-linux-x64.tar.gz或hadoop fs -get /jdk-8u73-linux-x64.tar.gz

先用ll命令查看當前文件夾下不存在該文件,下載后再查看,該文件已被下載

[root@localhost ~]# ll
total 4
-rw-------. 1 root root 998 Sep 26 17:58 anaconda-ks.cfg
[root@localhost ~]# hadoop fs -get hdfs://192.168.36.134:9000/jdk-8u73-linux-x64.tar.gz
[root@localhost ~]# ll
total 177068
-rw-------. 1 root root 998 Sep 26 17:58 anaconda-ks.cfg
-rw-r--r--. 1 root root 181310701 Oct 6 17:24 jdk-8u73-linux-x64.tar.gz
[root@localhost ~]#

4、使用mapreduce算法統計單詞數量,一般是在Java程序中使用的,這次就直接在hadoop安裝目錄下mapreduce下的Java例子中實驗

  1)、首先切換到mapreduce路徑下:cd /home/hadoop/hadoop-2.7.3/share/hadoop/mapreduce會看到 hadoop-mapreduce-examples-2.7.3.jar文件

  2)、新建一個測試文件,並填寫測試數據:vi test.data 里面隨便寫入英文單詞如,Youth is not a time of life; it is a state of mind; it is not a matter of rosy cheeks, red lips and supple knees; it is a matter of the will, a quality of the imagination, a vigor of the emotions; it is the freshness of the deep springs of life.保存后上傳到HDFS系統中

  3)、在HDFS系統中新建文件夾存放測試數據:

[root@localhost mapreduce]# hadoop fs -mkdir /test
[root@localhost mapreduce]# hadoop fs -mkdir /test/testdata
[root@localhost mapreduce]# hadoop fs -put test.data /test/testdata

[root@localhost mapreduce]# hadoop fs -ls /
Found 2 items
-rw-r--r-- 1 root supergroup 181310701 2016-10-06 15:35 /jdk-8u73-linux-x64.tar.gz
drwxr-xr-x - root supergroup 0 2016-10-06 17:54 /test
[root@localhost mapreduce]#

  4)、執行單詞統計操作,/test/output為輸出路徑

[root@localhost mapreduce]# hadoop jar hadoop-mapreduce-examples-2.7.3.jar wordcount /test/testdata /test/output

  5)、查看輸出結果

[root@localhost mapreduce]# hadoop fs -ls /test/output
Found 2 items
-rw-r--r-- 1 root supergroup 0 2016-10-06 18:13 /test/output/_SUCCESS
-rw-r--r-- 1 root supergroup 223 2016-10-06 18:12 /test/output/part-r-00000
[root@localhost mapreduce]# hadoop fs -cat /test/output/part-r-00000

5、根據取樣獲取pi值,取樣越多pi值越精確,下面以10X10為例

[root@localhost mapreduce]# hadoop jar hadoop-mapreduce-examples-2.7.3.jar pi 10 10

 


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM