MapReduce WordCount程序实践(IDEA版)

环境

Linux:Hadoop2.x

Windows:jdk1.8、Maven3、IDEA2021

步骤

编程分析

编程分析包括:

1.数据过程分析:数据从输入到输出的过程分析。

2.数据类型分析:Map的输入输出类型,Reduce的输入输出类型;

编程分析决定了我们该如何编写代码。

新建Maven工程

打开IDEA-->点击File-->New-->Project

选择Maven-->点击Next

选择一个空目录作为项目目录,目录名称例如:wordcount,建议目录路径不包含中文和空格,点击Finish

添加依赖

修改pom.xml,添加如下依赖

xml 复制代码
    <dependencies>
        <dependency>
            <groupId>org.apache.hadoop</groupId>
            <artifactId>hadoop-common</artifactId>
            <version>2.7.3</version>
        </dependency>
        <dependency>
            <groupId>org.apache.hadoop</groupId>
            <artifactId>hadoop-client</artifactId>
            <version>2.7.3</version>
        </dependency>
        <dependency>
            <groupId>org.apache.hadoop</groupId>
            <artifactId>hadoop-hdfs</artifactId>
            <version>2.7.3</version>
        </dependency>
        <dependency>
            <groupId>org.apache.hadoop</groupId>
            <artifactId>hadoop-mapreduce-client-core</artifactId>
            <version>2.7.3</version>
        </dependency>
    </dependencies>

加载依赖

新建包

src\main\java目录下,新建包:org.example

填入org.example,效果如下:

新建类

org.example包下,新建出三个类,分别为:MyMapperMyReducerMyMain,效果如下:

编写Map程序

编辑MyMapper类,步骤如下:

1.继承Mapper
2.重写map()方法
3.编写Map逻辑代码:
	1.v1由Text类型转换为String
	2.按空格进行分词:split(" ")方法
	3.输出k2, v2

编写Reduce程序

编辑MyReducer类,步骤如下:

1.继承Reducer
2.重写reduce()方法
3.编写Reduce逻辑代码:
    1.k4 = k3
    2.v4 = v3元素的和
    3.输出k4, v4

编写Main程序(Driver程序)

编辑MyMain类,步骤如下:

1. 创建一个job和任务入口(指定主类)
2. 指定job的mapper和输出的类型<k2 v2>
3. 指定job的reducer和输出的类型<k4  v4>
4. 指定job的输入和输出路径
5. 执行job

思考

代码编写完成后,可以先在Windows本地运行吗?

打包

看到BUILD SUCCESS为打包成功

打包后得到的jar包,在项目的target目录下

提交到Hadoop集群运行

1.将上一步打包得到的jar包,上传到linux

2.启动hadoop集群

start-all.sh

3.运行jar包

从Linux本地上传一个文件到hdfs

hdfs dfs -put 1.txt /input/1.txt

hdfs查看输入数据

运行jar包

hadoop jar wordcount-1.0-SNAPSHOT.jar org.example.MyMain /input/1.txt /output/wordcount

正常运行过程输出如下:

[hadoop@node1 ~]$ hadoop jar wordcount-1.0-SNAPSHOT.jar org.example.MyMain /input/1.txt /output/wordcount
22/03/29 00:23:59 INFO client.RMProxy: Connecting to ResourceManager at node1/192.168.193.140:8032
22/03/29 00:23:59 WARN mapreduce.JobResourceUploader: Hadoop command-line option parsing not performed. Implement the Tool interface and execute your application with ToolRunner to remedy this.
22/03/29 00:24:00 INFO input.FileInputFormat: Total input paths to process : 1
22/03/29 00:24:00 INFO mapreduce.JobSubmitter: number of splits:1
22/03/29 00:24:01 INFO mapreduce.JobSubmitter: Submitting tokens for job: job_1648484275192_0001
22/03/29 00:24:01 INFO impl.YarnClientImpl: Submitted application application_1648484275192_0001
22/03/29 00:24:01 INFO mapreduce.Job: The url to track the job: http://node1:8088/proxy/application_1648484275192_0001/
22/03/29 00:24:01 INFO mapreduce.Job: Running job: job_1648484275192_0001
22/03/29 00:24:08 INFO mapreduce.Job: Job job_1648484275192_0001 running in uber mode : false
22/03/29 00:24:08 INFO mapreduce.Job:  map 0% reduce 0%
22/03/29 00:24:12 INFO mapreduce.Job:  map 100% reduce 0%
22/03/29 00:24:17 INFO mapreduce.Job:  map 100% reduce 100%
22/03/29 00:24:19 INFO mapreduce.Job: Job job_1648484275192_0001 completed successfully
22/03/29 00:24:19 INFO mapreduce.Job: Counters: 49
	File System Counters
		FILE: Number of bytes read=55
		FILE: Number of bytes written=237261
		FILE: Number of read operations=0
		FILE: Number of large read operations=0
		FILE: Number of write operations=0
		HDFS: Number of bytes read=119
		HDFS: Number of bytes written=25
		HDFS: Number of read operations=6
		HDFS: Number of large read operations=0
		HDFS: Number of write operations=2
	Job Counters 
		Launched map tasks=1
		Launched reduce tasks=1
		Data-local map tasks=1
		Total time spent by all maps in occupied slots (ms)=2290
		Total time spent by all reduces in occupied slots (ms)=2516
		Total time spent by all map tasks (ms)=2290
		Total time spent by all reduce tasks (ms)=2516
		Total vcore-milliseconds taken by all map tasks=2290
		Total vcore-milliseconds taken by all reduce tasks=2516
		Total megabyte-milliseconds taken by all map tasks=2344960
		Total megabyte-milliseconds taken by all reduce tasks=2576384
	Map-Reduce Framework
		Map input records=2
		Map output records=4
		Map output bytes=41
		Map output materialized bytes=55
		Input split bytes=94
		Combine input records=0
		Combine output records=0
		Reduce input groups=3
		Reduce shuffle bytes=55
		Reduce input records=4
		Reduce output records=3
		Spilled Records=8
		Shuffled Maps =1
		Failed Shuffles=0
		Merged Map outputs=1
		GC time elapsed (ms)=103
		CPU time spent (ms)=1200
		Physical memory (bytes) snapshot=425283584
		Virtual memory (bytes) snapshot=4223356928
		Total committed heap usage (bytes)=277348352
	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=25
	File Output Format Counters 
		Bytes Written=25
[hadoop@node1 ~]$ 

查看输出结果

思考

  • 如果运行过程报如下错误,该如何解决?

  • 代码还可以优化吗?如何优化?

完成!enjoy it!

相关推荐
HyperAI超神经37 分钟前
Meta 首个多模态大模型一键启动!首个多针刺绣数据集上线,含超 30k 张图片
大数据·人工智能·深度学习·机器学习·语言模型·大模型·数据集
Hello.Reader3 小时前
TopK算法在大数据重复数据分析中的应用与挑战
大数据·算法·数据分析
数据龙傲天3 小时前
1688商品API接口:电商数据自动化的新引擎
java·大数据·sql·mysql
Elastic 中国社区官方博客3 小时前
Elasticsearch:使用 LLM 实现传统搜索自动化
大数据·人工智能·elasticsearch·搜索引擎·ai·自动化·全文检索
Jason不在家5 小时前
Flink 本地 idea 调试开启 WebUI
大数据·flink·intellij-idea
菜鸟一皓5 小时前
IDEA的lombok插件不生效了?!!
java·ide·intellij-idea
Elastic 中国社区官方博客6 小时前
使用 Vertex AI Gemini 模型和 Elasticsearch Playground 快速创建 RAG 应用程序
大数据·人工智能·elasticsearch·搜索引擎·全文检索
CHICX12297 小时前
【Hadoop】改一下core-site.xml和hdfs-site.xml配置就可以访问Web UI
xml·大数据·hadoop
权^8 小时前
MySQL--聚合查询、联合查询、子查询、合并查询(上万字超详解!!!)
大数据·数据库·学习·mysql
bin915312 小时前
【EXCEL数据处理】000010 案列 EXCEL文本型和常规型转换。使用的软件是微软的Excel操作的。处理数据的目的是让数据更直观的显示出来,方便查看。
大数据·数据库·信息可视化·数据挖掘·数据分析·excel·数据可视化