栏目分类:
子分类:
返回
名师互学网用户登录
快速导航关闭
当前搜索
当前分类
子分类
实用工具
热门搜索
名师互学网 > IT > 软件开发 > 后端开发 > Java

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

Java 更新时间: 发布时间: IT归档 最新发布 模块sitemap 名妆网 法律咨询 聚返吧 英语巴士网 伯小乐 网商动力

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

打开eclipse平台

在eclipse中创建项目



点击finish。

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



(1)“/opt/module/hadoop-3.2.2/share/hadoop/common/”目录下的hadoop-common-3.1.3.jar和haoop-nfs-3.1.3.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  [...] ");
            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 {
        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 {
         //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 {
        private IntWritable result = new IntWritable();
        public IntSumReducer() {
        }
        public void reduce(Text key, Iterable values, Reducer.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系统文件:

发现了myout文件,词频统计功能实现。

对程序的理解
转载请注明:文章转载自 www.mshxw.com
本文地址:https://www.mshxw.com/it/847216.html
我们一直用心在做
关于我们 文章归档 网站地图 联系我们

版权所有 (c)2021-2022 MSHXW.COM

ICP备案号:晋ICP备2021003244-6号