初识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

原文地址:https://www.cnblogs.com/hujiapeng/p/5933750.html