热门标签 | HotTags
当前位置:  开发笔记 > 编程语言 > 正文

MapReduce词频统计编程

需要的jar包评论区发邮箱1.创建本地文件系统本地创建txt文件,wordfile1.txt。2.在Eclipse中创建项目选择“File–New–JavaProject

需要的jar包评论区发邮箱


1.创建本地文件

系统本地创建txt文件,wordfile1.txt。

在这里插入图片描述


2. 在Eclipse中创建项目

选择“File–>New–>Java Project”菜单,开始创建一个Java工程,命名为名称“WordCount”,并选中“Use default location”,如下图所示界面。
在这里插入图片描述


3. 为项目添加需要用到的JAR包

进入下一步的设置以后,会弹出如下图所示界面。

在这里插入图片描述

点击界面中的“Libraries”选项卡,然后,点击界面右侧的“Add External JARs…”按钮,弹出如下图所示界面。

在这里插入图片描述

需要向Java工程中添加以下JAR包:
(1)“/usr/local/hadoop/share/hadoop/common”目录下的hadoop-common-3.1.3.jar和haoop-nfs-3.1.3.jar;
(2)“/usr/local/hadoop/share/hadoop/common/lib”目录下的所有JAR包;
(3)“/usr/local/hadoop/share/hadoop/mapreduce”目录下的所有JAR包,但是,不包括jdiff、lib、lib-examples和sources目录。

(4)“/usr/local/hadoop/share/hadoop/mapreduce/lib”目录下的所有JAR包。

在这里插入图片描述


4. 编写Java应用程序

选择“New–>Class”菜单以后,新建WordCount类。

在这里插入图片描述

Eclipse自动创建了一个名为“WordCount.java”的源代码文件,并且包含了代码“public class WordCount{}”,请清空该文件里面的代码,然后在该文件中输入完整的词频统计程序代码,具体如下:

import java.io.IOException;
import java.util.Iterator;
import java.util.StringTokenizer;
import org.apache.hadoop.conf.Configuration;
import org.apache.hadoop.fs.Path;
import org.apache.hadoop.io.IntWritable;
import org.apache.hadoop.io.Text;
import org.apache.hadoop.mapreduce.Job;
import org.apache.hadoop.mapreduce.Mapper;
import org.apache.hadoop.mapreduce.Reducer;
import org.apache.hadoop.mapreduce.lib.input.FileInputFormat;
import org.apache.hadoop.mapreduce.lib.output.FileOutputFormat;
import org.apache.hadoop.util.GenericOptionsParser;
public class WordCount {public WordCount() {}public static void main(String[] args) throws Exception {Configuration conf &#61; new Configuration();String[] otherArgs &#61; (new GenericOptionsParser(conf, args)).getRemainingArgs();if(otherArgs.length < 2) {System.err.println("Usage: wordcount [...] ");System.exit(2);}Job job &#61; Job.getInstance(conf, "word count");job.setJarByClass(WordCount.class);job.setMapperClass(WordCount.TokenizerMapper.class);job.setCombinerClass(WordCount.IntSumReducer.class);job.setReducerClass(WordCount.IntSumReducer.class);job.setOutputKeyClass(Text.class);job.setOutputValueClass(IntWritable.class); for(int i &#61; 0; i < otherArgs.length - 1; &#43;&#43;i) {FileInputFormat.addInputPath(job, new Path(otherArgs[i]));}FileOutputFormat.setOutputPath(job, new Path(otherArgs[otherArgs.length - 1]));System.exit(job.waitForCompletion(true)?0:1);}public static class TokenizerMapper extends Mapper<Object, Text, Text, IntWritable> {private static final IntWritable one &#61; new IntWritable(1);private Text word &#61; new Text();public TokenizerMapper() {}public void map(Object key, Text value, Mapper<Object, Text, Text, IntWritable>.Context context) throws IOException, InterruptedException {StringTokenizer itr &#61; new StringTokenizer(value.toString()); while(itr.hasMoreTokens()) {this.word.set(itr.nextToken());context.write(this.word, one);}}}
public static class IntSumReducer extends Reducer<Text, IntWritable, Text, IntWritable> {private IntWritable result &#61; new IntWritable();public IntSumReducer() {}public void reduce(Text key, Iterable<IntWritable> values, Reducer<Text, IntWritable, Text, IntWritable>.Context context) throws IOException, InterruptedException {int sum &#61; 0;IntWritable val;for(Iterator i$ &#61; values.iterator(); i$.hasNext(); sum &#43;&#61; val.get()) {val &#61; (IntWritable)i$.next();}this.result.set(sum);context.write(key, this.result);}}
}

5. 编译打包程序

现在就可以编译上面编写的代码。可以直接点击Eclipse工作界面上部的运行程序的快捷按钮&#xff0c;结果如下图所示。

在这里插入图片描述

下面就可以把Java应用程序打包生成JAR包&#xff0c;部署到Hadoop平台上运行。现在可以把词频统计程序放在“/usr/local/hadoop/myapp”目录下。如果该目录不存在&#xff0c;可以使用如下命令创建&#xff1a;

cd /usr/local/hadoop
mkdir myapp

将工程右击Export导出jar包&#xff1a;

在这里插入图片描述

在该界面中&#xff0c;选择“Runnable JAR file”&#xff0c;然后&#xff0c;点击“Next>”按钮&#xff0c;弹出如下图所示界面。

在这里插入图片描述

在该界面中&#xff0c;“Launch configuration”用于设置生成的JAR包被部署启动时运行的主类&#xff0c;需要在下拉列表中选择刚才配置的类“WordCount-WordCount”。在“Export destination”中需要设置JAR包要输出保存到哪个目录&#xff0c;比如&#xff0c;这里设置为“/usr/local/hadoop/myapp/WordCount.jar”。在“Library handling”下面选择“Extract required libraries into generated JAR”。然后&#xff0c;点击“Finish”按钮&#xff0c;会出现如下图所示界面。

在这里插入图片描述

直接点击界面右下角的“OK”按钮。至此&#xff0c;已经顺利把WordCount工程打包生成了WordCount.jar。


5. 运行程序

在运行程序之前&#xff0c;需要启动Hadoop&#xff0c;命令如下&#xff1a;

cd /usr/local/hadoop
./sbin/start-dfs.sh

在启动Hadoop之后&#xff0c;需要首先删除HDFS中与当前Linux用户hadoop对应的input和output目录&#xff08;即HDFS中的“/user/hadoop/input”和“/user/hadoop/output”目录&#xff09;&#xff0c;这样确保后面程序运行不会出现问题&#xff0c;具体命令如下&#xff1a;

./bin/hdfs dfs -rm -r input
./bin/hdfs dfs -rm -r output

然后&#xff0c;再在HDFS中新建与当前Linux用户hadoop对应的input目录&#xff0c;即“/user/hadoop/input”目录&#xff0c;具体命令如下&#xff1a;

./bin/hdfs dfs -mkdir input

然后&#xff0c;把Linux本地文件系统中新建的两个文件wordfile1.txt&#xff0c;上传到HDFS中“/user/hadoop/input”目录下&#xff0c;命令如下&#xff1a;

./bin/hdfs dfs -put ./wordfile1.txt input

现在&#xff0c;就可以在Linux系统中&#xff0c;使用hadoop jar命令运行程序&#xff0c;命令如下&#xff1a;

./bin/hadoop jar ./myapp/WordCount.jar input output

上面命令执行以后&#xff0c;当运行顺利结束时&#xff0c;屏幕上会显示类似如下的信息&#xff1a;

在这里插入图片描述

词频统计结果已经被写入了HDFS的“/user/hadoop/output”目录中&#xff0c;可以执行如下命令查看词频统计结果&#xff1a;

./bin/hdfs dfs -cat output/*

上面命令执行后&#xff0c;会在屏幕上显示如下词频统计结果&#xff1a;

hadoop&#64;cumin:/usr/local/hadoop$ ./bin/hdfs dfs -cat output/*
2021-05-24 21:17:42,733 INFO sasl.SaslDataTransferClient: SASL encryption trust check: localHostTrusted &#61; false, remoteHostTrusted &#61; false
000 1
5 1
Chinese 2
Commercials 1
In 1
Language 1
On 1
Secondly, 1
TVs, 1
The 1
Therefore, 1
This 1
To 1
Undoubtedly, 1
What&#39;s 1
Words 1
Yet 1
a 6
accumulate 1
accurate 1
age 1
an 1
and 5
appearing 1
arbitrary 1
argue 1
arouse 1
arouses 1
as 4
attention. 1
audience&#39;
s 1
awareness 1
ban 1
be 3
been 1
begin 1
boasting 1
boy, 1
brands. 1
by 4
can 1
cartoon 2
characters 1
children 1
commercial 2
commercial, 1
commercials 2
commercials, 1
concern 1
culture 1
culture, 1
deep 1
depicts 1
dictionary. 1
dignity 1
disrespect 1
dissimilar 1
distinguish 1
down 1
easy 1
first 1
for 3
fosters 1
from 1
general 1
grab 1
have 1
history 1
homophones 1
identifies 1
idiom 1
idiom, 1
image 1
importance 1
in 3
incorrect 1
innovative 1
integrity 2
intended 1
is 6
issue 1
it 2
its 1
itself 1
just 1
kept 1
kind 1
labels 1
language 3
last 1
magazines. 1
maintain 1
majority 1
makers 1
may 1
media 1
misled 1
misspelling 2
misspellings 1
nation&#39;s 1
nationality 1
newspapers 1
not 1
of 9
on 2
only 1
or 2
other 2
our 1
over 1
people 1
pin 1
presented 1
problem. 1
propaganda. 1
put 1
puzzled 1
quite 1
replaced 1
safeguarded. 1
scene 1
school 1
should 1
so 1
solve 1
spelling 1
spellings. 1
step 1
such 1
symbolizes 1
television 1
that 2
the 17
their 1
these 1
this 2
thought-provoking 1
to 5
trying 1
two 3
uncommon 1
unity. 1
versions 1
vocabulary 1
way 1
well 1
when 1
which 1
who 1
worse, 1
years. 1
yet 1

推荐阅读
  • Struts与Spring框架的集成指南
    本文详细介绍了如何将Struts和Spring两个流行的Java Web开发框架进行整合,涵盖从环境配置到代码实现的具体步骤。 ... [详细]
  • 使用Python在SAE上开发新浪微博应用的初步探索
    最近重新审视了新浪云平台(SAE)提供的服务,发现其已支持Python开发。本文将详细介绍如何利用Django框架构建一个简单的新浪微博应用,并分享开发过程中的关键步骤。 ... [详细]
  • 本文详细探讨了JDBC(Java数据库连接)的内部机制,重点分析其作为服务提供者接口(SPI)框架的应用。通过类图和代码示例,展示了JDBC如何注册驱动程序、建立数据库连接以及执行SQL查询的过程。 ... [详细]
  • 深入解析 Apache Shiro 安全框架架构
    本文详细介绍了 Apache Shiro,一个强大且灵活的开源安全框架。Shiro 专注于简化身份验证、授权、会话管理和加密等复杂的安全操作,使开发者能够更轻松地保护应用程序。其核心目标是提供易于使用和理解的API,同时确保高度的安全性和灵活性。 ... [详细]
  • Spring Boot 中静态资源映射详解
    本文深入探讨了 Spring Boot 如何简化 Web 应用中的静态资源管理,包括默认的静态资源映射规则、WebJars 的使用以及静态首页的处理方法。通过本文,您将了解如何高效地管理和引用静态资源。 ... [详细]
  • Logback使用小结
    1一定要使用slf4j的jar包,不要使用apachecommons的jar。否则滚动生成文件不生效,不滚动的时候却生效~~importorg.slf ... [详细]
  • Eclipse 中 JSP 开发环境配置指南
    本文详细介绍了如何在 Eclipse 集成开发环境中配置 JSP 运行环境,包括必要的软件下载、Tomcat 服务器的配置以及常见问题的解决方法。 ... [详细]
  • 本文介绍了如何在WildFly 10中配置MySQL数据源时遇到的服务依赖问题及其解决方案。 ... [详细]
  • 本文将介绍如何编写一些有趣的VBScript脚本,这些脚本可以在朋友之间进行无害的恶作剧。通过简单的代码示例,帮助您了解VBScript的基本语法和功能。 ... [详细]
  • 本文详细介绍了 Dockerfile 的编写方法及其在网络配置中的应用,涵盖基础指令、镜像构建与发布流程,并深入探讨了 Docker 的默认网络、容器互联及自定义网络的实现。 ... [详细]
  • 本文作者分享了在阿里巴巴获得实习offer的经历,包括五轮面试的详细内容和经验总结。其中四轮为技术面试,一轮为HR面试,涵盖了大量的Java技术和项目实践经验。 ... [详细]
  • 简化报表生成:EasyReport工具的全面解析
    本文详细介绍了EasyReport,一个易于使用的开源Web报表工具。该工具支持Hadoop、HBase及多种关系型数据库,能够将SQL查询结果转换为HTML表格,并提供Excel导出、图表显示和表头冻结等功能。 ... [详细]
  • 当 WebLogic 连接的数据源数据库密码发生更改时,需要在域目录的 config 文件夹下的 jdbc 配置文件中更新相应的密码。本文将详细介绍如何安全地修改和验证这些配置文件中的加密密码。 ... [详细]
  • 初探Java编程:从入门到实践
    本文旨在为初学者提供Java编程的基础知识,涵盖程序、算法、流程图的概念,以及JDK环境的配置和Eclipse的使用方法。 ... [详细]
  • 本文详细介绍了如何正确安装Java EE SDK,并解决在安装过程中可能遇到的问题,特别是关于servlet代码在Apache Tomcat 10中无法运行的情况。 ... [详细]
author-avatar
周啸夫_919
这个家伙很懒,什么也没留下!
PHP1.CN | 中国最专业的PHP中文社区 | DevBox开发工具箱 | json解析格式化 |PHP资讯 | PHP教程 | 数据库技术 | 服务器技术 | 前端开发技术 | PHP框架 | 开发工具 | 在线工具
Copyright © 1998 - 2020 PHP1.CN. All Rights Reserved | 京公网安备 11010802041100号 | 京ICP备19059560号-4 | PHP1.CN 第一PHP社区 版权所有