需要的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 = new Configuration();String[] otherArgs = (new GenericOptionsParser(conf, args)).getRemainingArgs();if(otherArgs.length < 2) {System.err.println("Usage: wordcount <in> [<in>...] <out>");System.exit(2);}Job job = 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 = 0; i < otherArgs.length - 1; ++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 = new IntWritable(1);private Text word = new Text();public TokenizerMapper() {}public void map(Object key, Text value, Mapper<Object, Text, Text, IntWritable>.Context context) throws IOException, InterruptedException {StringTokenizer itr = 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 = new IntWritable();public IntSumReducer() {}public void reduce(Text key, Iterable<IntWritable> values, Reducer<Text, IntWritable, Text, IntWritable>.Context context) throws IOException, InterruptedException {int sum = 0;IntWritable val;for(Iterator i$ = values.iterator(); i$.hasNext(); sum += val.get()) {val = (IntWritable)i$.next();}this.result.set(sum);context.write(key, this.result);}}
}

5. 编译打包程序

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

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

cd /usr/local/hadoop
mkdir myapp

将工程右击Export导出jar包:

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

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

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

5. 运行程序

在运行程序之前,需要启动Hadoop,命令如下:

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

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

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

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

./bin/hdfs dfs -mkdir input

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

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

现在,就可以在Linux系统中,使用hadoop jar命令运行程序,命令如下:

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

上面命令执行以后,当运行顺利结束时,屏幕上会显示类似如下的信息:

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

./bin/hdfs dfs -cat output/*

上面命令执行后,会在屏幕上显示如下词频统计结果:

hadoop@cumin:/usr/local/hadoop$ ./bin/hdfs dfs -cat output/*
2021-05-24 21:17:42,733 INFO sasl.SaslDataTransferClient: SASL encryption trust check: localHostTrusted = false, remoteHostTrusted = 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'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'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'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

MapReduce词频统计编程相关推荐

  1. MapReduce词频统计

    1.1 文件准备 创建本地目录和创建两个文本文件,在两个文件中输入单词,用于统计词频. cd /usr/local/hadoop mkdir WordFile cd WordFile touch wo ...

  2. 大数据技术——MapReduce词频统计

    注:参考林子雨老师教程,具体请见 MapReduce编程实践(Hadoop3.1.3)_厦大数据库实验室博客 一.实验目的 1.理解Hadoop中MapReduce模块的处理逻辑。 2.熟悉MapRe ...

  3. Mapreduce入门--词频统计

    前言 本篇博客内容:使用Hadoop提供给Java的依赖和接口轻松实现Mapreduce词频统计程序的入门. 工具:IDEA 需求:统计<yxp>这首诗中每个单词和符号出现的次数 诗的内容 ...

  4. MapRecuce 词频统计案例

    文章目录 初探MapReduce 一.MapReduce核心思想 二.MapReduce编程实例-词频统计思路 1.map阶段(映射) 2.reduce阶段(归并阶段) 三.词频统计编程实现 1.准备 ...

  5. MapReduce编程 -词频统计

    词频统计 首先,MapReduce通过默认的组件TextInputFormat将待处理的数据文件(text1.txt和text2.txt),把每一行的数据都转变为<key,value>键值 ...

  6. MapReduce实现改进版WordCount词频统计

    新手入门MapReduce实现改进版WordCount词频统计 一.实验任务要求 本实验是为了实现改进版的词频统计WordCount.要求根据所给的英文名著数据集和停用词表,统计英文名著数据集中词频, ...

  7. hadoop使用mapreduce统计词频_hadoop利用mapreduce运行词频统计(非例程)

    1.运行环境 1.Ubuntu16.04单系统 2.hadoop-3.2.1 2.操作步骤 1.使用eclipse编写map reduce run 函数 2.导出jar包 3.将需要进行词频统计的文件 ...

  8. MapReduce实现词频统计

    问题描述:现在有n个文本文件,使用MapReduce的方法实现词频统计. 附上统计词频的关键代码,首先是一个通用的MapReduce模块: class MapReduce:__doc__ = '''提 ...

  9. 调用MapReduce进行词频统计

    一.需求描述 Hadoop综合大作业 要求: 1.将待分析的文件(不少于10000英文单词)上传到HDFS. 2.调用MapReduce对文件中各个单词出现的次数进行统计. 3.将统计结果下载本地. ...

最新文章

  1. copy 修改时间_DAY5-step3 Python用shutil.copy(), shutil.copystat()复制文件
  2. idea控制台怎么调出来_酸汤饺子最近火了,可是酸汤是怎么调出来的?引起了网友的好奇...
  3. LeetCode 426. 将二叉搜索树转化为排序的双向链表
  4. LENOVO 充到60%就会停止充电
  5. 【转】Kotlin 新版来了,支持跨平台!
  6. ASP.NET生成静态页面方法大全(1)
  7. JAVA移慎_java里面给对象赋值,慎用赋值符号(=) (转)
  8. 拉丁超立方试验设计_南水北调工程通水 六年惠及超1.2亿人
  9. 利用java多线程技术和图像显示技术来完成动画设计。
  10. rabbitmq的发布订阅
  11. Swift开发:使用SwiftyJSON解析JSON数据
  12. 万年历插件软件测试,万年历的程序代码
  13. Red5流媒体服务器的搭建
  14. 怎样用计算机画太极,用IF函数画个太极图
  15. EUI多图片轮播滑动效果
  16. Java使用poi将list<Map>导出为表格
  17. 基于PHP+MySQL托管中心管理系统的设计与实现#计算机毕设
  18. Unity 3D游戏开发 - U3D入门 | 3D 模型重用之预制体
  19. 怎么实时查看mysql当前连接数呢
  20. ElasticSearch(2)

热门文章

  1. SQL 用户管理和授权
  2. 从华南地区实践看房地产信息化新趋势
  3. 银行表格票据识别SDK
  4. 3. 谷歌插件--Talend API Tester
  5. IntelliJ IDEA 的 Metamodel 配置
  6. C++多态全方面详解。
  7. 解决烘焙贴图错误的若干方法
  8. 前端必备的 Terminal 终端进阶技巧
  9. 前端基础JS——input输入框的oninput事件和onchange事件
  10. DCL——数据控制语言