运行hadoop,-hadoop上执行WordCount任务 - import org

📅 2026-11-30 👤 拼多多助力营销技巧 📂 拼多多助力平台 👁️ 237 次阅读

任务要求:

解决方案:

通过mapreduce去解决这个问题,下面看一下程序的编写,在编写我们的mapreduce程序的时候,实际上最核心的就是我们的map处理逻辑和reduce处理逻辑,因为之所以称之为mapreduce就是因为它由map函数加reduce函数构成的,所以需要先去编写我们用户自定义的map函数和用户自定义的reduce函数,先来看一下怎么去编写我们的map处理函数

Map输入类型为期望的Map输出类型为<单词,出现次数>Map输入类型最终确定为Map输出类型最终确定为

public static class MyMapper extends Mapper{

private final static IntWritable one = new IntWritable(1);

private Text word = new Text();

public void map(Object key, Text value, Context context) throws IOException,InterruptedException{

StringTokenizer itr = new StringTokenizer(value.toString());

while (itr.hasMoreTokens())

{

word.set(itr.nextToken());

context.write(word,one);

}

}

}

经过上面的代码后,会将单词转换成(word,1)的形式给shuffle去处理,然后再交由reduce去处理,那下面看reduce处理逻辑:

在Reduce处理数据之前,Map的结果首先通过Shuffle阶段进行整理Reduce阶段的任务:对输入数字序列进行求和Reduce的输入数据为

Reduce任务的输入数据:

<”I”,<1,1>>

<”is”,1>

……

<”from”,1>

<”China”,<1,1,1>>

public static class MyReducer extends Reducer{

private IntWritable result = new IntWritable();

public void reduce(Text key, Iterablevalues, Context context) throws IOException,InterruptedException{

int sum = 0;

for (IntWritable val : values)

{

sum += val.get();

}

result.set(sum);

context.write(key,result);

}

}

写完这两个函数之后,把他们封装起来,这时候需要写一个main函数:

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");

System.exit(2);

}

Jobjob=new Job(conf,"word count");//设置环境参数

job.setJarByClass(WordCount.class);//设置整个程序的类名

job.setMapperClass(MyMapper.class);//添加MyMapper类

job.setReducerClass(MyReducer.class);//添加MyReducer类

job.setOutputKeyClass(Text.class);//设置输出类型

job.setOutputValueClass(IntWritable.class);//设置输出类型

FileInputFormat.addInputPath(job,new Path(otherArgs[0]);//设置输入文件

FileOutputFormat.setOutputPath(job,new Path(otherArgs[1]));//设置输出文件

System.exit(job.waitForCompletion(true)?0:1);

}

将前面的代码全部封装在一起,完整代码

import java.io.IOException;

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 static class MyMapper extends Mapper{

private final static IntWritable one = new IntWritable(1);

private Text word = new Text();

public void map(Object key, Text value, Context context) throws IOException,InterruptedException{

StringTokenizer itr = new StringTokenizer(value.toString());

while (itr.hasMoreTokens()){

word.set(itr.nextToken());

context.write(word,one);

}

}

}

public static class MyReducer extends Reducer{

private IntWritable result = new IntWritable();

public void reduce(Text key, Iterablevalues, Context context) throws IOException,InterruptedException{

int sum = 0;

for (IntWritable val : values)

{

sum += val.get();

}

result.set(sum);

context.write(key,result);

}

}

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");

System.exit(2);

}

Job job = new Job(conf,"word count");

job.setJarByClass(WordCount.class);

job.setMapperClass(MyMapper.class);

job.setReducerClass(MyReducer.class);

job.setOutputKeyClass(Text.class);

job.setOutputValueClass(IntWritable.class);

FileInputFormat.addInputPath(job,new Path(otherArgs[0]));

FileOutputFormat.setOutputPath(job,new Path(otherArgs[1]));

System.exit(job.waitForCompletion(true)?0:1);

}

}

mapreduce程序完全屏蔽了底层分布式的细节,我们只需要去关注map和reduce处理逻辑就够了;

词频统计任务执行过程

首先,在Linux系统本地创建两个文件,即文件wordfile1.txt和wordfile2.txt。在实际应用中,这两个文件可能会非常大,会被分布存储到多个节点上。但是,为了简化任务,这里的两个文件只包含几行简单的内容。需要说明的是,针对这两个小数据集样本编写的MapReduce词频统计程序,不作任何修改,就可以用来处理大规模数据集的词频统计。

cd /usr/local/hadoop

ls

vim wordfile1.txt

文件wordfile1.txt的内容如下:

I love Spark

I love Hadoop

完成之后,执行以下命令行

vim wordfile2.txt

文件wordfile2.txt的内容如下:

Hadoop is good

Spark is fast

文件创建完成之后,启动hadoop

./sbin/start-dfs.sh

jps

启动eclipse

cd /usr/local/eclipse

./eclipse

启动以后会弹出如下图所示界面,提示设置工作空间(workspace),在此页面点击launch。

点击launch后出现以下界面

选择“File–>New–>Java Project”菜单,开始创建一个Java工程,弹出新界面,并做如图所示选择,然后点击Finish。

然后在下面这个页面点击界面中的“Libraries”选项卡,然后,点击界面右侧的“Add External JARs…”按钮。

然后先选择下图所示的路径并选择此路径下的所有jar包

然后点击OK,导入成功后,

再次点击“Add External JARs…”按钮,将“/usr/local/hadoop/share/hadoop/common/lib”目录下的所有JAR包按照上面的方式导入

再次点击“Add External JARs…”按钮,将“/usr/local/hadoop/share/hadoop/mapreduce”目录下的所有JAR包按照上面的方式导入

再次点击“Add External JARs…”按钮,将“/usr/local/hadoop/share/hadoop/mapreduce/lib”目录下的所有JAR包按照上面的方式导入

导入完成之后,点击右下角的apply and close;下面编写一个Java应用程序,即WordCount.java。请在Eclipse工作界面左侧的“Package Explorer”面板中,找到刚才创建好的工程名称“WordCount”,然后在该工程名称上点击鼠标右键,在弹出的菜单中选择“New–>Class”菜单。在出现的菜单中做以下两步操作

然后,清除WordCount.java文件的初始内容,将以下代码copy到WordCount.java文件中

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[...]");

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{

private static final IntWritable one = new IntWritable(1);

private Text word = new Text();

public TokenizerMapper() {

}

public void map(Object key, Text value, Mapper.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{

private IntWritable result = new IntWritable();

public IntSumReducer() {

}

public void reduce(Text key, Iterablevalues, Reducer.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);

}

}

}

现在就可以编译上面编写的代码。可以直接点击Eclipse工作界面上部的运行程序的快捷按钮,当把鼠标移动到该按钮上时,在弹出的菜单中选择“Run as”,继续在弹出来的菜单中选择“Java Application”,如下图所示。

然后,会弹出如下图所示界面。

程序运行结束后,会在底部的“Console”面板中显示运行结果信息(如下图所示)。(因为在程序执行过程中,我们没有提供输入和输出文件路径,所以会显示下面这样的红字,不过不用担心,我们这个程序主要用于部署在hadoop上面运行,所以跳过单机运行这一步)

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

cd /usr/local/hadoop

mkdir myapp

然后,请在Eclipse工作界面左侧的“Package Explorer”面板中,在工程名称“WordCount”上点击鼠标右键,在弹出的菜单中选择“Export”,如下图所示。

然后,会弹出如下图所示界面,在该界面中,选择“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”按钮,启动打包过程。打包过程结束后,会出现一个警告信息界面。

如下图所示,出现下面这个界面,可以忽略该界面的信息,直接点击界面右下角的“OK”按钮。

至此,已经顺利把WordCount工程打包生成了WordCount.jar。可以到Linux系统中查看一下生成的WordCount.jar文件,可以在Linux的终端中执行如下命令:

cd /usr/local/hadoop/myapp

ls

可以看到,“/usr/local/hadoop/myapp”目录下已经存在一个WordCount.jar文件。

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

cd /usr/local/hadoop

./sbin/start-dfs.sh

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

cd /usr/local/hadoop

./bin/hdfs dfs -rm -r input

./bin/hdfs dfs -rm -r output

然后,再在HDFS中新建与当前Linux用户hadoop对应的input目录,即“/user/hadoop/input”目录,不用创建output文件,程序运行完成后,会自动创建,创建input目录的具体命令如下:

cd /usr/local/hadoop

./bin/hdfs dfs -mkdir input

然后,把之前在第上面在Linux本地文件系统中新建的两个文件wordfile1.txt和wordfile2.txt(这两个文件位于“/usr/local/hadoop”目录下,并且里面包含了一些英文语句),上传到HDFS中的“/user/hadoop/input”目录下,命令如下:

cd /usr/local/hadoop

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

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

下面查看input中wordfile1.txt和wordfile2.txt这两个文件是否存在

./bin/hdfs dfs -ls /user/hadoop/input

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

cd /usr/local/hadoop

./bin/hadoop jar ./myapp/WordCount.jar input output 拼多多帮助力入口

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

cd /usr/local/hadoop

./bin/hdfs dfs -cat output/*

出现如下结果

至此,词频统计程序顺利运行结束。需要注意的是,如果要再次运行WordCount.jar,需要首先删除HDFS中的output目录,否则会报错。

标签: 拼多多助力网站平台 赚钱项目 拼多多助力网 首码项目 手赚APP
拼多多助力营销技巧