设为首页 收藏本站
查看: 1054|回复: 0

[经验分享] 二、hadoop文件操作

[复制链接]

尚未签到

发表于 2017-12-17 07:33:07 | 显示全部楼层 |阅读模式
  1、使用hadoop命令查看hdfs下文件
[iyunv@localhost hadoop-2.7.2]# hadoop fs -ls hdfs://192.168.211.129:9000/  (最后一定要加/)

  开始在xshell上执行这条命令失败,使用netstat -nltp命令查看监听的9000端口,是127.0.0.1:9000,没有找到办法更改这个监听的IP和端口
  后来就把etc/hadoop/core-site.xml配置下的localhost改为192.168.211.129,保存配置重启HDFS,使用上面命令还是不行,重启系统后再启动hdfs和yarn,再用上面命令就不报错了,命令执行后没有任何反应,那是因为没有在HDFS系统上存文件
  2、上传文件到HDFS系统,例子中上传/jdk-7u80-linux-x64.rpm文件
[iyunv@localhost hadoop-2.7.2]# hadoop fs -put /jdk-7u80-linux-x64.rpm  hdfs://192.168.211.129:9000/

  上传完成后再查看hdfs下文件
  hadoop fs -ls -h hdfs://192.168.211.129:9000//或hadoop fs -ls -h /
[iyunv@localhost sbin]# hadoop fs -ls /

  Found 1 items -rw-r--r--   1 root supergroup    131.7 M 2017-04-19 00:00 /jdk-7u80-linux-x64.rpm  
  这个上传的文件就被切割成块分别存放在datanode节点上了,由于都在同一台服务器上,那么文件被分的块在路径
  /home/hadoop/hadoop-2.7.2/tmp/dfs/data/current/BP-2144136712-127.0.0.1-1492526252533/current/finalized/subdir0/subdir0
  进入到该目录可以查看到相关的块信息:
[iyunv@localhost subdir0]# ll

  总用量 135916
  -rw-r--r--. 1 root root 134217728 4月  19 00:00 blk_1073741825
  -rw-r--r--. 1 root root   1048583 4月  19 00:00 blk_1073741825_1001.meta
  -rw-r--r--. 1 root root   3872558 4月  19 00:00 blk_1073741826
  -rw-r--r--. 1 root root     30263 4月  19 00:00 blk_1073741826_1002.meta
[iyunv@localhost subdir0]# pwd

  /home/hadoop/hadoop-2.7.2/tmp/dfs/data/current/BP-2144136712-127.0.0.1-1492526252533/current/finalized/subdir0/subdir0
[iyunv@localhost subdir0]#

  3、下载HDFS系统文件
  hadoop fs -get hdfs://192.168.211.129:9000/jdk-7u80-linux-x64.rpm或hadoop fs -get /jdk-7u80-linux-x64.rpm
  先用ll命令查看当前文件夹下不存在该文件,下载后再查看,该文件已被下载
[iyunv@localhost ~]# ll total 4 -rw-r--r--. 1 root root  8252 4月  17 23:28 install.log.syslog

[iyunv@localhost ~]# hadoop fs -get hdfs://192.168.211.129:9000/jdk-7u80-linux-x64.rpm

[iyunv@localhost ~]# ll

  总用量 134904
  -rw-------. 1 root root 998 Sep 26 17:58 anaconda-ks.cfg
  -rw-r--r--. 1 root root 138090286 4月  19 00:15 jdk-7u80-linux-x64.rpm
[iyunv@localhost ~]#

  4、使用mapreduce算法统计单词数量,一般是在Java程序中使用的,这次就直接在hadoop安装目录下mapreduce下的Java例子中实验
  1)、首先切换到mapreduce路径下:cd /home/hadoop/hadoop-2.7.2/share/hadoop/mapreduce会看到 hadoop-mapreduce-examples-2.7.2.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系统中新建文件夹存放测试数据:
[iyunv@localhost mapreduce]# hadoop fs -mkdir /test

[iyunv@localhost mapreduce]# hadoop fs -mkdir /test/testdata

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

[iyunv@localhost mapreduce]# hadoop fs -ls /

  Found 2 items
  -rw-r--r-- 1 root supergroup 181310701 2017-04-19 15:35 /jdk-7u80-linux-x64.rpm
  drwxr-xr-x - root supergroup 0 2017-04-19 15:35 /test
[iyunv@localhost mapreduce]#

  4)、执行单词统计操作,/test/output为输出路径
[iyunv@localhost mapreduce]# hadoop jar hadoop-mapreduce-examples-2.7.2.jar wordcount /test/testdata /test/output

  5)、查看输出结果
[iyunv@localhost mapreduce]# hadoop fs -ls /test/output

  Found 2 items
  -rw-r--r-- 1 root supergroup 0 2017-04-19 15:35 /test/output/_SUCCESS
  -rw-r--r-- 1 root supergroup 223 2017-04-19 15:35 /test/output/part-r-00000
[iyunv@localhost mapreduce]# hadoop fs -cat /test/output/part-r-00000

  Youth 1
  a 6
  and 1
  cheeks, 1
  deep 1
  emotions; 1
  freshness 1
  imagination, 1
  is 5
  it 4
  knees; 1
  life. 1
  life; 1
  lips 1
  matter 2
  mind; 1
  not 2
  of 8
  quality 1
  red 1
  rosy 1
  springs 1
  state 1
  supple 1
  the 5
  time 1
  vigor 1
  will, 1

运维网声明 1、欢迎大家加入本站运维交流群:群②:261659950 群⑤:202807635 群⑦870801961 群⑧679858003
2、本站所有主题由该帖子作者发表,该帖子作者与运维网享有帖子相关版权
3、所有作品的著作权均归原作者享有,请您和我们一样尊重他人的著作权等合法权益。如果您对作品感到满意,请购买正版
4、禁止制作、复制、发布和传播具有反动、淫秽、色情、暴力、凶杀等内容的信息,一经发现立即删除。若您因此触犯法律,一切后果自负,我们对此不承担任何责任
5、所有资源均系网友上传或者通过网络收集,我们仅提供一个展示、介绍、观摩学习的平台,我们不对其内容的准确性、可靠性、正当性、安全性、合法性等负责,亦不承担任何法律责任
6、所有作品仅供您个人学习、研究或欣赏,不得用于商业或者其他用途,否则,一切后果均由您自己承担,我们对此不承担任何法律责任
7、如涉及侵犯版权等问题,请您及时通知我们,我们将立即采取措施予以解决
8、联系人Email:admin@iyunv.com 网址:www.yunweiku.com

所有资源均系网友上传或者通过网络收集,我们仅提供一个展示、介绍、观摩学习的平台,我们不对其承担任何法律责任,如涉及侵犯版权等问题,请您及时通知我们,我们将立即处理,联系人Email:kefu@iyunv.com,QQ:1061981298 本贴地址:https://www.yunweiku.com/thread-424914-1-1.html 上篇帖子: Hive和SparkSQL: 基于 Hadoop 的数据仓库工具 下篇帖子: Hadoop集群的安装
您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

扫码加入运维网微信交流群X

扫码加入运维网微信交流群

扫描二维码加入运维网微信交流群,最新一手资源尽在官方微信交流群!快快加入我们吧...

扫描微信二维码查看详情

客服E-mail:kefu@iyunv.com 客服QQ:1061981298


QQ群⑦:运维网交流群⑦ QQ群⑧:运维网交流群⑧ k8s群:运维网kubernetes交流群


提醒:禁止发布任何违反国家法律、法规的言论与图片等内容;本站内容均来自个人观点与网络等信息,非本站认同之观点.


本站大部分资源是网友从网上搜集分享而来,其版权均归原作者及其网站所有,我们尊重他人的合法权益,如有内容侵犯您的合法权益,请及时与我们联系进行核实删除!



合作伙伴: 青云cloud

快速回复 返回顶部 返回列表