使用Hadoop以及Eclipse平台,创建Hadoop项目——编写简单MapReduce程序,运行MapReduce词频统计程序,查看词频统计程序的结果。

该博客介绍了如何在Eclipse环境中创建一个Hadoop项目,添加所需的JAR包,编写并理解WordCount Java应用程序。程序通过MapReduce实现词频统计,最后打包并运行在Hadoop平台上。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

打开eclipse平台

在这里插入图片描述

在eclipse中创建项目

在这里插入图片描述
在这里插入图片描述
点击finish。

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

在这里插入图片描述
在这里插入图片描述
(1)“/opt/module/hadoop-3.2.2/share/hadoop/common/”目录下的hadoop-common-3.2.2.jar和haoop-nfs-3.2.2.jar;
(2)“ /opt/module/hadoop-3.2.2/share/hadoop/common/lib”目录下的所有JAR包;
(3)“/opt/module/hadoop-3.2.2/share/hadoop/mapreduce”目录下的所有JAR包,但是,不包括jdiff、lib、lib-examples和sources目录
(4)“/opt/module/hadoop-3.2.2/share/hadoop/mapreduce/lib”目录下的所有JAR包。
下面演示(1)的添加:
在这里插入图片描述
然后点击界面右下角的“确定”按钮,就可以把这两个JAR包增加到当前Java工程中依次添加即可。
最后添加如下,点击OK。
在这里插入图片描述

编写Java应用程序在这里插入图片描述

选择Class。
在这里插入图片描述

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 {
     //Loading hadoop Configuration
    	   Configuration conf = new Configuration();
        String[] otherArgs = (new GenericOptionsParser(conf, args)).getRemainingArgs();
        //Validates command line input parameters
        if(otherArgs.length < 2) {
            System.err.println("Usage: wordcount <in> [<in>...] <out>");
            System.exit(2);
        }
        //Create a Job instance Job and name it "word count"
        Job job = Job.getInstance(conf, "word count");
        //set jar
        job.setJarByClass(WordCount.class);
        //set mapper
        job.setMapperClass(WordCount.TokenizerMapper.class);
        //set combiner
        job.setCombinerClass(WordCount.IntSumReducer.class);
        //set reduce
        job.setReducerClass(WordCount.IntSumReducer.class);
        //set outputkey
        job.setOutputKeyClass(Text.class);
        //set outputvalue
        job.setOutputValueClass(IntWritable.class); 
        
        //add input Path
        for(int i = 0; i < otherArgs.length - 1; ++i) {
            FileInputFormat.addInputPath(job, new Path(otherArgs[i]));
        }
        	//add output Path
        FileOutputFormat.setOutputPath(job, new Path(otherArgs[otherArgs.length - 1]));
        //Wait for the job to complete and exit
        System.exit(job.waitForCompletion(true)?0:1);
    }
     //TokenizerMapper as the Map phase, you need to inherit Mapper and rewrite the Map () function
     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 {
         //Use StringTokenizer as a tokenizer to split a value
        	StringTokenizer itr = new StringTokenizer(value.toString());
        	// end after traversing the participle
            while(itr.hasMoreTokens()) {
            	//Set String to Text word
                this.word.set(itr.nextToken());
                //(Word,1), that is, (Text,IntWritable), is written to the context 
                //for use in the subsequent Reduce phase
                context.write(this.word, one);
            }
        }
    }
     //IntSumReducer as the Reduce stage, need to inherit Reducer and rewrite Reduce () functions
     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;
            //Each val in values in the output result of map phase is iterated, and the sum is accumulated
            IntWritable val;
            for(Iterator i$ = values.iterator(); i$.hasNext(); sum += val.get()) {
                val = (IntWritable)i$.next();
            }
            //Set sum to IntWritable result
            this.result.set(sum);
            //Output the result (key, result) via the write() method of the context, i.e. (Text,IntWritable)
            context.write(key, this.result);
        }
    }
}

打包编译程序

在这里插入图片描述
得到如下结果:
在这里插入图片描述
下面就可以把Java应用程序打包生成JAR包,部署到Hadoop平台上运行。现在可以把词频统计程序放在“/usr/local/hadoop/myapp”目录下。
在这里插入图片描述
在这里插入图片描述
点击next
在这里插入图片描述

运行程序

查看原来的HDFS系统文件:
运行程序
在这里插入图片描述
再查看HDFS系统文件:
在这里插入图片描述
发现了myout文件,词频统计功能实现。
在这里插入图片描述

对程序的理解

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

榆钱不知秋

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值