Eclipse环境搭建并且运行wordcount程序

摘要:
一、安装Hadoop插件  1.所需环境hadoop2.0伪分布式环境平台正常运行所需压缩包:eclipse-jee-luna-SR2-linux-gtk-x86_64.tar.gz在Linux环境下运行的eclipse软件压缩包,解压后文件名为eclipsehadoop2x-eclipse-plugin-master.zip在eclipse中需要安装的Hadoop插件,解压后文件名为hadoop2x-eclipse-plugin-master如图所示,将所有的压缩包放在同一个文件夹下并解压。
一、安装Hadoop插件

  1. 所需环境

hadoop2.0伪分布式环境平台正常运行

所需压缩包:eclipse-jee-luna-SR2-linux-gtk-x86_64.tar.gz
在Linux环境下运行的eclipse软件压缩包,解压后文件名为eclipse
hadoop2x-eclipse-plugin-master.zip
在eclipse中需要安装的Hadoop插件,解压后文件名为hadoop2x-eclipse-plugin-master

如图所示,将所有的压缩包放在同一个文件夹下并解压。

Eclipse环境搭建并且运行wordcount程序第1张

  2.编译jar包

编译hadoop2x-eclipse-plugin-master的plugin 的插件源码,需要先安装ant工具

Eclipse环境搭建并且运行wordcount程序第2张

接着输入命令(注意ant命令在什么路径下使用,具体路径在下一张截图中,不然这个命令会用不了):

ant jar -Dversion=2.6.0 -Declipse.home='/home/xiaow/hadoop2.0/eclipse'  # 刚才放进去的eclipse软件包的路径 -Dversion=2.6.0 hadoop的版本号
              -Dhadoop.home='/home/xiaow/hadoop2.0/hadoop-2.6.0'  # hadoop安装文件的路径

Eclipse环境搭建并且运行wordcount程序第3张

等待一小会时间就好了
编译成功后,找到放在 /home/xiaow/ hadoop2.0/hadoop2x-eclipse-pluginmaster/build/contrib/eclipse-plugin下, 名为hadoop-eclipse-plugin-2.6.0.jar的jar包, 并将其拷贝到/hadoop2.0/eclipse/plugins下

输入命令

cp -r /home/xiaow/hadoop2.0/hadoop2x-eclipse-plugin-master/build/contrib/eclipse-plugin/hadoop-eclipse-plugin-2.6.0.jar /home/xiaow/hadoop2.0/eclipse/plugins/

Eclipse环境搭建并且运行wordcount程序第4张

Eclipse环境搭建并且运行wordcount程序第5张

Eclipse环境搭建并且运行wordcount程序第6张

二、Eclipse配置

接下来打开eclipse软件

Eclipse环境搭建并且运行wordcount程序第7张

一定要出现这个图标,没有出现的话前面步骤可能错了,或者重新启动几次Eclipse

Eclipse环境搭建并且运行wordcount程序第8张

然后按照下面的截图操作:

Eclipse环境搭建并且运行wordcount程序第9张

Eclipse环境搭建并且运行wordcount程序第10张

如此,Eclipse环境搭建完成。

三、wordcount程序

建工程:

Eclipse环境搭建并且运行wordcount程序第11张

Eclipse环境搭建并且运行wordcount程序第12张

Eclipse环境搭建并且运行wordcount程序第13张

Eclipse环境搭建并且运行wordcount程序第14张

Eclipse环境搭建并且运行wordcount程序第15张

Eclipse环境搭建并且运行wordcount程序第16张

Eclipse环境搭建并且运行wordcount程序第17张

输入如下代码:

package wordcount;
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.LongWritable;  
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.input.TextInputFormat;  
import org.apache.hadoop.mapreduce.lib.output.FileOutputFormat;  
import org.apache.hadoop.mapreduce.lib.output.TextOutputFormat;  
import org.apache.hadoop.mapreduce.lib.reduce.IntSumReducer;
import org.apache.hadoop.util.GenericOptionsParser;
public class wordcount {  
	// 自定义的mapper,继承org.apache.hadoop.mapreduce.Mapper
    public static class WordCountMap extends Mapper<LongWritable, Text, Text, IntWritable> {  
        private final IntWritable one = new IntWritable(1);  
        private Text word = new Text();  
        //  Mapper<LongWritable, Text, Text, LongWritable>.Context context
        public void map(LongWritable key, Text value, Context context)   throws IOException, InterruptedException {  
            String line = value.toString();  
            System.out.println(line);
            // split 函数是用于按指定字符(串)或正则去分割某个字符串,结果以字符串数组形式返回,这里按照“	”来分割text文件中字符,即一个制表符
            // ,这就是为什么我在文本中用了空格分割,导致最后的结果有很大的出入
            StringTokenizer token = new StringTokenizer(line);  
            while (token.hasMoreTokens()) {  
                word.set(token.nextToken());  
                context.write(word, one);  
            }  
        }  
    }  
    // 自定义的reducer,继承org.apache.hadoop.mapreduce.Reducer
    public static class WordCountReduce extends Reducer<Text, IntWritable, Text, IntWritable> {  
    	// Reducer<Text, LongWritable, Text, LongWritable>.Context context
        public void reduce(Text key, Iterable<IntWritable> values, Context context) throws IOException, InterruptedException {  
        	System.out.println(key);
        	System.out.println(values);
            int sum = 0;  
            for (IntWritable val : values) {  
                sum += val.get();  
            }  
            context.write(key, new IntWritable(sum));  
        }  
    }  
    //  客户端代码,写完交给ResourceManager框架去执行
    public static void main(String[] args) throws Exception {  
        Configuration conf = new Configuration();  
        Job job = new Job(conf,"word count"); 
        //  打成jar执行
        job.setJarByClass(wordcount.class);     
        //  数据在哪里?
        FileInputFormat.addInputPath(job, new Path(args[0])); 
        //  使用哪个mapper处理输入的数据?
        job.setMapperClass(WordCountMap.class); 
        //  map输出的数据类型是什么?
        //job.setMapOutputKeyClass(Text.class);
        //job.setMapOutputValueClass(LongWritable.class);
        job.setCombinerClass(IntSumReducer.class);
        //  使用哪个reducer处理输入的数据
        job.setReducerClass(WordCountReduce.class); 
        //  reduce输出的数据类型是什么?
        job.setOutputKeyClass(Text.class);  
        job.setOutputValueClass(IntWritable.class);  
//        job.setInputFormatClass(TextInputFormat.class);  
//        job.setOutputFormatClass(TextOutputFormat.class);  
        //  数据输出到哪里?
        FileOutputFormat.setOutputPath(job, new Path(args[1]));  
        //  交给yarn去执行,直到执行结束才退出本程序
        job.waitForCompletion(true);  
        /*
        String[] otherArgs = new GenericOptionsParser(conf,args).getRemainingArgs();
        if(otherArgs.length<2){
        	System.out.println("Usage:wordcount <in> [<in>...] <out>");
        	System.exit(2);
        }
        for(int i=0;i<otherArgs.length-1;i++){
        	FileInputFormat.addInputPath(job, new Path(otherArgs[i]));
        }
        System.exit(job.waitForCompletion(tr0ue)?0:1);
        */
    }  
}  

Eclipse环境搭建并且运行wordcount程序第18张

Eclipse环境搭建并且运行wordcount程序第19张

Eclipse环境搭建并且运行wordcount程序第20张

Eclipse环境搭建并且运行wordcount程序第21张 

将准备到的文档导入进去

Eclipse环境搭建并且运行wordcount程序第22张

Eclipse环境搭建并且运行wordcount程序第23张

目录结构如下:

Eclipse环境搭建并且运行wordcount程序第24张

运行mapreduce程序

Eclipse环境搭建并且运行wordcount程序第25张

Eclipse环境搭建并且运行wordcount程序第26张

Eclipse环境搭建并且运行wordcount程序第27张

Eclipse环境搭建并且运行wordcount程序第28张

Eclipse环境搭建并且运行wordcount程序第29张

OK,搞定收工!!!

免责声明:文章转载自《Eclipse环境搭建并且运行wordcount程序》仅用于学习参考。如对内容有疑问,请及时联系本站处理。

上篇ubuntu环境下部署SVN自动更新总结React 路由下篇

宿迁高防,2C2G15M,22元/月;香港BGP,2C5G5M,25元/月 雨云优惠码:MjYwNzM=

相关文章

使用hive访问elasticsearch的数据

使用hive访问elasticsearch的数据 1.配置 将elasticsearch-hadoop-2.1.1.jar拷贝到hive/lib hive -hiveconf hive.aux.jars.path=/usr/local/hive-1.2.1/lib/elasticsearch-hadoop-2.1.1.jar 或者配置: hive-s...

基于kubernetes实现链路监控

介绍 官方文档:https://skywalking.apache.org/docs/main/latest/readme/ chart包地址:https://github.com/apache/skywalking-kubernetes 实践 Install released version using Helm repository 下载cha...

Mac下搭建php开发环境【转】

Mac OS X 内置了Apache 和 PHP,这样使用起来非常方便。本文以Mac OS X 10.6.3为例。主要内容包括: 启动Apache 运行PHP 安装MySQL 使用phpMyAdmin 配置PHP的MCrypt扩展库 设置虚拟主机 启动Apache   有两种方法: 打开“系统设置偏好(System Preferences)” ->...

Spring Boot常见问题(二)Unable to start embedded container; nested exception is java.lang.NoSuchMethodError: org.apache.tomcat.util.scan.StandardJarScanner.setJarScanFilter(Lorg/apache/tomcat/JarScanFilter;

问题描述:通过Spring Boot官方提供的方式,写出如下HelloWorld代码。 @Controller @EnableAutoConfiguration public class HelloWorld { @RequestMapping("/wu") @ResponseBody String home() {...

Flink DataStream API

5.Flink DataStream API 5.1 Flink 运行模型     以上为 Flink 的运行模型,Flink 的程序主要由三部分构成,分别为 Source、 Transformation、Sink。DataSource 主要负责数据的读取,Transformation 主要负责对 属于的转换操作,Sink 负责最终数据的输出。 5.2...

Apache Beam实战指南 | 大数据管道(pipeline)设计及实践

Apache Beam实战指南 | 大数据管道(pipeline)设计及实践  mp.weixin.qq.com 策划 & 审校 | Natalie作者 | 张海涛编辑 | LindaAI 前线导读: 本文是 Apache Beam 实战指南系列文章第五篇内容,将对 Beam 框架中的 pipeline 管道进行剖析,并结合应用示例介绍如...