• CentOS7下搭建Hadoop(3.3.4)集群


    7.0 MapReduce 编程 | 菜鸟教程

    1. 前期准备

    1)规划

    主机IP角色
    172.16.10.67172.16.10.67NameNode、SecondaryNameNode、ResourceManager、DataNode、NodeManager
    172.16.10.68172.16.10.68DataNode、NodeManager
    172.16.10.68172.16.10.69DataNode、NodeManager

    2、环境搭建

    一、配置host(三台机器) 我没配置,如果配置

    1. # 设置主机名
    2. hostnamectl set-hostname node01
    3. # hosts映射
    4. cat /etc/hosts
    5. 172.16.10.67 node01
    6. 172.16.10.68 node02
    7. 172.16.10.69 node03

    二、关闭防火墙(三台机器)

    1. # 查看防火墙状态
    2. firewall-cmd --state
    3. # 停止firewalld服务
    4. systemctl stop firewalld.service
    5. # 开机禁用firewalld服务
    6. systemctl disable firewalld.service

    三、ssh免密登录

    注:只需要配置node1至node1、node2、node3即可

    1. #node1生成公钥私钥 (一路回车)
    2. ssh-keygen
    3. #node1配置免密登录到node1 node2 node3
    4. ssh-copy-id node1
    5. ssh-copy-id node2
    6. ssh-copy-id node3

    四、集群时间同步(三台机器)

    1. yum install ntpdate
    2. # 网络时间同步
    3. ntpdate ntp5.aliyun.com

    五、创建统一工作目录(三台机器)

    mkdir -p /root/hadoop/server

    六、安装Hadoop,所有节点

    1. [root@172 server]# wget --no-check-certificate -c https://mirrors.tuna.tsinghua.edu.cn/apache/hadoop/common/hadoop-3.3.4/hadoop-3.3.4.tar.gz
    2. JAVA_HOME=/usr/java/jdk
    3. PATH=$GRADLE_HOME/bin:$JAVA_HOME/bin:$NODE_HOME/bin:$NODE_HOME1/bin:$PATH
    4. CLASSPATH=.:$JAVA_HOME/jre/lib/ext:$JAVA_HOME/jre/lib/ext/jar:$JAVA_HOME/lib/tools.jar
    5. export PATH JAVA_HOME CLASSPATH
    6. export HADOOP_HOME=/root/hadoop/server/hadoop
    7. export HADOOP_INSTALL=$HADOOP_HOME
    8. export HADOOP_MAPRED_HOME=$HADOOP_HOME
    9. export HADOOP_HDFS_HOME=$HADOOP_HOME
    10. export HADOOP_COMMON_HOME=$HADOOP_HOME
    11. export HADOOP_CONF_DIR=$HADOOP_HOME/etc/hadoop
    12. export PATH=$PATH:$HADOOP_HOME/sbin:$HADOOP_HOME/bin
    13. # 配置文件生效
    14. source /etc/profile
    15. # 检查是否成功
    16. [root@172 server]# hadoop version
    17. Hadoop 3.3.4
    18. Source code repository https://github.com/apache/hadoop.git -r a585a73c3e02ac62350c136643a5e7f6095a3dbb
    19. Compiled by stevel on 2022-07-29T12:32Z
    20. Compiled with protoc 3.7.1
    21. From source with checksum fb9dd8918a7b8a5b430d61af858f6ec
    22. This command was run using /root/hadoop/server/hadoop/share/hadoop/common/hadoop-common-3.3.4.jar

    七、修改配置文件

    要获取的默认文件文件存放在Hadoop的jar包中的位置作用
    [core-site.xml]hadoop-common-2.7.2.jar/ core-default.xmlhdaoop核心模块配置文件
    [hdfs-site.xml]hadoop-hdfs-2.7.2.jar/ hdfs-default.xmlhdfs文件系统模块配置
    [yarn-site.xml]hadoop-yarn-common-2.7.2.jar/ yarn-default.xmlyarn模块配置
    [mapred-site.xml]hadoop-mapreduce-client-core-2.7.2.jar/ mapred-default.xmlMapReduce模块配置

    core-site.xml: 

    1. [root@172 hadoop]# cat core-site.xml
    2. <?xml version="1.0" encoding="UTF-8"?>
    3. <?xml-stylesheet type="text/xsl" href="configuration.xsl"?>
    4. <!--
    5. Licensed under the Apache License, Version 2.0 (the "License");
    6. you may not use this file except in compliance with the License.
    7. You may obtain a copy of the License at
    8. http://www.apache.org/licenses/LICENSE-2.0
    9. Unless required by applicable law or agreed to in writing, software
    10. distributed under the License is distributed on an "AS IS" BASIS,
    11. WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
    12. See the License for the specific language governing permissions and
    13. limitations under the License. See accompanying LICENSE file.
    14. -->
    15. <!-- Put site-specific property overrides in this file. -->
    16. <configuration>
    17. <!-- 设置默认使用的文件系统 Hadoop支持file、HDFS、GFS、ali|Amazon云等文件系统 -->
    18. <property>
    19. <name>fs.defaultFS</name>
    20. <value>hdfs://172.16.10.67:8020</value>
    21. </property>
    22. <!-- 设置Hadoop本地保存数据路径 -->
    23. <property>
    24. <name>hadoop.tmp.dir</name>
    25. <value>/root/hadoop/server/hadoop</value>
    26. </property>
    27. <!-- 设置HDFS web UI用户身份 -->
    28. <property>
    29. <name>hadoop.http.staticuser.user</name>
    30. <value>root</value>
    31. </property>
    32. <!-- 整合hive 用户代理设置 -->
    33. <property>
    34. <name>hadoop.proxyuser.root.hosts</name>
    35. <value>*</value>
    36. </property>
    37. <property>
    38. <name>hadoop.proxyuser.root.groups</name>
    39. <value>*</value>
    40. </property>
    41. <!-- 文件系统垃圾桶保存时间 -->
    42. <property>
    43. <name>fs.trash.interval</name>
    44. <value>1440</value>
    45. </property>
    46. </configuration>

    hdfs-site.xml

    1. [root@172 hadoop]# cat hdfs-site.xml
    2. <?xml version="1.0" encoding="UTF-8"?>
    3. <?xml-stylesheet type="text/xsl" href="configuration.xsl"?>
    4. <!--
    5. Licensed under the Apache License, Version 2.0 (the "License");
    6. you may not use this file except in compliance with the License.
    7. You may obtain a copy of the License at
    8. http://www.apache.org/licenses/LICENSE-2.0
    9. Unless required by applicable law or agreed to in writing, software
    10. distributed under the License is distributed on an "AS IS" BASIS,
    11. WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
    12. See the License for the specific language governing permissions and
    13. limitations under the License. See accompanying LICENSE file.
    14. -->
    15. <!-- Put site-specific property overrides in this file. -->
    16. <configuration>
    17. <!-- 设置SNN进程运行机器位置信息 -->
    18. <property>
    19. <name>dfs.replication</name>
    20. <value>3</value>
    21. </property>
    22. <property>
    23. <name>dfs.namenode.secondary.http-address</name>
    24. <value>172.16.10.67:9868</value>
    25. </property>
    26. </configuration>

    yarn-site.xml 

    1. [root@172 hadoop]# cat yarn-site.xml
    2. <?xml version="1.0"?>
    3. <!--
    4. Licensed under the Apache License, Version 2.0 (the "License");
    5. you may not use this file except in compliance with the License.
    6. You may obtain a copy of the License at
    7. http://www.apache.org/licenses/LICENSE-2.0
    8. Unless required by applicable law or agreed to in writing, software
    9. distributed under the License is distributed on an "AS IS" BASIS,
    10. WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
    11. See the License for the specific language governing permissions and
    12. limitations under the License. See accompanying LICENSE file.
    13. -->
    14. <configuration>
    15. <!-- Site specific YARN configuration properties -->
    16. <!-- 设置YARN集群主角色运行机器位置 -->
    17. <property>
    18. <name>yarn.resourcemanager.hostname</name>
    19. <value>172.16.10.67</value>
    20. </property>
    21. <property>
    22. <name>yarn.nodemanager.aux-services</name>
    23. <value>mapreduce_shuffle</value>
    24. </property>
    25. <!-- 是否将对容器实施物理内存限制 -->
    26. <property>
    27. <name>yarn.nodemanager.pmem-check-enabled</name>
    28. <value>false</value>
    29. </property>
    30. <!-- 是否将对容器实施虚拟内存限制。 -->
    31. <property>
    32. <name>yarn.nodemanager.vmem-check-enabled</name>
    33. <value>false</value>
    34. </property>
    35. <!-- 开启日志聚集 -->
    36. <property>
    37. <name>yarn.log-aggregation-enable</name>
    38. <value>true</value>
    39. </property>
    40. <!-- 设置yarn历史服务器地址 -->
    41. <property>
    42. <name>yarn.log.server.url</name>
    43. <value>http://172.16.10.67:19888/jobhistory/logs</value>
    44. </property>
    45. <!-- 历史日志保存的时间 7天 -->
    46. <property>
    47. <name>yarn.log-aggregation.retain-seconds</name>
    48. <value>604800</value>
    49. </property>
    50. <!--每个磁盘的磁盘利用率百分比-->
    51. <property>
    52. <name>yarn.nodemanager.disk-health-checker.max-disk-utilization-per-disk-percentage</name>
    53. <value>95.0</value>
    54. </property>
    55. <!--集群内存-->
    56. <property>
    57. <name>yarn.nodemanager.resource.memory-mb</name>
    58. <value>2048</value>
    59. </property>
    60. <!--调度程序最小值-分配-->
    61. <property>
    62. <name>yarn.scheduler.minimum-allocation-mb</name>
    63. <value>2048</value>
    64. </property>
    65. <!--比率,具体是啥比率还没查...-->
    66. <property>
    67. <name>yarn.nodemanager.vmem-pmem-ratio</name>
    68. <value>2.1</value>
    69. </property>
    70. </configuration>

    mapred-site.xml 

    1. [root@172 hadoop]# cat mapred-site.xml
    2. <?xml version="1.0"?>
    3. <?xml-stylesheet type="text/xsl" href="configuration.xsl"?>
    4. <!--
    5. Licensed under the Apache License, Version 2.0 (the "License");
    6. you may not use this file except in compliance with the License.
    7. You may obtain a copy of the License at
    8. http://www.apache.org/licenses/LICENSE-2.0
    9. Unless required by applicable law or agreed to in writing, software
    10. distributed under the License is distributed on an "AS IS" BASIS,
    11. WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
    12. See the License for the specific language governing permissions and
    13. limitations under the License. See accompanying LICENSE file.
    14. -->
    15. <!-- Put site-specific property overrides in this file. -->
    16. <configuration>
    17. <!-- 设置MR程序默认运行模式: yarn集群模式 local本地模式 -->
    18. <property>
    19. <name>mapreduce.framework.name</name>
    20. <value>yarn</value>
    21. </property>
    22. <!-- MR程序历史服务地址 -->
    23. <property>
    24. <name>mapreduce.jobhistory.address</name>
    25. <value>172.16.10.67:10020</value>
    26. </property>
    27. <!-- MR程序历史服务器web端地址 -->
    28. <property>
    29. <name>mapreduce.jobhistory.webapp.address</name>
    30. <value>172.16.10.67:19888</value>
    31. </property>
    32. <property>
    33. <name>yarn.app.mapreduce.am.env</name>
    34. <value>HADOOP_MAPRED_HOME=${HADOOP_HOME}</value>
    35. </property>
    36. <property>
    37. <name>mapreduce.map.env</name>
    38. <value>HADOOP_MAPRED_HOME=${HADOOP_HOME}</value>
    39. </property>
    40. <property>
    41. <name>mapreduce.reduce.env</name>
    42. <value>HADOOP_MAPRED_HOME=${HADOOP_HOME}</value>
    43. </property>
    44. <property>
    45. <name>mapreduce.application.classpath</name>
    46. <value>$HADOOP_HOME/share/hadoop/mapreduce/*:$HADOOP_HOME/share/hadoop/mapreduce/lib-examples/*</value>
    47. </property>
    48. </configuration>

    workers:

    1. [root@172 hadoop]# cat workers
    2. 172.16.10.67
    3. 172.16.10.68
    4. 172.16.10.69

    八、启动集群

    1.

    解决:



      yarn.app.mapreduce.am.env
      HADOOP_MAPRED_HOME=${HADOOP_HOME}


      mapreduce.map.env
      HADOOP_MAPRED_HOME=${HADOOP_HOME}


      mapreduce.reduce.env
      HADOOP_MAPRED_HOME=${HADOOP_HOME}

     

    2.INFO conf.Configuration: resource-types.xml not found
    INFO resource.ResourceUtils: Unable to find 'resource-types.xml'.
    解决:

     
            mapreduce.application.classpath
            $HADOOP_HOME/share/hadoop/mapreduce/*:$HADOOP_HOME/share/hadoop/mapreduce/lib/*
       

     

    注意:格式化NameNode,会产生新的集群id,导致NameNode和DataNode的集群id不一致,集群找不到已往数据。所以,格式NameNode时,一定要先删除data数据和log日志,然后再格式化NameNode。

    注:如果集群是第一次启动,需要格式化NameNode(注意格式化之前,一定要先停止上次启动的所有namenode和datanode进程,然后再删除data和log数据)

    (1)如果第一次启动集群需格式化NameNode  三个节点都操作

    [root@172 hadoop]# hdfs namenode -format

    (2)启动Namenode

    1. [root@172 hadoop]# start-all.sh 启动所有
    2. [root@172 hadoop]# jcmd | grep hadoo
    3. 23942 org.apache.hadoop.hdfs.server.namenode.NameNode
    4. 24726 org.apache.hadoop.yarn.server.nodemanager.NodeManager
    5. 24087 org.apache.hadoop.hdfs.server.datanode.DataNode
    6. 24587 org.apache.hadoop.yarn.server.resourcemanager.ResourceManager
    7. 24335 org.apache.hadoop.hdfs.server.namenode.SecondaryNameNode
    8. [root@172 hadoop]# jps
    9. 23942 NameNode
    10. 24726 NodeManager
    11. 24087 DataNode
    12. 24587 ResourceManager
    13. 17643 Jps
    14. 24335 SecondaryNameNode
    15. 指定启动Namenode;注:一个集群只有一个Namenode
    16. [root@172 hadoop]# hdfs --daemon start namenode
    17. 其他节点执行:
    18. 启动Datanode和secondarynamenode
    19. [root@173 hadoop]# hdfs --daemon start datanode
    20. [root@173 hadoop]# hdfs --daemon start secondarynamenode

    HDFS集群: http://172.16.10.67:9870/

     YARN集群:http://172.16.10.67:8088/

     

     九:文件传输测试

    HDFS界面:http://172.16.10.67:9870/

    查看NameNode状态;
    该端口的定义位于core-site.xml中,可以在hdfs-site.xml 中修改;
    如果通过该端口看着这个页面,NameNode节点是存活的

    ⒈ 上传文件到集群

    构建一个测试文本和HDFS存储目录

    1. 创建目录:
    2. [root@172 hadoop]# hadoop fs -mkdir /jettech
    3. [root@172 work]# cat input.txt
    4. I love runoob
    5. I like runoob
    6. I love hadoop
    7. I like hadoop
    8. 上传:
    9. [root@172 hadoop]# hadoop fs -put input.txt /jettech
    10. 查看:
    11. [root@172 work]# hadoop fs -ls /jettech
    12. Found 5 items
    13. -rw-r--r-- 3 root supergroup 15217 2022-09-02 16:59 /jettech/LICENSE.txt
    14. -rw-r--r-- 3 root supergroup 56 2022-09-02 17:00 /jettech/input.txt
    15. drwxr-xr-x - root supergroup 0 2022-09-02 17:05 /jettech/output
    16. drwxr-xr-x - root supergroup 0 2022-09-02 17:07 /jettech/output1
    17. drwxr-xr-x - root supergroup 0 2022-09-02 17:21 /jettech/output2
    18. [root@172 work]# hadoop fs -cat /jettech/input.txt
    19. I love runoob
    20. I like runoob
    21. I love hadoop
    22. I like hadoop

    如果不想要可以用rm删除,hdfs dfs命令之后附加的命令和Linux下常用命令基本相同。

     

    Size为当前大小,Block Size为一个存储块最大的存储容量为128M。

    可从网站上进行下载,如果上传的文件容量大于规定的128M,将会把此文件分成两个块进行存储。同时,也可找到该文件存储目录下对应的blk_id,自行按顺序进行拼接后,可还原成原始文件。

     

    1. [root@172 work]# ls /root/hadoop/server/hadoop/dfs/data/current/BP-512546310-172.16.10.67-1662109116057/current/finalized/subdir0/subdir0/
    2. blk_1073741825 blk_1073741839_1015.meta blk_1073741851 blk_1073741862_1038.meta blk_1073741874 blk_1073741885_1061.meta
    3. blk_1073741825_1001.meta blk_1073741840 blk_1073741851_1027.meta blk_1073741863 blk_1073741874_1050.meta blk_1073741886
    4. blk_1073741826 blk_1073741840_1016.meta blk_1073741852 blk_1073741863_1039.meta blk_1073741875 blk_1073741886_1062.meta
    5. blk_1073741826_1002.meta blk_1073741841 blk_1073741852_1028.meta blk_1073741870 blk_1073741875_1051.meta blk_1073741887
    6. blk_1073741836 blk_1073741841_1017.meta blk_1073741859 blk_1073741870_1046.meta blk_1073741882 blk_1073741887_1063.meta
    7. blk_1073741836_1012.meta blk_1073741848 blk_1073741859_1035.meta blk_1073741871 blk_1073741882_1058.meta blk_1073741888
    8. blk_1073741837 blk_1073741848_1024.meta blk_1073741860 blk_1073741871_1047.meta blk_1073741883 blk_1073741888_1064.meta
    9. blk_1073741837_1013.meta blk_1073741849 blk_1073741860_1036.meta blk_1073741872 blk_1073741883_1059.meta
    10. blk_1073741838 blk_1073741849_1025.meta blk_1073741861 blk_1073741872_1048.meta blk_1073741884
    11. blk_1073741838_1014.meta blk_1073741850 blk_1073741861_1037.meta blk_1073741873 blk_1073741884_1060.meta
    12. blk_1073741839 blk_1073741850_1026.meta blk_1073741862 blk_1073741873_1049.meta blk_1073741885

    创建tmp.file用来存放文件内容,开始拼接

    1. # cat blk_1073741836>>tmp.file
    2. # cat blk_1073741837>>tmp.file

    案例:集群模式下,需要先上传到集群中

    统计案例

    1. [root@172 work]# yarn jar /root/hadoop/server/hadoop/share/hadoop/mapreduce/hadoop-mapreduce-examples-3.3.4.jar wordcount /jettech/input.txt /jettech/output4
    2. 2022-09-05 11:27:12,265 INFO client.DefaultNoHARMFailoverProxyProvider: Connecting to ResourceManager at /172.16.10.67:8032
    3. 2022-09-05 11:27:12,846 INFO mapreduce.JobResourceUploader: Disabling Erasure Coding for path: /tmp/hadoop-yarn/staging/root/.staging/job_1662112035948_0001
    4. 2022-09-05 11:27:13,571 INFO input.FileInputFormat: Total input files to process : 1
    5. 2022-09-05 11:27:14,477 INFO mapreduce.JobSubmitter: number of splits:1
    6. 2022-09-05 11:27:14,635 INFO mapreduce.JobSubmitter: Submitting tokens for job: job_1662112035948_0001
    7. 2022-09-05 11:27:14,635 INFO mapreduce.JobSubmitter: Executing with tokens: []
    8. 2022-09-05 11:27:14,817 INFO conf.Configuration: resource-types.xml not found
    9. 2022-09-05 11:27:14,818 INFO resource.ResourceUtils: Unable to find 'resource-types.xml'.
    10. 2022-09-05 11:27:15,265 INFO impl.YarnClientImpl: Submitted application application_1662112035948_0001
    11. 2022-09-05 11:27:15,305 INFO mapreduce.Job: The url to track the job: http://172.16.10.67:8088/proxy/application_1662112035948_0001/
    12. 2022-09-05 11:27:15,306 INFO mapreduce.Job: Running job: job_1662112035948_0001
    13. 2022-09-05 11:27:23,514 INFO mapreduce.Job: Job job_1662112035948_0001 running in uber mode : false
    14. 2022-09-05 11:27:23,517 INFO mapreduce.Job: map 0% reduce 0%
    15. 2022-09-05 11:27:28,659 INFO mapreduce.Job: map 100% reduce 0%
    16. 2022-09-05 11:27:32,710 INFO mapreduce.Job: map 100% reduce 100%
    17. 2022-09-05 11:27:33,737 INFO mapreduce.Job: Job job_1662112035948_0001 completed successfully
    18. 2022-09-05 11:27:33,875 INFO mapreduce.Job: Counters: 54
    19. File System Counters
    20. FILE: Number of bytes read=62
    21. FILE: Number of bytes written=553631
    22. FILE: Number of read operations=0
    23. FILE: Number of large read operations=0
    24. FILE: Number of write operations=0
    25. HDFS: Number of bytes read=163
    26. HDFS: Number of bytes written=36
    27. HDFS: Number of read operations=8
    28. HDFS: Number of large read operations=0
    29. HDFS: Number of write operations=2
    30. HDFS: Number of bytes read erasure-coded=0
    31. Job Counters
    32. Launched map tasks=1
    33. Launched reduce tasks=1
    34. Rack-local map tasks=1
    35. Total time spent by all maps in occupied slots (ms)=2599
    36. Total time spent by all reduces in occupied slots (ms)=2680
    37. Total time spent by all map tasks (ms)=2599
    38. Total time spent by all reduce tasks (ms)=2680
    39. Total vcore-milliseconds taken by all map tasks=2599
    40. Total vcore-milliseconds taken by all reduce tasks=2680
    41. Total megabyte-milliseconds taken by all map tasks=5322752
    42. Total megabyte-milliseconds taken by all reduce tasks=5488640
    43. Map-Reduce Framework
    44. Map input records=4
    45. Map output records=12
    46. Map output bytes=104
    47. Map output materialized bytes=62
    48. Input split bytes=107
    49. Combine input records=12
    50. Combine output records=5
    51. Reduce input groups=5
    52. Reduce shuffle bytes=62
    53. Reduce input records=5
    54. Reduce output records=5
    55. Spilled Records=10
    56. Shuffled Maps =1
    57. Failed Shuffles=0
    58. Merged Map outputs=1
    59. GC time elapsed (ms)=135
    60. CPU time spent (ms)=1340
    61. Physical memory (bytes) snapshot=576929792
    62. Virtual memory (bytes) snapshot=5615321088
    63. Total committed heap usage (bytes)=486014976
    64. Peak Map Physical memory (bytes)=345632768
    65. Peak Map Virtual memory (bytes)=2804011008
    66. Peak Reduce Physical memory (bytes)=231297024
    67. Peak Reduce Virtual memory (bytes)=2811310080
    68. Shuffle Errors
    69. BAD_ID=0
    70. CONNECTION=0
    71. IO_ERROR=0
    72. WRONG_LENGTH=0
    73. WRONG_MAP=0
    74. WRONG_REDUCE=0
    75. File Input Format Counters
    76. Bytes Read=56
    77. File Output Format Counters
    78. Bytes Written=36

    查看执行结果 

    1. [root@172 work]# hadoop fs -cat /jettech/output4/part-r-00000
    2. I 4
    3. hadoop 2
    4. like 2
    5. love 2
    6. runoob 2

    在学习了 MapReduce 的使用之后,我们已经可以处理 Word Count 这类统计和检索任务,但是客观上 MapReduce 可以做的事情还有很多。

    MapReduce 主要是依靠开发者通过编程来实现功能的,开发者可以通过实现 Map 和 Reduce 相关的方法来进行数据处理。

    为了简单的展示这一过程,我们将手工编写一个 Word Count 程序。

    注意:MapReduce 依赖 Hadoop 的库,但由于本教程使用的 Hadoop 运行环境是 Docker 容器,难以部署开发环境,所以真实的开发工作(包含调试)将需要一个运行 Hadoop 的计算机。在这里我们仅学习已完成程序的部署。

    1. root@172 work]# mkdir -p com/runoob/hadoop/
    2. root@172 work]# cat com/runoob/hadoop/
    3. Map.class MyWordCount.class MyWordCount.java Reduce.class
    4. [root@172 work]# cat com/runoob/hadoop/MyWordCount.java
    5. /**
    6. * 引用声明
    7. * 本程序引用自 http://hadoop.apache.org/docs/r1.0.4/cn/mapred_tutorial.html
    8. */
    9. package com.runoob.hadoop;
    10. import java.io.IOException;
    11. import java.util.*;
    12. import org.apache.hadoop.fs.Path;
    13. import org.apache.hadoop.io.*;
    14. import org.apache.hadoop.mapred.*;
    15. /**
    16. * 与 `Map` 相关的方法
    17. */
    18. class Map extends MapReduceBase implements Mapper<LongWritable, Text, Text, IntWritable> {
    19. private final static IntWritable one = new IntWritable(1);
    20. private Text word = new Text();
    21. public void map(LongWritable key,
    22. Text value,
    23. OutputCollector<Text, IntWritable> output,
    24. Reporter reporter)
    25. throws IOException {
    26. String line = value.toString();
    27. StringTokenizer tokenizer = new StringTokenizer(line);
    28. while (tokenizer.hasMoreTokens()) {
    29. word.set(tokenizer.nextToken());
    30. output.collect(word, one);
    31. }
    32. }
    33. }
    34. /**
    35. * 与 `Reduce` 相关的方法
    36. */
    37. class Reduce extends MapReduceBase implements Reducer<Text, IntWritable, Text, IntWritable> {
    38. public void reduce(Text key,
    39. Iterator<IntWritable> values,
    40. OutputCollector<Text, IntWritable> output,
    41. Reporter reporter)
    42. throws IOException {
    43. int sum = 0;
    44. while (values.hasNext()) {
    45. sum += values.next().get();
    46. }
    47. output.collect(key, new IntWritable(sum));
    48. }
    49. }
    50. public class MyWordCount {
    51. public static void main(String[] args) throws Exception {
    52. JobConf conf = new JobConf(MyWordCount.class);
    53. conf.setJobName("my_word_count");
    54. conf.setOutputKeyClass(Text.class);
    55. conf.setOutputValueClass(IntWritable.class);
    56. conf.setMapperClass(Map.class);
    57. conf.setCombinerClass(Reduce.class);
    58. conf.setReducerClass(Reduce.class);
    59. conf.setInputFormat(TextInputFormat.class);
    60. conf.setOutputFormat(TextOutputFormat.class);
    61. // 第一个参数表示输入
    62. FileInputFormat.setInputPaths(conf, new Path(args[0]));
    63. // 第二个输入参数表示输出
    64. FileOutputFormat.setOutputPath(conf, new Path(args[1]));
    65. JobClient.runJob(conf);
    66. }
    67. }

    编译: 

    [root@172 work]# javac -classpath /root/hadoop/server/hadoop/share/hadoop/mapreduce/hadoop-mapreduce-client-core-3.3.4.jar -classpath /root/hadoop/server/hadoop/share/hadoop/client/hadoop-client-api-3.3.4.jar com/runoob/hadoop/MyWordCount.java

    打包:

    [root@172 work]# jar -cf wubo-word-count.jar com

    执行:

    [root@172 work]# hadoop jar wubo-word-count.jar com.runoob.hadoop.MyWordCount /jettech/input.txt /jettech/output5

  • 相关阅读:
    计算机网络总结笔记
    【PMP学习笔记】第4章 项目整合管理
    YB4556系列 28V 高输入耐压4056 可支持4.2V 4.3V 电池 并且具有多种封装方式 可应用于电子烟 充电器 等各类产品
    前端如何把HTML转成图片再下载
    java字符串的学习总结
    数据结构入门(C语言版)图的概念和功能函数实现
    一种加权变异的粒子群算法-附代码
    C语言 利用选择排序法对数组中10个整数由小到大排序
    中国机器人发展的机遇(赵杰)
    LibTorch实战二:MNIST的libtorch代码
  • 原文地址:https://blog.csdn.net/Michaelwubo/article/details/126700348