运行Hadoop自带的wordcount单词统计程序

简介:

1.使用示例程序实现单词统计


(1)wordcount程序

    wordcount程序在hadoop的share目录下,如下:

1
2
3
4
5
6
7
8
9
[root@leaf mapreduce] # pwd
/usr/local/hadoop/share/hadoop/mapreduce
[root@leaf mapreduce] # ls
hadoop-mapreduce-client-app-2.6.5.jar         hadoop-mapreduce-client-jobclient-2.6.5-tests.jar
hadoop-mapreduce-client-common-2.6.5.jar      hadoop-mapreduce-client-shuffle-2.6.5.jar
hadoop-mapreduce-client-core-2.6.5.jar        hadoop-mapreduce-examples-2.6.5.jar
hadoop-mapreduce-client-hs-2.6.5.jar          lib
hadoop-mapreduce-client-hs-plugins-2.6.5.jar  lib-examples
hadoop-mapreduce-client-jobclient-2.6.5.jar   sources

    就是这个hadoop-mapreduce-examples-2.6.5.jar程序。

 

(2)创建HDFS数据目录

    创建一个目录,用于保存MapReduce任务的输入文件:

1
[root@leaf ~] # hadoop fs -mkdir -p /data/wordcount

    创建一个目录,用于保存MapReduce任务的输出文件:

1
[root@leaf ~] # hadoop fs -mkdir /output

    查看刚刚创建的两个目录:

1
2
3
[root@leaf ~] # hadoop fs -ls /
drwxr-xr-x   - root supergroup          0 2017-09-01 20:34  /data
drwxr-xr-x   - root supergroup          0 2017-09-01 20:35  /output


(3)创建一个单词文件,并上传到HDFS

    创建的单词文件如下:

1
2
3
4
5
6
[root@leaf ~] # cat myword.txt 
leaf yyh
yyh xpleaf
katy ling
yeyonghao leaf
xpleaf katy

    上传该文件到HDFS中:

1
[root@leaf ~] # hadoop fs -put myword.txt /data/wordcount

    在HDFS中查看刚刚上传的文件及内容:

1
2
3
4
5
6
7
8
[root@leaf ~] # hadoop fs -ls /data/wordcount
-rw-r--r--   1 root supergroup         57 2017-09-01 20:40  /data/wordcount/myword .txt
[root@leaf ~] # hadoop fs -cat /data/wordcount/myword.txt
leaf yyh
yyh xpleaf
katy ling
yeyonghao leaf
xpleaf katy


(4)运行wordcount程序

    执行如下命令:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
[root@leaf ~] # hadoop jar /usr/local/hadoop/share/hadoop/mapreduce/hadoop-mapreduce-examples-2.6.5.jar wordcount /data/wordcount /output/wordcount
...
17 /09/01  20:48:14 INFO mapreduce.Job: Job job_local1719603087_0001 completed successfully
17 /09/01  20:48:14 INFO mapreduce.Job: Counters: 38
         File System Counters
                 FILE: Number of bytes  read =585940
                 FILE: Number of bytes written=1099502
                 FILE: Number of  read  operations=0
                 FILE: Number of large  read  operations=0
                 FILE: Number of write operations=0
                 HDFS: Number of bytes  read =114
                 HDFS: Number of bytes written=48
                 HDFS: Number of  read  operations=15
                 HDFS: Number of large  read  operations=0
                 HDFS: Number of write operations=4
         Map-Reduce Framework
                 Map input records=5
                 Map output records=10
                 Map output bytes=97
                 Map output materialized bytes=78
                 Input  split  bytes=112
                 Combine input records=10
                 Combine output records=6
                 Reduce input  groups =6
                 Reduce shuffle bytes=78
                 Reduce input records=6
                 Reduce output records=6
                 Spilled Records=12
                 Shuffled Maps =1
                 Failed Shuffles=0
                 Merged Map outputs=1
                 GC  time  elapsed (ms)=92
                 CPU  time  spent (ms)=0
                 Physical memory (bytes) snapshot=0
                 Virtual memory (bytes) snapshot=0
                 Total committed heap usage (bytes)=241049600
         Shuffle Errors
                 BAD_ID=0
                 CONNECTION=0
                 IO_ERROR=0
                 WRONG_LENGTH=0
                 WRONG_MAP=0
                 WRONG_REDUCE=0
         File Input Format Counters 
                 Bytes Read=57
         File Output Format Counters 
                 Bytes Written=48

    

(5)查看统计结果

    如下:

1
2
3
4
5
6
7
[root@leaf ~] # hadoop fs -cat /output/wordcount/part-r-00000
katy    2
leaf    2
ling    1
xpleaf  2
yeyonghao       1
yyh     2




本文转自 xpleaf 51CTO博客,原文链接:http://blog.51cto.com/xpleaf/1962271,如需转载请自行联系原作者

相关文章
|
30天前
|
存储 分布式计算 算法
探索Hadoop的三种运行模式:单机模式、伪分布式模式和完全分布式模式
在配置Hadoop集群之前,了解这三种模式的特点、适用场景和配置差异是非常重要的。这有助于用户根据个人需求和资源情况,选择最适合自己的Hadoop运行模式。在最初的学习和开发阶段,单机模式和伪分布式模式能为用户提供便利和成本效益。进而,当用户要处理大规模数据集时,完全分布式模式将是理想的选择。
70 2
|
28天前
|
存储 分布式计算 Hadoop
Hadoop 运行的三种模式
【8月更文挑战第31天】
83 0
|
1月前
|
分布式计算 Hadoop Java
Hadoop_MapReduce中的WordCount运行详解
MapReduce的WordCount程序在分布式系统中计算大数据集中单词出现的频率时,提供了一个可以复用和可伸缩的解决方案。它体现了MapReduce编程模型的强大之处:简单、可靠且将任务自动分布到一个集群中去执行。它首先运行一系列的Map任务来处理原始数据,然后通过Shuffle和Sort机制来组织结果,最后通过运行Reduce任务来完成最终计算。因此,即便数据量非常大,通过该模型也可以高效地进行处理。
48 1
|
2月前
|
分布式计算 资源调度 监控
Hadoop中运行Job
【7月更文挑战第10天】
39 2
|
3月前
|
存储 缓存 分布式计算
|
1月前
|
存储 分布式计算 Hadoop
|
27天前
|
图形学 数据可视化 开发者
超实用Unity Shader Graph教程:从零开始打造令人惊叹的游戏视觉特效,让你的作品瞬间高大上,附带示例代码与详细步骤解析!
【8月更文挑战第31天】Unity Shader Graph 是 Unity 引擎中的强大工具,通过可视化编程帮助开发者轻松创建复杂且炫酷的视觉效果。本文将指导你使用 Shader Graph 实现三种效果:彩虹色渐变着色器、动态光效和水波纹效果。首先确保安装最新版 Unity 并启用 Shader Graph。创建新材质和着色器图谱后,利用节点库中的预定义节点,在编辑区连接节点定义着色器行为。
86 0
|
1月前
|
分布式计算 资源调度 Hadoop
Hadoop入门基础(五):Hadoop 常用 Shell 命令一网打尽,提升你的大数据技能!
Hadoop入门基础(五):Hadoop 常用 Shell 命令一网打尽,提升你的大数据技能!
|
1月前
|
存储 SQL 分布式计算
Hadoop生态系统概述:构建大数据处理与分析的基石
【8月更文挑战第25天】Hadoop生态系统为大数据处理和分析提供了强大的基础设施和工具集。通过不断扩展和优化其组件和功能,Hadoop将继续在大数据时代发挥重要作用。
|
1月前
|
资源调度 分布式计算 Hadoop
揭秘Hadoop Yarn背后的秘密!它是如何化身‘资源大师’,让大数据处理秒变高效大戏的?
【8月更文挑战第24天】在大数据领域,Hadoop Yarn(另一种资源协调者)作为Hadoop生态的核心组件,扮演着关键角色。Yarn通过其ResourceManager、NodeManager、ApplicationMaster及Container等组件,实现了集群资源的有效管理和作业调度。当MapReduce任务提交时,Yarn不仅高效分配所需资源,还能确保任务按序执行。无论是处理Map阶段还是Reduce阶段的数据,Yarn都能优化资源配置,保障任务流畅运行。此外,Yarn还在Spark等框架中展现出灵活性,支持不同模式下的作业执行。未来,Yarn将持续助力大数据技术的发展与创新。
30 2

相关实验场景

更多